#include <bits/stdc++.h>
#pragma optimize("O3, unroll-loops")
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define pb push_back
#define FIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
const int N = 307, inf = 2e9;
int n, m, k;
int a[N], b[N];
int mem[2*N][N];
pii gr[2*N][N];
vector <int> add[2*N], rem[2*N];
struct MQ {
queue <ll> q; deque <ll> dq;
inline void push(ll x) {q.push(x); while (!dq.empty() && dq.back() < x) dq.pop_back(); dq.pb(x);}
inline void pop() {ll x = q.front(); q.pop(); if (dq[0] == x) dq.pop_front();}
inline ll get() {return dq[0];}
};
int main () {
FIO;
cin >> n >> m >> k;
vector <pii> so;
for (int i = 0; i < k; i++) {
cin >> a[i] >> b[i];
a[i]--; b[i]--;
so.pb({a[i], b[i]});
}
sort(all(so));
for (int i = 0; i < k; i++) a[i] = so[i].F, b[i] = so[i].S;
int d = a[0]; for (int i = 0; i < k; i++) a[i] -= d;
d = n-a[k-1]-1; for (int i = 1; i < k; i++) d = max(d, a[i]-a[i-1]-1);
a[k] = n-1;
vector <pll> v;
for (int i = 0; i <= k; i++) {
for (int j = i; j <= k; j++) {
if (a[j]-a[i] >= d) v.pb({a[j]-a[i], 0});
if (a[j]-a[i]-1 >= d) v.pb({a[j]-a[i]-1, 0});
if (a[j]+n-a[i]-1 >= d) v.pb({n-a[i]-1, a[j]});
if (a[i]+n-a[j]-1 >= d) v.pb({n-a[j]-1, a[i]});
}
if (n-a[i]-1 >= d) v.pb({n-a[i]-1, 0});
}
sort(all(v)); v.erase(unique(all(v)), v.end());
//PRECOMPUTE -> O(N^2 log N)
for (int i = 0; i < k; i++) {
set <int> s;
multiset <int> rj;
rj.insert(0);
for (int j = i; j >= 0; j--) {
mem[i][j] = mem[i][j+1];
gr[i][j].F = gr[i][j+1].F;
gr[i][j].S = gr[i][j+1].S;
if (s.find(b[j]) != s.end()) continue;
auto p1 = s.lower_bound(b[j]);
if (p1 == s.end()) {
if (!s.empty()) rj.insert(b[j]-*(--p1)-1);
s.insert(b[j]);
gr[i][j] = {*s.begin(), m-*s.rbegin()-1};
}
else if (p1 == s.begin()) {
rj.insert(*p1-b[j]-1);
s.insert(b[j]);
gr[i][j] = {*s.begin(), m-*s.rbegin()-1};
}
else {
int x = *p1; --p1; int y = *p1;
rj.erase(rj.find(x-y-1));
rj.insert(x-b[j]-1);
rj.insert(b[j]-y-1);
s.insert(b[j]);
}
mem[i][j] = *rj.rbegin();
}
}
//SOLVE O(N^3)
ll mn = inf;
for (auto x : v) {
MQ m1, m2, m3;
vector <pair <pii, ll> > vv;
int z = 0;
for (int i = 0; i < k; i++) {
while (a[z]+x.F < a[i]) {
if (vv.back().S != (ll)a[z]+x.F+1) vv.pb({{0, 1}, (ll)a[z]+x.F+1});
else vv[vv.size()-1].F.S++;
z++;
}
if (vv.empty() || vv.back().S != a[i]) vv.pb({{1, 0}, a[i]-x.S});
else vv[vv.size()-1].F.F++;
}
while (z < k) {
if (vv.back().S != (ll)a[z]+x.F+1) vv.pb({{0, 1}, (ll)a[z]+x.F+1});
else vv[vv.size()-1].F.S++;
z++;
}
int l = 0, r = -1; z = 0;
#define C (x.F+x.S+max(m1.get(), m2.get()+m3.get()))
for (int i = 0; i+1 < vv.size(); i++) {
l += vv[i].F.S;
r += vv[i].F.F;
m1.push(mem[r][l]); m2.push(gr[r][l].F); m3.push(gr[r][l].S);
while (vv[z].S+n-1 < vv[i+1].S) {
mn = min(mn, C);
m1.pop(); m2.pop(); m3.pop();
z++;
}
}
}
cout << mn << "\n";
return 0;
}
Compilation message
cultivation.cpp:3: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
3 | #pragma optimize("O3, unroll-loops")
|
cultivation.cpp: In function 'int main()':
cultivation.cpp:117:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
117 | for (int i = 0; i+1 < vv.size(); i++) {
| ~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
488 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
488 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
3 ms |
1244 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
3 ms |
860 KB |
Output is correct |
22 |
Correct |
8 ms |
1344 KB |
Output is correct |
23 |
Correct |
1 ms |
600 KB |
Output is correct |
24 |
Correct |
14 ms |
2544 KB |
Output is correct |
25 |
Correct |
13 ms |
1748 KB |
Output is correct |
26 |
Correct |
27 ms |
4512 KB |
Output is correct |
27 |
Correct |
32 ms |
4564 KB |
Output is correct |
28 |
Correct |
15 ms |
2512 KB |
Output is correct |
29 |
Correct |
23 ms |
4564 KB |
Output is correct |
30 |
Correct |
24 ms |
4564 KB |
Output is correct |
31 |
Correct |
25 ms |
4560 KB |
Output is correct |
32 |
Correct |
26 ms |
4564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
488 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
3 ms |
1244 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
3 ms |
860 KB |
Output is correct |
22 |
Correct |
8 ms |
1344 KB |
Output is correct |
23 |
Correct |
1 ms |
600 KB |
Output is correct |
24 |
Correct |
14 ms |
2544 KB |
Output is correct |
25 |
Correct |
13 ms |
1748 KB |
Output is correct |
26 |
Correct |
27 ms |
4512 KB |
Output is correct |
27 |
Correct |
32 ms |
4564 KB |
Output is correct |
28 |
Correct |
15 ms |
2512 KB |
Output is correct |
29 |
Correct |
23 ms |
4564 KB |
Output is correct |
30 |
Correct |
24 ms |
4564 KB |
Output is correct |
31 |
Correct |
25 ms |
4560 KB |
Output is correct |
32 |
Correct |
26 ms |
4564 KB |
Output is correct |
33 |
Correct |
15 ms |
4564 KB |
Output is correct |
34 |
Correct |
30 ms |
4560 KB |
Output is correct |
35 |
Correct |
33 ms |
4564 KB |
Output is correct |
36 |
Correct |
34 ms |
4564 KB |
Output is correct |
37 |
Correct |
31 ms |
4564 KB |
Output is correct |
38 |
Correct |
35 ms |
4564 KB |
Output is correct |
39 |
Correct |
41 ms |
4564 KB |
Output is correct |
40 |
Correct |
35 ms |
4564 KB |
Output is correct |
41 |
Incorrect |
19 ms |
3540 KB |
Output isn't correct |
42 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
488 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
3 ms |
1244 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
3 ms |
860 KB |
Output is correct |
22 |
Correct |
8 ms |
1344 KB |
Output is correct |
23 |
Correct |
1 ms |
600 KB |
Output is correct |
24 |
Correct |
14 ms |
2544 KB |
Output is correct |
25 |
Correct |
13 ms |
1748 KB |
Output is correct |
26 |
Correct |
27 ms |
4512 KB |
Output is correct |
27 |
Correct |
32 ms |
4564 KB |
Output is correct |
28 |
Correct |
15 ms |
2512 KB |
Output is correct |
29 |
Correct |
23 ms |
4564 KB |
Output is correct |
30 |
Correct |
24 ms |
4564 KB |
Output is correct |
31 |
Correct |
25 ms |
4560 KB |
Output is correct |
32 |
Correct |
26 ms |
4564 KB |
Output is correct |
33 |
Correct |
15 ms |
4564 KB |
Output is correct |
34 |
Correct |
30 ms |
4560 KB |
Output is correct |
35 |
Correct |
33 ms |
4564 KB |
Output is correct |
36 |
Correct |
34 ms |
4564 KB |
Output is correct |
37 |
Correct |
31 ms |
4564 KB |
Output is correct |
38 |
Correct |
35 ms |
4564 KB |
Output is correct |
39 |
Correct |
41 ms |
4564 KB |
Output is correct |
40 |
Correct |
35 ms |
4564 KB |
Output is correct |
41 |
Incorrect |
19 ms |
3540 KB |
Output isn't correct |
42 |
Halted |
0 ms |
0 KB |
- |