HDU 4393 Throw nails (简单题)

Throw nails

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 647    Accepted Submission(s): 194

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
2012 Multi-University Training Contest 10  

 

Recommend zhuyuanchen520  

 

Statistic | 
Submit | 
Discuss |
Note

 

 

 

 

 

主要是思路,其实仔细想一想不难的。

F<=500,S<=100

我的做法是把所有速度相同的放在一个优先队列里面,优先队列先按照F从大到小排序,F相同则按照序号从小到大排序。

然后每次从队列顶部找出距离最远的出队。

 

还有种做法就是考虑到F<=500,所以501s之后F已经没有影响了,完全由S决定。

所以可以先暴力到500s,之后就按照S的大小从大到小输出了。

 

 

/*
HDU 4393
G++ 406ms  1044K
*/

#include<stdio.h>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;

struct Node
{
    int F;
    int index;
    friend bool operator<(Node a,Node b)
    {
        if(a.F!=b.F)return a.F<b.F;//F大的优先级高
        else return a.index>b.index;//index小的优先级高
    }
};
priority_queue<Node>q[110];
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int T;
    int iCase=0;
    int n;
    Node a;
    int S;
    scanf("%d",&T);
    while(T--)
    {
        iCase++;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&a.F,&S);
            a.index=i;
            q[S].push(a);
        }
        printf("Case #%d:\n",iCase);
        for(int i=0;i<n;i++)
        {
            int fast=-1,id=1;
            for(int j=1;j<=100;j++)
              if(!q[j].empty())
              {
                  Node tmp=q[j].top();
                  if(tmp.F+i*j>fast) fast=tmp.F+i*j,id=j;
                  else if(tmp.F+i*j==fast&&tmp.index<q[id].top().index)id=j;
              }
            printf("%d",q[id].top().index);
            q[id].pop();
            if(i<n-1)printf(" ");
            else printf("\n");
        }
    }
    return 0;
}

 

 

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/kuangbin/archive/2012/08/27/2658459.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞