题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1163
分析:本题采用优先队列是一个比较好的选择,每次替换之前最小的。
代码:
#include <iostream>
#include <string.h>
#include <algorithm>
#include <queue>
#include <stdio.h>
using namespace std;
const int N = 50005;
typedef long long LL;
struct Node
{
int E,W;
friend bool operator < (Node a, Node b)
{
return a.W > b.W;
}
};
Node node[N];
bool cmp(Node a, Node b)
{
return a.E < b.E;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d %d", &node[i].E,&node[i].W);
sort(node, node+n, cmp);
priority_queue<Node> Q;
for(int i=0; i<n; i++)
{
if(Q.size() < node[i].E)
Q.push(node[i]);
else if(node[i].W > (Q.top()).W)
{
Q.pop();
Q.push(node[i]);
}
}
LL ans = 0;
while(!Q.empty())
{
ans += (Q.top()).W;
Q.pop();
}
printf("%I64d\n", ans);
return 0;
}