-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirefly.cc
130 lines (96 loc) · 2.28 KB
/
firefly.cc
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <cstdio>
#include <vector>
// Fenwick?
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
const bool debug = false;
struct Obstacle {
int height;
bool up;
};
/**
* Tree.
*/
long u_tree[500001];
long d_tree[500001];
/**
* Sum.
*/
inline long sum_up(int index) {
long sum = 0;
while (index > 0) {
sum += u_tree[index];
index -= (index & -index);
}
return sum;
}
/**
* Add.
*/
inline void add_up(int height, long delta, int n) {
height++;
while (height <= n) {
u_tree[height] += delta;
height += (height & -height);
}
}
/**
* Sum.
*/
inline long sum_down(int height) {
long sum = 0;
while (height > 0) {
sum += d_tree[height];
height -= (height & -height);
}
return sum;
}
/**
* Add.
*/
inline void add_down(int height, long delta, int n) {
height++;
while (height <= n) {
d_tree[height] += delta;
height += (height & -height);
}
}
inline int collisions_at_height(int h, int max_height, int length, int mhds) {
int downies = mhds - sum_down(max_height - h);
int uppies = (length / 2) - sum_up(h + 1);
return uppies + downies;
}
// May need something fenwicky??
int main() {
int len, height; // len always even.
scanf("%d %d", &len, &height);
std::vector<int> at_height(height);
//std::vector<int> obstacles;
for (int i = 0; i < len; i += 2) {
int a, b; scanf("%d %d", &a, &b);
add_up(a, 1, height);
add_down(b, 1, height);
}
// int min_collisions = len;
// // int n = 0;
// for (int h = 0; h < height; h++) {
// int collisions = collisions_at_height(h, height, len);
// }
int mhds = sum_down(height);
int min_collisions = len;
int n = 0;
for (int h = 0; h < height; h++) {
int collisions = collisions_at_height(h, height, len, mhds); //at_height[h];
if (debug) printf("@%d = %d\n", h, collisions);
if (collisions < min_collisions) {
min_collisions = collisions_at_height(h, height, len, mhds);
n = 1;
} else if (collisions == min_collisions) {
n++;
}
}
//
printf("%d %d\n", min_collisions, n);
}