UESTC 95 Ants Run! 水题

Ants Run!

Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
Submit  Status

Professor Yang likes to play with ants when he is free. What? Are you asking why he plays with ants instead of others? Ah, because ant is the 

only non-plant living thing which can be found in Qingshuihe Campus of UESTC apart from human beings.

This time, Professor Yang caught several ants after finishing his lecture for freshmen. At the beginning of the game, he puts  N N ants around a 

plate and numbers them in clockwise order. The ants are so obedient that they run clockwise under the guide of Professor Yang on the boundary

 of the plate which is a circle. When one ant catches up with its previous ant, the game is over. Knowing the speed of ants, Professor Yang wants

 you to help him to adjust the distance between adjacent ants to make the game last longer.

Input

The first line of the input is  T  (no more than  10000 ), which stands for the number of test cases you need to solve.

Each test case begins with N R representing the number of ants participating the game is  N  and the radius of the circle is  R  cm. The next 

line lists  N integers and the  ith  number is the speed (cm/s) of the 
i
-th ant in clockwise direction. All numbers are positive integer not larger 

than  20 .

Output

If the game can last forever, print Inf in a single line, otherwise please output the longest time in seconds each game can last, which should be 

printed accurately rounded to three decimals.

Sample input and output

Sample Input Sample Output
2
3 1
1 1 1
3 1
3 2 1
Inf
3.142

Source

The 8th UESTC Programming Contest Preliminary
The question is from   here.

My Solution

本来觉得,可以把蚂蚁安排成逆序,比如 1追2,2追3,3追1;1追不上2,2追不上3,只有3可以追上1;所以2*π*R/(maxspeed - minspeed);
但是事实上蚂蚁是有顺序的,不能换顺序,所以只能统计出速度差,按比例安排距离,所过y <= x 则把xy放一起,然后用总总距离除以速度差的和。

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

int main()
{
    int T, N, R, x, y, first, sum;
    scanf("%d", &T);
    while(T--){
        sum = 0; y = 0;
        scanf("%d%d", &N, &R);
        for(int i = 1; i <= N; i++){
            scanf("%d", &x);
            if(i == 1) first = x;
            if(x < y) sum += (y-x);
            y = x;
        }
        if(first < y) sum += (y-first);
        if(sum != 0) printf("%.3f\n", 2*acos(-1)*R/sum);
        else printf("Inf\n");
    }
    return 0;
}

谢谢

你可能感兴趣的:(ACM,水题)