思路:直接开个优先队列搞搞,如果前面的行程已经安排满了,那么就替换最小值
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn = 50000+7;
int n;
struct Node
{
int w;
int t;
}a[maxn];
bool cmp(Node a,Node b)
{
return a.t<b.t;
}
priority_queue<int,vector<int>,greater<int> >q;
int main()
{
scanf("%d",&n);
for(int i = 1;i<=n;i++)
scanf("%d%d",&a[i].t,&a[i].w);
LL ans = 0;
sort(a+1,a+1+n,cmp);
for(int i = 1;i<=n;i++)
{
if(a[i].t>q.size())
{
q.push(a[i].w);
ans+=a[i].w;
}
else
{
if(a[i].w > q.top())
{
ans-=q.top();
q.pop();
ans+=a[i].w;
q.push(a[i].w);
}
}
}
printf("%lld\n",ans);
}
基准时间限制: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