Bestcoder_Round#27_1002-Taking bus(HDUOJ_5163)

Problem Description

There are n kids and they want to know who can jump the farthest. For each kid, he can jump three times and the distance he jumps is maximum distance amount all the three jump. For example, if the distance of each jump is (10, 30, 20), then the farthest distance he can jump is 30. Given the distance for each jump of the kids, you should find the rank of each kid.

Input

There are multiple test cases. The first line of input contains an integer T (1T100) , indicating the number of test cases. For each test case: The first line contains an integer n (2n3) , indicating the number of kids. For the next n lines, each line contains three integers ai,bi and ci ( 1ai,bi,ci,300 ), indicating the distance for each jump of the i -th kid. It's guaranteed that the final rank of each kid won't be the same (ie. the farthest distance each kid can jump won't be the same).

Output

For each test case, you should output a single line contain n integers, separated by one space. The i -th integer indicating the rank of i -th kid.

Sample Input

   
   
   
   
2 3 10 10 10 10 20 30 10 10 20 2 3 4 1 1 2 1

Sample Output

   
   
   
   
3 1 2 1 2
Hint
For the first case, the farthest distance each kid can jump is 10, 30 and 20. So the rank is 3, 1, 2.
 

比赛的时候大脑短路,没想出高效快速的方法,一个劲的在那里用循环,虽然后来算法改进了一点,但还是很麻烦,还是有很多循环,超时什么的已经快吃吐了。其实这题只要分类讨论一下,分别把公式写出来就好。用time_start代表起始时间,用x和y代表起点和终点,那么大致有以下六种情况:time_start≤x<y , x<time_start≤y , x<y<time_start , time_start≤y<x , y<time_start≤x , y<x<time_start。其中有几种情况公式是一样的,我把它们合并在了一起,具体内容请看代码。

代码

#include <cstdio>

long long time[100000];

int main()
{
    int t,n,m,time_start,d,x,y,i;
    long long time_all; //注意要用long long
    scanf("%d",&t);
    while(t--)
    {
        time[0]=0;
        scanf("%d%d",&n,&m);
        for(i=1; i<n; i++)
        {
            scanf("%d",&d);
            time[i]=time[i-1]+d;
        }
        for(i=1; i<=m; i++)
        {
            time_start=(i-1)%n+1;
            scanf("%d%d",&x,&y);
            if(x<y)
            {
                if(time_start<=x)
                    time_all=time[y-1]-time[time_start-1];
                else
                    time_all=2*time[n-1]-time[time_start-1]+time[y-1];
            }
            else
            {
                time_all=2*time[n-1]-time[time_start-1]-time[y-1];
            }
            printf("%lld\n",time_all);
        }
    }
    return 0;
}


你可能感兴趣的:(Bestcoder_Round#27_1002-Taking bus(HDUOJ_5163))