hdu 4393 Throw nails 相同的状态做相同的处理 贪心

Throw nails

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


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
 

Recommend
zhuyuanchen520   |   We have carefully selected several similar problems for you:   4390  4398  4397  4396  4395 
 

相同的状态做相同的处理,以节约时间。

  n大,二重循环不可能,
Si 小(0 < Si <= 100),
Si相同的人的顺序有F[i]和自身编号决定(要利用这点),构造Si值从1到100的100个优先队列(S[i]大的优先),以节省时间。
选n次,每次只考虑每个队列的队头元素。


#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)
#define  lson   num<<1,le,mid
#define rson    num<<1|1,mid+1,ri
#define MID   int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mk    make_pair
#define _f     first
#define _s     second

using namespace std;
const int INF =0x3f3f3f3f;
const int maxn=   50000+10  ;
//const int maxm=    ;
//const int INF=    ;
typedef long long ll;
const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
//by yskysker123

int n;
int v[maxn];
int f[maxn];
struct cmp{
    bool operator ()(int a,int b){
     if(f[a]!=f[b])
    return f[a]<f[b];
        //int a,int b   (默认右边大)大的优先返回<  //1 ,小的优先返回大于>  //0
    return a>b;
    }
};

priority_queue<int ,vector<int > ,cmp  > q[110];
int main()
{
    int T,kase=0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        printf("Case #%d:\n",++kase);
        for(int i=1;i<=100;i++)
        {
            while(!q[i].empty())
            q[i].pop();
        }
        FOR1(i,n)
        {
            scanf("%d%d",&f[i],&v[i]);
            q[v[i]].push( i );

        }

        for(int i=1;i<=n;i++)
        {
            int p,maxi=-1,pv;
            for(int j=1;j<=100;j++)
            {
                if(q[j].empty())  continue;
                int x=q[j].top();
                int tmp=   (i-1)*v[x]+f[x];
                if( tmp>maxi )  {maxi=tmp;p=x;pv=j;}
                else if(tmp==maxi&&x<p)  {maxi=tmp;p=x;pv=j;   }

            }
            q[pv ].pop();
            if(i==1) printf("%d",p);
            else
             printf(" %d",p);
        }
        putchar('\n');

    }


    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值