AcWing 145. 超市——Java代码版-堆结果集
原题链接
简单
作者:
三玖天下第一
,
2021-03-20 14:37:51
,
所有人可见
,
阅读 476
import java.io.*;
import java.util.*;
public class Main{
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
ArrayList<Pair> list = new ArrayList<>(10000);
PriorityQueue<Pair> res = new PriorityQueue<>((o1, o2) -> o1.value-o2.value);
while(sc.hasNext()){
int n = sc.nextInt();
list.clear();
res.clear();
for (int i = 0; i < n; i++) {
int value = sc.nextInt();
int day = sc.nextInt();
list.add(new Pair(day, value));
}
Collections.sort(list,(o1, o2) -> o1.day-o2.day);
int days = 0;
int le = 0, re = list.size();
while(le<re){
Pair p = list.get(le++);
days = p.day;
res.add(p);
if (res.size() > days){
res.poll();
}
}
int out = 0;
while(!res.isEmpty()){
out += res.poll().value;
}
writer.write(out+"\n");
}
writer.flush();
}
static class Pair{
int day;
int value;
public Pair(int day, int value) {
this.day = day;
this.value = value;
}
}
}