HDU 4393 Throw nails(优先队列)

优先队列的应用  

好坑,好坑,好坑,重要的事情说三遍!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define maxn 50005
struct Node
{

int
f,s,id;
friend
bool operator < (Node a,Node b)
{

if
(a.f != b.f) return a.f < b.f;
else return
a.id > b.id;
}
}
node[maxn];
priority_queue<Node>que[];
int
main()
{

int
t,n,Case =;
scanf("%d",&t);
while
(t--)
{

scanf("%d",&n);
for
(int i =; i < n; i++)
{

scanf("%d%d",&node[i].f,&node[i].s);
node[i].id = i+;
que[node[i].s].push(node[i]);
}

printf("Case #%d:\n",++Case);
int
now =;
while
(n--)
{

int
maxt = -,minid =,pos =;
for
(int i =; i <=; i++)
{

if
(!que[i].empty())
{

Node te = que[i].top();
int
tmp = te.f + te.s*now;
if
(tmp > maxt)
{

maxt = tmp;
minid = te.id;
pos = i;
}

if
(tmp == maxt)
{

if
(te.id < minid)
{

minid = te.id;
pos = i;
}
}
}
}

if
(!que[pos].empty())que[pos].pop();
if
(n ==) printf("%d",minid);
else
printf("%d ",minid);
now++;
}

puts("");
}
}
上一篇:四、Socket之UDP异步传输文件-用控件显示文件传输进度


下一篇:HDU 4393 Throw nails