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

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 <= ), indicates the number of test cases.
In each case, the first line contains one integer n ( <= n <= ), which is the number of the players.
Then n lines follow, each contains two integers Fi( <= Fi <= ), Si ( < Si <= ) 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 .
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

 
Sample Output
Case #:

Case #:
    
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();即可
有够详细了。
 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 50006
#define inf 1e12
int n;
struct Node{
int f,index;
friend bool operator < (Node a,Node b){
if(a.f!=b.f) return a.f<b.f;
return a.index>b.index;
}
};
int main()
{
int t;
int ac=;
scanf("%d",&t);
while(t--){ priority_queue<Node>q[]; scanf("%d",&n);
Node tmp;
int s;
for(int i=;i<=n;i++){
scanf("%d%d",&tmp.f,&s);
tmp.index=i;
q[s].push(tmp);
}
printf("Case #%d:\n",++ac);
for(int i=;i<n;i++){
int fast=-,id=;
for(int j=;j<=;j++){
if(!q[j].empty()){
Node cnt=q[j].top();
int now_dit=cnt.f+i*j;
if(now_dit>fast) fast=now_dit,id=j;
else if(now_dit==fast){
if(q[id].top().index>cnt.index){
id=j;
}
}
}
}
printf("%d",q[id].top().index);
q[id].pop();
if(i!=n-){
printf(" ");
}
}
printf("\n"); }
return ;
}
上一篇:ExtJs学习笔记之学习小结LoginDemo


下一篇:机器学习技法笔记:06 Support Vector Regression