#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5005;
using pi = pair<int, int>;
int n, m;
int a[MAXN], b[MAXN], c[MAXN];
bool Do(vector<int> l, vector<int> r, vector<pi> intv, int K) {
l[0] = r[0] = 1e9;
for (int i = 1; i < l.size(); i++)
l[i] = min(l[i], l[i - 1]);
for (int i = 1; i < r.size(); i++)
r[i] = min(r[i], r[i - 1]);
l.push_back(0);
r.push_back(0);
sort(intv.begin(), intv.end());
priority_queue<int, vector<int>, greater<int>> pq;
int ptr = 0;
vector<int> opt(r.size());
for (int i = 0; i + 1 < l.size(); i++) {
while (ptr < intv.size() && intv[ptr].first <= i) {
pq.push(intv[ptr++].second);
}
while (l[i + 1] < K) {
if (pq.empty())
return 0;
int nxt = pq.top();
pq.pop();
opt[nxt]++;
K--;
}
}
for (int i = opt.size() - 2; i >= 0; i--)
opt[i] += opt[i + 1];
for (int i = 0; i < r.size(); i++) {
if (opt[i] > r[i])
return false;
}
return true;
}
int cnt[MAXN];
bool trial(int x) {
int maxpos = max_element(cnt + 1, cnt + n + 1) - cnt;
int mx = cnt[maxpos];
if (mx <= x)
return true;
for (int i = mx - x; i <= mx - x + 1 && i <= m; i++) {
vector<int> surp(n - 1);
for (int j = 0; j < n - 1; j++) {
surp[j] = x + i - cnt[j + 1];
surp[j] /= 2;
}
// number of reversal
for (int j = maxpos; j < n && j < maxpos + 1; j++) { // where to cut
vector<pi> intv;
for (int k = 0; k < m; k++) {
if (a[k] <= j && b[k] > j) {
intv.emplace_back(a[k] - 1, n - b[k]);
}
}
vector<int> l = {0};
vector<int> r = {0};
for (int k = 0; k < j - 1; k++)
l.push_back(surp[k]);
for (int k = n - 2; k >= j; k--)
r.push_back(surp[k]);
if (Do(l, r, intv, i))
return true;
}
}
return false;
}
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> a[i] >> b[i] >> c[i];
if (a[i] > b[i])
swap(a[i], b[i]);
cnt[a[i]]++;
cnt[b[i]]--;
}
for (int i = 1; i <= n; i++)
cnt[i] += cnt[i - 1];
int s = 0, e = 10000;
while (s != e) {
int m = (s + e) / 2;
if (trial(m))
e = m;
else
s = m + 1;
}
cout << s << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
300 KB |
Output is correct |
6 |
Correct |
1 ms |
304 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
300 KB |
Output is correct |
6 |
Correct |
1 ms |
304 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
304 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
300 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
304 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
304 KB |
Output is correct |
26 |
Correct |
1 ms |
304 KB |
Output is correct |
27 |
Correct |
1 ms |
300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
300 KB |
Output is correct |
6 |
Correct |
1 ms |
304 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
304 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
300 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
304 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
304 KB |
Output is correct |
26 |
Correct |
1 ms |
304 KB |
Output is correct |
27 |
Correct |
1 ms |
300 KB |
Output is correct |
28 |
Correct |
5 ms |
504 KB |
Output is correct |
29 |
Correct |
5 ms |
444 KB |
Output is correct |
30 |
Correct |
6 ms |
468 KB |
Output is correct |
31 |
Correct |
6 ms |
468 KB |
Output is correct |
32 |
Correct |
6 ms |
468 KB |
Output is correct |
33 |
Correct |
6 ms |
444 KB |
Output is correct |
34 |
Incorrect |
6 ms |
468 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
300 KB |
Output is correct |
6 |
Correct |
1 ms |
304 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
304 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
300 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
304 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
304 KB |
Output is correct |
26 |
Correct |
1 ms |
304 KB |
Output is correct |
27 |
Correct |
1 ms |
300 KB |
Output is correct |
28 |
Correct |
5 ms |
504 KB |
Output is correct |
29 |
Correct |
5 ms |
444 KB |
Output is correct |
30 |
Correct |
6 ms |
468 KB |
Output is correct |
31 |
Correct |
6 ms |
468 KB |
Output is correct |
32 |
Correct |
6 ms |
468 KB |
Output is correct |
33 |
Correct |
6 ms |
444 KB |
Output is correct |
34 |
Incorrect |
6 ms |
468 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
300 KB |
Output is correct |
6 |
Correct |
1 ms |
304 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
304 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
300 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
304 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
304 KB |
Output is correct |
26 |
Correct |
1 ms |
304 KB |
Output is correct |
27 |
Correct |
1 ms |
300 KB |
Output is correct |
28 |
Correct |
5 ms |
504 KB |
Output is correct |
29 |
Correct |
5 ms |
444 KB |
Output is correct |
30 |
Correct |
6 ms |
468 KB |
Output is correct |
31 |
Correct |
6 ms |
468 KB |
Output is correct |
32 |
Correct |
6 ms |
468 KB |
Output is correct |
33 |
Correct |
6 ms |
444 KB |
Output is correct |
34 |
Incorrect |
6 ms |
468 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |