目录
D - Printing Machine:
题目大意:
思路解析:
代码实现:
D - Printing Machine:
题目大意:
思路解析:
打印一次后,需要充电一微秒后才能再次打印就可以看作每微妙只能打印一个产品。然后就可以这个题看作一个贪心的过程,我们一定是要每次打印已经进入了打印机的产品中最快退出打印机的产品。 因为打印玩它后,你还可以继续打印哪些没有退出打印机的产品,如果先打印后退出的,可能会导致部分产品还没有打印就已经退出。 (这里遍历时,如果遇到能加入打印机的产品,也就产品加入打印机)
如果整个打印机中没有产品,我们就将时间调整到下一个最快能进入打印机的时刻,这样就可以快速遍历时间。
代码实现:
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
static int inf = (int) 2e7;
public static void main(String[] args) throws IOException {
int t = 1;
while (t > 0) {
solve();
t--;
}
w.flush();
w.close();
br.close();
}
static int maxN = 100005;
public static void solve() {
int n = f.nextInt();
PriorityQueue<Long> use = new PriorityQueue<>(new Comparator<Long>() {
@Override
public int compare(Long o1, Long o2) {
return Long.compare(o1, o2);
}
});
long ans = 0;
long[][] a = new long[n][2];
for (int i = 0; i < n; i++) {
a[i][0] = f.nextLong(); a[i][1] = f.nextLong();
}
Arrays.sort(a, ((o1, o2) -> {
return Long.compare(o1[0], o2[0]);
}));
int it = 0;
for (long i = 0;;i++) {
if (use.isEmpty()){
if(it == n) break;
i = a[it][0];
}
while (it < n && a[it][0] == i){
use.add(a[it][0] + a[it][1]);
it++;
}
while (!use.isEmpty() && use.peek() < i) use.poll();
if (!use.isEmpty()){
use.poll();
ans++;
}
}
w.println(ans);
}
public static class Node{
long x;
public Node(long x) {
this.x = x;
}
}
static PrintWriter w = new PrintWriter(new OutputStreamWriter(System.out));
static Input f = new Input(System.in);
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static class Input {
public BufferedReader reader;
public StringTokenizer tokenizer;
public Input(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public String nextLine() {
String str = null;
try {
str = reader.readLine();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return str;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public Double nextDouble() {
return Double.parseDouble(next());
}
public BigInteger nextBigInteger() {
return new BigInteger(next());
}
}
}