hdu 4393 Throw nails(STL之优先队列)

本文详细介绍了一种解决自行车竞赛中干扰者问题的算法。通过使用优先队列和双层循环,该算法能够准确地预测在每一轮比赛中,哪位选手会因干扰者的行动而被淘汰。文章提供了完整的代码实现,并解释了如何利用选手的速度和初始位置来计算其在比赛中的位置,进而确定被淘汰的选手。
摘要由CSDN通过智能技术生成
Problem Description
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
2
3
100 1
100 2
3 100
5
1 1
2 2
3 3
4 1
3 4

 

 

 

Sample Output
Case #1:
1 3 2
Case #2:
4 5 3 2 1

 


Hint
Hint The first case: 1st Second end Player1 100m (BOOM!!) Player2 100m Player3 3m 2nd Second end Player2 102m Player3 103m (BOOM!!) 3rd Second end Player2 104m (BOOM!!)
 

 

Source
 
 
这道题要用优先队列来解决。
首先定义一个优先队列,类型为Node,Node里面有两个参数f和index,f表示当前的距离,index表示下标。在Node里面排序(如果f大则优先,如果f相等则下标小的优先)
优先队列的数量100个就够了,因为s最大才100.
然后就是两个for循环,第一个for循环0~n-1,表示一共要淘汰n次,且这个循环的i可以表示当前时间的秒数供下面计算距离
第二个for循环j表示s,范围为1~100,条件是q[j].empty()==0.找出最大的距离。id表示s,如果有多个最大的距离相等,id表示最小的s。
输出printf("%d",q[id].top().index);然后q[id].pop();即可
有够详细了。
 1 #pragma comment(linker, "/STACK:1024000000,1024000000")
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<math.h>
 7 #include<algorithm>
 8 #include<queue>
 9 #include<set>
10 #include<bitset>
11 #include<map>
12 #include<vector>
13 #include<stdlib.h>
14 #include <stack>
15 using namespace std;
16 #define PI acos(-1.0)
17 #define max(a,b) (a) > (b) ? (a) : (b)
18 #define min(a,b) (a) < (b) ? (a) : (b)
19 #define ll long long
20 #define eps 1e-10
21 #define MOD 1000000007
22 #define N 50006
23 #define inf 1e12
24 int n;
25 struct Node{
26    int f,index;
27    friend bool operator < (Node a,Node b){
28       if(a.f!=b.f) return a.f<b.f;
29       return a.index>b.index;
30    }
31 };
32 int main()
33 {
34    int t;
35    int ac=0;
36    scanf("%d",&t);
37    while(t--){
38 
39       priority_queue<Node>q[106];
40 
41       scanf("%d",&n);
42       Node tmp;
43       int s;
44       for(int i=1;i<=n;i++){
45          scanf("%d%d",&tmp.f,&s);
46          tmp.index=i;
47          q[s].push(tmp);
48       }
49       printf("Case #%d:\n",++ac);
50       for(int i=0;i<n;i++){
51          int fast=-1,id=1;
52          for(int j=1;j<=100;j++){
53             if(!q[j].empty()){
54                Node cnt=q[j].top();
55                int now_dit=cnt.f+i*j;
56                if(now_dit>fast) fast=now_dit,id=j;
57                else if(now_dit==fast){
58                   if(q[id].top().index>cnt.index){
59                      id=j;
60                   }
61                }
62             }
63          }
64          printf("%d",q[id].top().index);
65          q[id].pop();
66          if(i!=n-1){
67             printf(" ");
68          }
69       }
70       printf("\n");
71 
72 
73 
74    }
75     return 0;
76 }
View Code

 

转载于:https://www.cnblogs.com/UniqueColor/p/4988928.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值