http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1163&judgeId=598136
基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题
收藏
关注
有N个任务,每个任务有一个最晚结束时间以及一个对应的奖励。在结束时间之前完成该任务,就可以获得对应的奖励。完成每一个任务所需的时间都是1个单位时间。有时候完成所有任务是不可能的,因为时间上可能会有冲突,这需要你来取舍。求能够获得的最高奖励。
Input
第1行:一个数N,表示任务的数量(2 <= N <= 50000) 第2 - N + 1行,每行2个数,中间用空格分隔,表示任务的最晚结束时间E[i]以及对应的奖励W[i]。(1 <= E[i] <= 10^9,1 <= W[i] <= 10^9)
Output
输出能够获得的最高奖励。
Input示例
7 4 20 2 60 4 70 3 40 1 30 4 50 6 10
Output示例
230
看a出来的人挺多的,就过来a这道题。。。。没想到卡着了,容器不熟悉,果然要吃大亏的.....一直在想怎么能让时间确定的情况下选最大的的和。。。怎么排序都不行因为它不是一个动态的,在网上一看...优先队列woc..........原来是这样
#include<iostream>
#include<string.h>
#include<algorithm>
#include<queue>
#include<vector>
#define maxn 50005
#define ll long long
using namespace std;
struct node
{
int time;
int p;
}ac[maxn];
bool cmp(node a,node b)
{
if(a.time==b.time)
return a.p>b.p;
return a.time<b.time;
}
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>ac[i].time>>ac[i].p;
}
sort(ac,ac+n,cmp);
ll ans=0;
priority_queue<int,vector <int> ,greater <int> >que;
for(int i=0;i<n;i++)
{
int q=ac[i].p;
if(ac[i].time>que.size())
{
ans+=q;
que.push(q);
}
else
{
ans+=q;
que.push(q);
ans-=que.top();
que.pop();
}
}
cout<<ans<<endl;
return 0;
}
优先队列写出来了,在网上看别人的代码终于找到一个不用优先队列的,emmmm她竟然用了并查集,而且是我想用的之前一直没想通的先排质量,然后不断的更新,,真的厉害了。
#include<iostream>
#include<string.h>
#include<algorithm>
#include<queue>
#include<vector>
#define maxn 50005
#define ll long long
using namespace std;
struct node
{
int time;
int p;
}ac[maxn];
int par[maxn]; //最晚i秒的事情可在j秒完成
bool cmp(node a,node b)
{
return a.p>b.p;
}
int find(int x)
{
if(x<=0) return -1;
else if(x==par[x])
{
par[x]=x-1;
}
else return par[x]=find(par[x]);//路径压缩
}
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>ac[i].time>>ac[i].p;
par[i]=i;
}
sort(ac,ac+n,cmp);
ll ans=0;
for(int i=0;i<n;i++)
{
int t=ac[i].time;
if(find(t)>=0)
{
ans+=ac[i].p;
}
}
cout<<ans<<endl;
return 0;
}