您的位置:首页 > 产品设计 > UI/UE

hdu4393(模拟+queue)

2013-07-31 23:56 344 查看

Throw nails

[b]Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1007    Accepted Submission(s): 323
[/b]

[align=left]Problem Description[/align]
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.
 

[align=left]Input[/align]
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.

 

[align=left]Output[/align]
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.

 

[align=left]Sample Input[/align]

2
3
100 1
100 2
3 100
5
1 1
2 2
3 3
4 1
3 4

 

[align=left]Sample Output[/align]

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

 本题若果只是模拟而不用STL优化的话,我想是很容易超时的,我用的模拟+STL,时间复杂度为O(N*S*logM)。总共选n次,0-n-1,正好可以当做秒数,F+i*S为当前每个的路程,其中可以以S把数据分成100组,每组的后面速度相同,只取决于F,从而对于每组建立优先级队列,对头的总是F最大,且在F相同的情况下下表序号最小。#include<stdio.h>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;struct Node
{
    int F,S;
    int index;
};struct cmp////////////////////////注意优先级队列的排序方式
{
 bool operator()(Node a,Node b)
 {
  if(a.F!=b.F)
   return a.F<b.F;///F从大到小排序
  return a.index>b.index;//index从小到大排序
 }
};priority_queue<Node,vector<Node>,cmp>qq[110];///////////////////int main()
{
 int cas,i,n,j,tag=1;
 cin>>cas;
 while(cas--)
 {
  scanf("%d",&n);
  Node tmp;
  for(i=0;i<n;i++)
  {
   scanf("%d%d",&tmp.F,&tmp.S);
   tmp.index=i+1;
   qq[tmp.S].push(tmp);
  }  printf("Case #%d:\n",tag++);
  for(i=0;i<n;i++)
  {
   int Max=-1,ind;
   for(j=1;j<=100;j++)
   {
    if(!qq[j].empty())
    {
     tmp=qq[j].top();
     // cout<<"j="<<j<<"    tmp.F="<<tmp.F<<endl;
     if(tmp.F+j*i>Max)
      Max=tmp.F+j*i,ind=j;
     else if(tmp.F+j*i==Max&&tmp.index<(qq[ind].top()).index)
      ind=j;
    }
   }   printf("%d",qq[ind].top().index);
    qq[ind].pop();
   if(i!=n-1)
    printf(" ");
   else printf("\n");
  }
 }
 return 0;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构 STL