先排序
4
2 10
2 11
3 20
3 21
在用优先队列,按价值小的优先,day<=s[i].d入队,
队列的容量要<=day,如>day,出队
View Code
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
struct data
{
long long d;
long long p;
}s[100009];
struct data1
{
long long d;
long long p;
data1(data a)
{
d=a.d;
p=a.p;
}
friend bool operator<(data1 a,data1 b)
{
return a.p>b.p;//内部按val从小到大
}
};
int cmp(data a,data b)
{
if(a.d==b.d)
return a.p>b.p;
else
return a.d<b.d;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int i,max=0;
for(i=1;i<=n;i++)
{
scanf("%lld%lld",&s[i].d,&s[i].p);
if(max<s[i].d)max=s[i].d;
}
sort(&s[1],&s[n+1],cmp);
priority_queue<data1> qq;
int day=1;i=1;
while(day<=max)
{
for(i;i<=n;i++)
{
if(s[i].d<=day)
{
qq.push(s[i]);
}
else
break;
}
while(qq.size()>day)
qq.pop();
day++;
}
long long all=0;
while(!qq.empty())
{
all+=qq.top().p;
qq.pop();
}
printf("%lld\n",all);
}
return 0;
}