HDU 4393 Throw nails(技巧性模拟)

来源:互联网 发布:jdbc连接数据库 编辑:程序博客网 时间:2024/07/24 02:35

The annual school bicycle contest started. ZL is a student in this school. He is so boring because he can't ride a bike!! So he decided to interfere with the contest. He has got the players' information by previous contest video. A player can run F meters the first second, and then can run S meters every second. 
Each player has a single straight runway. And ZL will throw a nail every second end to the farthest player's runway. After the "BOOM", this player will be eliminated. If more then one players are NO.1, he always choose the player who has the smallest ID.
Input
In the first line there is an integer T (T <= 20), indicates the number of test cases. 
In each case, the first line contains one integer n (1 <= n <= 50000), which is the number of the players. 
Then n lines follow, each contains two integers Fi(0 <= Fi <= 500), Si (0 < Si <= 100) of the ith player. Fi is the way can be run in first second and Si is the speed after one second .i is the player's ID start from 1. 
Hint

Huge input, scanf is recommended. 
Huge output, printf is recommended. 
Output
For each case, the output in the first line is "Case #c:". 
c is the case number start from 1. 
The second line output n number, separated by a space. The ith number is the player's ID who will be eliminated in ith second end. 
Sample Input
23100 1100 23 10051 12 23 34 13 4
Sample Output
Case #1:1 3 2Case #2:4 5 3 2 1          
Hint
HintThe first case:1st Second endPlayer1  100m    (BOOM!!)Player2  100mPlayer3    3m2nd Second endPlayer2  102m    Player3  103m    (BOOM!!)3rd  Second endPlayer2  104m    (BOOM!!)         


题解:

题意:

有n个人跑步,每一秒你要给跑在最前撒钉子,boom,如果有多个人就boom掉编号最小的,每boom掉一个人输出他的编号

思路:

一开始用数组模拟,每次写一个带变量的cmp+sort排序华丽得tle了2发,冷静下来,其实这题由于初始值是500最大,所以两个人之间的差距最大也就是500,一个人的速度最小是那1,那么过501秒后是不是胜负已分,落后的再也追不上前面的了呢,所以就用带点技巧模拟前501s的情况,然后后面的只要按照501s的时候的排名输出就行了,不必再模拟下去了

代码:

#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>#include<stack>#include<math.h>#include<vector>#include<map>#include<set>#include<stdlib.h>#include<cmath>#include<string>#include<algorithm>#include<iostream>using namespace std;#define lson k*2#define rson k*2+1#define M (t[k].l+t[k].r)/2#define ll long long#define INF 10086111int t;struct node{    int s;    int v;    int index;    friend bool operator<(node x,node y)//用于501s后的排序,不同路程路程长的在前,相同路程标号小的在前    {        if(x.s==y.s)            return x.index<y.index;        return x.s>y.s;    }}a[50005];int n;int find()//查找一个最大的index{    int i,maxx=-1,index=1008611,p;    for(i=0;i<n;i++)    {        if(maxx<a[i].s||(maxx==a[i].s&&a[i].index<index))//我wa了2发就是这个,标号小的等值也是要替换的        {            p=i;            maxx=a[i].s;            index=a[i].index;        }        a[i].s+=a[i].v;    }    if(p!=n-1)//把该节点换到最后        swap(a[p],a[n-1]);    n--;//数组大小--,每次查找就会快一些    return index;}int main(){int m,i,j;int test,p,size;node now;while(scanf("%d",&test)!=EOF)    {        for(p=1;p<=test;p++)        {            scanf("%d",&n);            for(i=0;i<n;i++)            {                scanf("%d%d",&a[i].s,&a[i].v);                a[i].index=i+1;            }            printf("Case #%d:\n",p);            printf("%d",find());            t=0;            while(n>0&&t<502)//模拟前502s的情况。。501貌似也可以            {                printf(" %d",find());                t++;            }            if(n>0)//如果n还大于0,直接sort一遍输出            {                sort(a,a+n);                for(i=0;i<n;i++)                {                    printf(" %d",a[i].index);                }            }            printf("\n");        }    }    return 0;}


原创粉丝点击