题目链接:
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1163
题解:
先按时间升序排列,再把价值降序排列,一直更新最小值就行了。(自己用的优先队列更的新)
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <functional>
using namespace std;
#define met(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
const int maxn = 50000+10;
struct node
{
int time;
int val;
}s[maxn];
int cmp(node a,node b)
{
if(a.time!=b.time)
return a.time<b.time;
return a.val>b.val;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
scanf("%d%d",&s[i].time,&s[i].val);
sort(s,s+n,cmp);
priority_queue<int,vector<int>,greater<int> >q;
while(!q.empty())
q.pop();
int len=1;
int pos=s[0].time;
q.push(s[0].val);
for(int i=1;i<n;i++)
{
if(s[i-1].time!=s[i].time)
{
q.push(s[i].val);
pos=s[i].time;
len++;
}
else if(s[i-1].time==s[i].time)
{
if(len<pos&&len<s[i].time)
{
q.push(s[i].val);
len++;
}
else
{
int num=q.top();
if(num<s[i].val)
{
q.pop();
q.push(s[i].val);
}
}
}
}
ll sum=0;
while(!q.empty())
{
int num=q.top();
//printf("%d\n",num);
sum+=num;
q.pop();
}
printf("%lld\n",sum);
}
}