#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const ll INF=2e9+7;
const int LIM=307;
pair<ll,ll>T[LIM];
vector<ll>ord;
ll tmi[LIM][LIM], tma[LIM][LIM], R, C, n, akt1, akt2, akt3;
vector<pair<ll,ll>>Ci, Di, Ei;
ll solveab(ll a, ll b) {
ll c=0, d=0, cpd=0;
while(akt1>0 && Ci[akt1-1].st<=b) --akt1;
c=Ci[akt1].nd;
while(akt2>0 && Di[akt2-1].st<=b) --akt2;
d=Di[akt2].nd;
while(akt3<Ei.size() && Ei[akt3].st<b) ++akt3;
if(Ei.size()>0 && Ei[akt3].st>=b) cpd=max(Ei[akt3].nd, 0ll);
return a+b+max(c+d, cpd);
}
ll solvea(ll a) {
Ci.clear(); Di.clear(); Ei.clear();
vector<pair<ll,ll>>P;
rep(i, n) P.pb({T[i].nd, i});
sort(all(P));
vector<pair<ll,ll>>Q;
rep(i, n) {
Q.pb({max(1ll, T[P[i].nd].st-a), T[P[i].nd].st});
for(int j=(int)Q.size()-1; j>0; --j) if(Q[j]<Q[j-1]) swap(Q[j], Q[j-1]);
ll ma=1, mi=0;
for(auto j : Q) {
mi=max(mi, j.st-ma-1);
ma=max(ma, j.nd);
}
mi=max(mi, R-ma);
if(Q[0].st!=1) mi=INF;
Ci.pb({mi, P[i].st-1});
}
Q.clear();
rep(i, n) {
Q.pb({max(1ll, T[P[n-i-1].nd].st-a), T[P[n-i-1].nd].st});
for(int j=(int)Q.size()-1; j>0; --j) if(Q[j]<Q[j-1]) swap(Q[j], Q[j-1]);
ll ma=1, mi=0;
for(auto j : Q) {
mi=max(mi, j.st-ma-1);
ma=max(ma, j.nd);
}
mi=max(mi, R-ma);
if(Q[0].st!=1) mi=INF;
Di.pb({mi, C-P[n-i-1].st});
}
ll einf=-1;
rep(xd, 2) {
rep(i, n) rep(j, n) {
tmi[i][j]=R+1;
tma[i][j]=0;
}
rep(i, n) {
for(int j=i+1; j<n; ++j) {
tmi[i][j]=tmi[i][j-1];
if(T[P[j].nd].st>=T[P[i].nd].st) tmi[i][j]=min(tmi[i][j], T[P[j].nd].st-a);
}
}
rep(i, n) {
for(int j=i-1; j>=0; --j) {
tma[j][i]=tma[j+1][i];
if(T[P[j].nd].st<=T[P[i].nd].st) tma[j][i]=max(tma[j][i], T[P[j].nd].st);
}
}
rep(j, n) rep(i, j) {
if(T[P[i].nd].st<=T[P[j].nd].st) {
if(tmi[i][j-1]<=0) continue;
else if(tmi[i][j-1]==10*INF || tma[i+1][j]==0) einf=max(einf, abs(P[i].st-P[j].st)-1);
//Ei.pb({10*INF, abs(P[i].st-P[j].st)-1});
else if(tmi[i][j-1]-tma[i+1][j]-2>=0) Ei.pb({tmi[i][j-1]-tma[i+1][j]-2, abs(P[i].st-P[j].st)-1});
}
}
reverse(all(P));
}
Ei.pb({10*INF, einf});
sort(all(Ei));
for(int i=(int)Ei.size()-2; i>=0; --i) Ei[i].nd=max(Ei[i].nd, Ei[i+1].nd);
vector<ll>A;
rep(i, n) A.pb(T[i].st);
sort(all(A));
ll b=R-A[n-1], apb=0;
rep(i, n-1) apb=max(apb, A[i+1]-A[i]-1);
ll ans=INF;
vector<ll>kol;
rep(i, n) {
ll x=R-T[i].st;
if(x>=b && a+x>=apb) kol.pb(x);
}
sort(all(kol));
akt1=Ci.size();
akt2=Di.size();
akt3=0;
ll lst=-INF;
for(auto i : kol) if(i!=lst) {
ans=min(ans, solveab(a, i));
lst=i;
}
kol.clear();
for(auto x : ord) {
if(x==-1 || x<apb) continue;
ll y=x-a;
if(y>=b) kol.pb(y);
}
sort(all(kol));
akt1=Ci.size();
akt2=Di.size();
akt3=0;
lst=-INF;
for(auto i : kol) if(i!=lst) {
ans=min(ans, solveab(a, i));
lst=i;
}
return ans;
}
ll solve() {
ord.clear();
rep(i, n) rep(j, i) ord.pb(abs(T[i].st-T[j].st)-1);
sort(all(ord));
ll ans=INF;
rep(i, n) ans=min(ans, solvea(T[i].st-1));
return ans;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> R >> C >> n;
rep(i, n) cin >> T[i].st >> T[i].nd;
ll ans=INF;
rep(i, 4) {
ans=min(ans, solve());
rep(j, n) T[j]={C-T[j].nd+1, T[j].st};
swap(R, C);
}
cout << ans << '\n';
}
Compilation message
cultivation.cpp: In function 'll solveab(ll, ll)':
cultivation.cpp:22:13: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | while(akt3<Ei.size() && Ei[akt3].st<b) ++akt3;
| ~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 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 |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 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 |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
17 ms |
852 KB |
Output is correct |
19 |
Correct |
2 ms |
600 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
10 ms |
860 KB |
Output is correct |
22 |
Correct |
102 ms |
1116 KB |
Output is correct |
23 |
Correct |
3 ms |
600 KB |
Output is correct |
24 |
Correct |
450 ms |
2012 KB |
Output is correct |
25 |
Correct |
206 ms |
1548 KB |
Output is correct |
26 |
Correct |
1506 ms |
3216 KB |
Output is correct |
27 |
Correct |
1588 ms |
3440 KB |
Output is correct |
28 |
Correct |
482 ms |
1940 KB |
Output is correct |
29 |
Correct |
1634 ms |
3208 KB |
Output is correct |
30 |
Correct |
1567 ms |
3184 KB |
Output is correct |
31 |
Correct |
1603 ms |
3260 KB |
Output is correct |
32 |
Correct |
1592 ms |
3176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 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 |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
17 ms |
852 KB |
Output is correct |
19 |
Correct |
2 ms |
600 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
10 ms |
860 KB |
Output is correct |
22 |
Correct |
102 ms |
1116 KB |
Output is correct |
23 |
Correct |
3 ms |
600 KB |
Output is correct |
24 |
Correct |
450 ms |
2012 KB |
Output is correct |
25 |
Correct |
206 ms |
1548 KB |
Output is correct |
26 |
Correct |
1506 ms |
3216 KB |
Output is correct |
27 |
Correct |
1588 ms |
3440 KB |
Output is correct |
28 |
Correct |
482 ms |
1940 KB |
Output is correct |
29 |
Correct |
1634 ms |
3208 KB |
Output is correct |
30 |
Correct |
1567 ms |
3184 KB |
Output is correct |
31 |
Correct |
1603 ms |
3260 KB |
Output is correct |
32 |
Correct |
1592 ms |
3176 KB |
Output is correct |
33 |
Correct |
1511 ms |
3176 KB |
Output is correct |
34 |
Correct |
1345 ms |
3124 KB |
Output is correct |
35 |
Correct |
1701 ms |
3308 KB |
Output is correct |
36 |
Correct |
1708 ms |
3372 KB |
Output is correct |
37 |
Correct |
1620 ms |
3240 KB |
Output is correct |
38 |
Correct |
1659 ms |
3208 KB |
Output is correct |
39 |
Correct |
1713 ms |
3232 KB |
Output is correct |
40 |
Correct |
1686 ms |
3216 KB |
Output is correct |
41 |
Correct |
1360 ms |
2780 KB |
Output is correct |
42 |
Correct |
1514 ms |
3088 KB |
Output is correct |
43 |
Correct |
1689 ms |
3324 KB |
Output is correct |
44 |
Correct |
1667 ms |
3188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
476 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
476 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
65 ms |
1116 KB |
Output is correct |
22 |
Correct |
64 ms |
1112 KB |
Output is correct |
23 |
Correct |
50 ms |
860 KB |
Output is correct |
24 |
Correct |
59 ms |
860 KB |
Output is correct |
25 |
Correct |
58 ms |
1116 KB |
Output is correct |
26 |
Correct |
65 ms |
1108 KB |
Output is correct |
27 |
Correct |
44 ms |
1116 KB |
Output is correct |
28 |
Correct |
65 ms |
1116 KB |
Output is correct |
29 |
Correct |
60 ms |
1116 KB |
Output is correct |
30 |
Correct |
60 ms |
1116 KB |
Output is correct |
31 |
Correct |
63 ms |
1112 KB |
Output is correct |
32 |
Correct |
61 ms |
1116 KB |
Output is correct |
33 |
Correct |
44 ms |
1112 KB |
Output is correct |
34 |
Correct |
51 ms |
1064 KB |
Output is correct |
35 |
Correct |
48 ms |
1116 KB |
Output is correct |
36 |
Correct |
55 ms |
1112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 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 |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
17 ms |
852 KB |
Output is correct |
19 |
Correct |
2 ms |
600 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
10 ms |
860 KB |
Output is correct |
22 |
Correct |
102 ms |
1116 KB |
Output is correct |
23 |
Correct |
3 ms |
600 KB |
Output is correct |
24 |
Correct |
450 ms |
2012 KB |
Output is correct |
25 |
Correct |
206 ms |
1548 KB |
Output is correct |
26 |
Correct |
1506 ms |
3216 KB |
Output is correct |
27 |
Correct |
1588 ms |
3440 KB |
Output is correct |
28 |
Correct |
482 ms |
1940 KB |
Output is correct |
29 |
Correct |
1634 ms |
3208 KB |
Output is correct |
30 |
Correct |
1567 ms |
3184 KB |
Output is correct |
31 |
Correct |
1603 ms |
3260 KB |
Output is correct |
32 |
Correct |
1592 ms |
3176 KB |
Output is correct |
33 |
Correct |
1511 ms |
3176 KB |
Output is correct |
34 |
Correct |
1345 ms |
3124 KB |
Output is correct |
35 |
Correct |
1701 ms |
3308 KB |
Output is correct |
36 |
Correct |
1708 ms |
3372 KB |
Output is correct |
37 |
Correct |
1620 ms |
3240 KB |
Output is correct |
38 |
Correct |
1659 ms |
3208 KB |
Output is correct |
39 |
Correct |
1713 ms |
3232 KB |
Output is correct |
40 |
Correct |
1686 ms |
3216 KB |
Output is correct |
41 |
Correct |
1360 ms |
2780 KB |
Output is correct |
42 |
Correct |
1514 ms |
3088 KB |
Output is correct |
43 |
Correct |
1689 ms |
3324 KB |
Output is correct |
44 |
Correct |
1667 ms |
3188 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
2 ms |
348 KB |
Output is correct |
47 |
Correct |
3 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
2 ms |
348 KB |
Output is correct |
50 |
Correct |
2 ms |
348 KB |
Output is correct |
51 |
Correct |
2 ms |
348 KB |
Output is correct |
52 |
Correct |
1 ms |
348 KB |
Output is correct |
53 |
Correct |
2 ms |
348 KB |
Output is correct |
54 |
Correct |
1 ms |
348 KB |
Output is correct |
55 |
Correct |
2 ms |
604 KB |
Output is correct |
56 |
Correct |
2 ms |
476 KB |
Output is correct |
57 |
Correct |
1 ms |
348 KB |
Output is correct |
58 |
Correct |
1 ms |
344 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
2 ms |
348 KB |
Output is correct |
61 |
Correct |
2 ms |
348 KB |
Output is correct |
62 |
Correct |
1 ms |
348 KB |
Output is correct |
63 |
Correct |
1 ms |
348 KB |
Output is correct |
64 |
Correct |
1 ms |
348 KB |
Output is correct |
65 |
Correct |
65 ms |
1116 KB |
Output is correct |
66 |
Correct |
64 ms |
1112 KB |
Output is correct |
67 |
Correct |
50 ms |
860 KB |
Output is correct |
68 |
Correct |
59 ms |
860 KB |
Output is correct |
69 |
Correct |
58 ms |
1116 KB |
Output is correct |
70 |
Correct |
65 ms |
1108 KB |
Output is correct |
71 |
Correct |
44 ms |
1116 KB |
Output is correct |
72 |
Correct |
65 ms |
1116 KB |
Output is correct |
73 |
Correct |
60 ms |
1116 KB |
Output is correct |
74 |
Correct |
60 ms |
1116 KB |
Output is correct |
75 |
Correct |
63 ms |
1112 KB |
Output is correct |
76 |
Correct |
61 ms |
1116 KB |
Output is correct |
77 |
Correct |
44 ms |
1112 KB |
Output is correct |
78 |
Correct |
51 ms |
1064 KB |
Output is correct |
79 |
Correct |
48 ms |
1116 KB |
Output is correct |
80 |
Correct |
55 ms |
1112 KB |
Output is correct |
81 |
Correct |
653 ms |
2184 KB |
Output is correct |
82 |
Correct |
688 ms |
2188 KB |
Output is correct |
83 |
Correct |
1012 ms |
2676 KB |
Output is correct |
84 |
Correct |
745 ms |
2596 KB |
Output is correct |
85 |
Correct |
1680 ms |
3204 KB |
Output is correct |
86 |
Correct |
1793 ms |
3248 KB |
Output is correct |
87 |
Correct |
1738 ms |
3364 KB |
Output is correct |
88 |
Correct |
1330 ms |
2792 KB |
Output is correct |
89 |
Correct |
1300 ms |
2268 KB |
Output is correct |
90 |
Correct |
933 ms |
2876 KB |
Output is correct |
91 |
Correct |
1696 ms |
3168 KB |
Output is correct |
92 |
Correct |
1754 ms |
3216 KB |
Output is correct |
93 |
Correct |
1364 ms |
2812 KB |
Output is correct |
94 |
Correct |
1118 ms |
3140 KB |
Output is correct |
95 |
Correct |
1661 ms |
3208 KB |
Output is correct |
96 |
Correct |
1589 ms |
2836 KB |
Output is correct |
97 |
Correct |
1214 ms |
3188 KB |
Output is correct |
98 |
Correct |
1237 ms |
3160 KB |
Output is correct |