有N个任务,每个任务有一个最晚结束时间以及一个对应的奖励。在结束时间之前完成该任务,就可以获得对应的奖励。完成每一个任务所需的时间都是1个单位时间。有时候完成所有任务是不可能的,因为时间上可能会有冲突,这需要你来取舍。求能够获得的最高奖励。
收起
输入
第1行:一个数N,表示任务的数量(2 <= N <= 50000)
第2 - N + 1行,每行2个数,中间用空格分隔,表示任务的最晚结束时间E[i]以及对应的奖励W[i]。(1 <= E[i] <= 10^9,1 <= W[i] <= 10^9)
输出
输出能够获得的最高奖励。
输入样例
7
4 20
2 60
4 70
3 40
1 30
4 50
6 10
输出样例
230
先根据时间的原则进行从小到大排序,再利用优先队列,根据值的情况进队出队。
package aCM;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;
class nodess {
int v;
int time;
public nodess(int v, int time) {
super();
this.v = v;
this.time = time;
}
public nodess() {
super();
// TODO Auto-generated constructor stub
}
}
public class Main15 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner cin = new Scanner(System.in);
int n, s;
n = cin.nextInt();
List<nodess> list = new LinkedList<nodess>();
for (int i = 1; i <= n; i++) {
int v, time;
time = cin.nextInt();
v = cin.nextInt();
nodess no = new nodess(v, time);
list.add(no);
}
list.sort(new Comparator<nodess>() {
@Override
public int compare(nodess o1, nodess o2) {
// TODO Auto-generated method stub
return o1.time-o2.time;//时间从小到大排列
}
});
PriorityQueue<Integer>que=new PriorityQueue<Integer>();//默认从大到小;
long sum = 0;
int t=0;
for (nodess l : list) {//看时间!
if(l.time>que.size())
{
sum+=l.v;
que.add(l.v);
//这个时候直接进去
}else
{
//如果这个点的时间比较早
sum+=l.v;
que.add(l.v);
sum-=que.poll();//先加进去,反正后面小点是v要出去。
}
}
System.out.println(sum);
}
}