forked from nanwan03/poj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2373 Dividing the Path.java
73 lines (70 loc) · 1.66 KB
/
2373 Dividing the Path.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.*;
class Node {
int x;
int number;
Node(int x, int number) {
this.x = x;
this.number = number;
}
}
class NodeComparator implements Comparator<Node> {
public int compare(Node a, Node b) {
return a.number - b.number;
}
}
public class Main {
private static final int MAX = Integer.MAX_VALUE;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int number = in.nextInt();
int length = in.nextInt();
int[] rst = new int[length + 1];
int[] cows = new int[length + 1];
int leftBound = in.nextInt();
int rightBound = in.nextInt();
leftBound <<= 1;
rightBound <<= 1;
Queue<Node> queue = new PriorityQueue<Node>(length, new NodeComparator());
for (int i = 0; i < number; ++i) {
int leftRange = in.nextInt();
int rightRange = in.nextInt();
++cows[leftRange + 1];
--cows[rightRange];
}
int cowNumber = 0;
for (int i = 0; i <= length; ++i) {
rst[i] = MAX;
cowNumber += cows[i];
cows[i] = (cowNumber > 0) ? 1 : 0;
}
for (int i = leftBound; i <= rightBound; i = i + 2) {
if (cows[i] == 0) {
rst[i] = 1;
if (i <= rightBound - leftBound + 2) {
queue.offer(new Node(i, 1));
}
}
}
for (int i = rightBound + 2; i <= length; i = i + 2) {
if (cows[i] == 0) {
while (!queue.isEmpty()) {
Node n = queue.peek();
if (n.x >= i - rightBound) {
rst[i] = n.number + 1;
break;
} else {
queue.poll();
}
}
}
if (rst[i - leftBound + 2] != MAX) {
queue.offer(new Node(i - leftBound + 2, rst[i - leftBound + 2]));
}
}
if (rst[length] == MAX) {
System.out.println(-1);
} else {
System.out.println(rst[length]);
}
}
}