Find the law of the ultraviolet A-10714 +, and the ultraviolet A-10714

Source: Internet
Author: User

Find the law of the ultraviolet A-10714 +, and the ultraviolet A-10714

Question:

There are n ants on a wooden stick with len length, and the ants crawl at a speed of 1 cm/s. If an ant crawls to the wooden stick endpoint, it will fall down; if the two ants come together, they will turn around and crawl in the opposite direction. Enter the initial positions of len and n ants (with the left endpoint as the origin point). Q: the shortest time and longest time for all ants to fall down the wooden stick (the initial crawling direction of the ants is not fixed ).

Ideas:

The shortest time is obvious. As long as the ant financial whose names are close to the left endpoint crawls to the left endpoint and the ant financial whose names are close to the right endpoint crawls to the right endpoint, the shortest time can be reached; the shortest time is the maximum distance between all ants and Their endpoints.

I did not expect the longest time. In the following example, I found that the longest time is the maximum length of the ant distance from the endpoint.

After the code is written, it will pass .......

After reading other people's ideas, I know that because the rate of ant financial is constant, we can ignore this. (assume that A and B have switched to B. I only need to regard B as, if A is regarded as B, it can be done when they do not overhead). In this case, the problem is to find the maximum length of the ant from the endpoint, so the above idea is correct.

In fact, if you consider turning, you can also list the situations of 1, 2, and 3 ants for observation, and then you can get the rule.


The Code is as follows:


#include<iostream>#include<cstdio>#include<cstring>#include<cmath>using namespace std;int a[1100000];int main(){     int i,j,t,len,n,min1,max1;     scanf("%d",&t);     while(t--)     {            scanf("%d%d",&len,&n);            for(i=0;i<n;i++)                scanf("%d",&a[i]);            min1=0; max1=0;            for(i=0;i<n;i++)            {                  if(a[i]<len/2)                        min1=max(a[i],min1);                  else                       min1=max(len-a[i],min1);                  max1=max(max1,a[i]);                  max1=max(len-a[i],max1);            }            printf("%d %d\n",min1,max1);     } return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.