#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];
map<pair<ll,ll>,ll>mp;
ll R, C, n;
ll solveab(ll a, ll b) {
if(mp[{a, b}]) return INF;
mp[{a, b}]=1;
set<pair<ll,ll>>A;
multiset<ll>B;
vector<pair<ll,ll>>V;
vector<ll>odw(n);
rep(i, n) V.pb({max(1ll, T[i].st-a), i});
rep(i, n) V.pb({min(R+1, T[i].st+b+1), i});
sort(all(V));
ll c=0, d=0, cpd=0, lst=1;
for(auto i : V) {
if(i.st!=lst) {
if(A.size()) {
auto it=A.begin();
auto p=*it;
c=max(c, p.st-1);
it=A.end(); --it;
p=*it;
d=max(d, C-p.st);
}
if(B.size()) {
auto it=B.end(); --it;
ll p=*it;
cpd=max(cpd, p);
}
lst=i.st;
}
if(!odw[i.nd]) {
odw[i.nd]=1;
if(A.size()) {
ll lewo=0, prawo=C+1;
auto it=A.lower_bound({T[i.nd].nd, i.nd});
if(it!=A.end()) {
auto p=*it;
prawo=p.st;
}
if(it!=A.begin()) {
--it;
auto p=*it;
lewo=p.st;
}
if(lewo!=0 && prawo!=C+1) B.erase(B.find(prawo-lewo-1));
if(lewo!=0) B.insert(T[i.nd].nd-lewo-1);
if(prawo!=C+1) B.insert(prawo-T[i.nd].nd-1);
}
A.insert({T[i.nd].nd, i.nd});
} else {
A.erase({T[i.nd].nd, i.nd});
if(A.size()) {
ll lewo=0, prawo=C+1;
auto it=A.lower_bound({T[i.nd].nd, i.nd});
if(it!=A.end()) {
auto p=*it;
prawo=p.st;
}
if(it!=A.begin()) {
--it;
auto p=*it;
lewo=p.st;
}
if(lewo!=0 && prawo!=C+1) B.insert(prawo-lewo-1);
if(lewo!=0) B.erase(B.find(T[i.nd].nd-lewo-1));
if(prawo!=C+1) B.erase(B.find(prawo-T[i.nd].nd-1));
}
}
}
return a+b+max(c+d, cpd);
}
ll solvea(ll a) {
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;
rep(i, n) {
ll x=R-T[i].st;
if(x>=b && a+x>=apb) ans=min(ans, solveab(a, x));
}
rep(i, n) rep(j, i) {
ll x=abs(T[i].st-T[j].st)-1;
if(x==-1 || x<apb) continue;
ll y=x-a;
if(y>=b) ans=min(ans, solveab(a, y));
}
return ans;
}
ll solve() {
mp.clear();
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) {
if(i%2==0) ans=min(ans, solve());
rep(j, n) T[j]={C-T[j].nd+1, T[j].st};
swap(R, C);
}
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 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 |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 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 |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
3 ms |
348 KB |
Output is correct |
18 |
Correct |
40 ms |
484 KB |
Output is correct |
19 |
Correct |
9 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
41 ms |
504 KB |
Output is correct |
22 |
Correct |
203 ms |
500 KB |
Output is correct |
23 |
Correct |
3 ms |
348 KB |
Output is correct |
24 |
Correct |
453 ms |
644 KB |
Output is correct |
25 |
Correct |
289 ms |
348 KB |
Output is correct |
26 |
Correct |
879 ms |
524 KB |
Output is correct |
27 |
Correct |
956 ms |
528 KB |
Output is correct |
28 |
Correct |
427 ms |
344 KB |
Output is correct |
29 |
Correct |
887 ms |
592 KB |
Output is correct |
30 |
Correct |
938 ms |
1032 KB |
Output is correct |
31 |
Correct |
918 ms |
596 KB |
Output is correct |
32 |
Correct |
901 ms |
528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 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 |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
3 ms |
348 KB |
Output is correct |
18 |
Correct |
40 ms |
484 KB |
Output is correct |
19 |
Correct |
9 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
41 ms |
504 KB |
Output is correct |
22 |
Correct |
203 ms |
500 KB |
Output is correct |
23 |
Correct |
3 ms |
348 KB |
Output is correct |
24 |
Correct |
453 ms |
644 KB |
Output is correct |
25 |
Correct |
289 ms |
348 KB |
Output is correct |
26 |
Correct |
879 ms |
524 KB |
Output is correct |
27 |
Correct |
956 ms |
528 KB |
Output is correct |
28 |
Correct |
427 ms |
344 KB |
Output is correct |
29 |
Correct |
887 ms |
592 KB |
Output is correct |
30 |
Correct |
938 ms |
1032 KB |
Output is correct |
31 |
Correct |
918 ms |
596 KB |
Output is correct |
32 |
Correct |
901 ms |
528 KB |
Output is correct |
33 |
Correct |
64 ms |
348 KB |
Output is correct |
34 |
Correct |
826 ms |
596 KB |
Output is correct |
35 |
Correct |
999 ms |
848 KB |
Output is correct |
36 |
Correct |
1022 ms |
852 KB |
Output is correct |
37 |
Correct |
845 ms |
592 KB |
Output is correct |
38 |
Correct |
965 ms |
848 KB |
Output is correct |
39 |
Correct |
1010 ms |
524 KB |
Output is correct |
40 |
Correct |
961 ms |
824 KB |
Output is correct |
41 |
Correct |
443 ms |
348 KB |
Output is correct |
42 |
Correct |
790 ms |
532 KB |
Output is correct |
43 |
Correct |
994 ms |
592 KB |
Output is correct |
44 |
Correct |
972 ms |
592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
31 ms |
604 KB |
Output is correct |
3 |
Correct |
34 ms |
620 KB |
Output is correct |
4 |
Correct |
25 ms |
600 KB |
Output is correct |
5 |
Correct |
30 ms |
608 KB |
Output is correct |
6 |
Correct |
21 ms |
344 KB |
Output is correct |
7 |
Correct |
3 ms |
344 KB |
Output is correct |
8 |
Correct |
15 ms |
348 KB |
Output is correct |
9 |
Correct |
20 ms |
596 KB |
Output is correct |
10 |
Correct |
31 ms |
588 KB |
Output is correct |
11 |
Correct |
31 ms |
604 KB |
Output is correct |
12 |
Correct |
31 ms |
588 KB |
Output is correct |
13 |
Correct |
7 ms |
348 KB |
Output is correct |
14 |
Correct |
16 ms |
600 KB |
Output is correct |
15 |
Correct |
6 ms |
348 KB |
Output is correct |
16 |
Correct |
38 ms |
604 KB |
Output is correct |
17 |
Correct |
34 ms |
640 KB |
Output is correct |
18 |
Correct |
7 ms |
348 KB |
Output is correct |
19 |
Correct |
33 ms |
508 KB |
Output is correct |
20 |
Correct |
15 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
31 ms |
604 KB |
Output is correct |
3 |
Correct |
34 ms |
620 KB |
Output is correct |
4 |
Correct |
25 ms |
600 KB |
Output is correct |
5 |
Correct |
30 ms |
608 KB |
Output is correct |
6 |
Correct |
21 ms |
344 KB |
Output is correct |
7 |
Correct |
3 ms |
344 KB |
Output is correct |
8 |
Correct |
15 ms |
348 KB |
Output is correct |
9 |
Correct |
20 ms |
596 KB |
Output is correct |
10 |
Correct |
31 ms |
588 KB |
Output is correct |
11 |
Correct |
31 ms |
604 KB |
Output is correct |
12 |
Correct |
31 ms |
588 KB |
Output is correct |
13 |
Correct |
7 ms |
348 KB |
Output is correct |
14 |
Correct |
16 ms |
600 KB |
Output is correct |
15 |
Correct |
6 ms |
348 KB |
Output is correct |
16 |
Correct |
38 ms |
604 KB |
Output is correct |
17 |
Correct |
34 ms |
640 KB |
Output is correct |
18 |
Correct |
7 ms |
348 KB |
Output is correct |
19 |
Correct |
33 ms |
508 KB |
Output is correct |
20 |
Correct |
15 ms |
348 KB |
Output is correct |
21 |
Execution timed out |
2059 ms |
3280 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 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 |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
3 ms |
348 KB |
Output is correct |
18 |
Correct |
40 ms |
484 KB |
Output is correct |
19 |
Correct |
9 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
41 ms |
504 KB |
Output is correct |
22 |
Correct |
203 ms |
500 KB |
Output is correct |
23 |
Correct |
3 ms |
348 KB |
Output is correct |
24 |
Correct |
453 ms |
644 KB |
Output is correct |
25 |
Correct |
289 ms |
348 KB |
Output is correct |
26 |
Correct |
879 ms |
524 KB |
Output is correct |
27 |
Correct |
956 ms |
528 KB |
Output is correct |
28 |
Correct |
427 ms |
344 KB |
Output is correct |
29 |
Correct |
887 ms |
592 KB |
Output is correct |
30 |
Correct |
938 ms |
1032 KB |
Output is correct |
31 |
Correct |
918 ms |
596 KB |
Output is correct |
32 |
Correct |
901 ms |
528 KB |
Output is correct |
33 |
Correct |
64 ms |
348 KB |
Output is correct |
34 |
Correct |
826 ms |
596 KB |
Output is correct |
35 |
Correct |
999 ms |
848 KB |
Output is correct |
36 |
Correct |
1022 ms |
852 KB |
Output is correct |
37 |
Correct |
845 ms |
592 KB |
Output is correct |
38 |
Correct |
965 ms |
848 KB |
Output is correct |
39 |
Correct |
1010 ms |
524 KB |
Output is correct |
40 |
Correct |
961 ms |
824 KB |
Output is correct |
41 |
Correct |
443 ms |
348 KB |
Output is correct |
42 |
Correct |
790 ms |
532 KB |
Output is correct |
43 |
Correct |
994 ms |
592 KB |
Output is correct |
44 |
Correct |
972 ms |
592 KB |
Output is correct |
45 |
Correct |
7 ms |
344 KB |
Output is correct |
46 |
Correct |
31 ms |
604 KB |
Output is correct |
47 |
Correct |
34 ms |
620 KB |
Output is correct |
48 |
Correct |
25 ms |
600 KB |
Output is correct |
49 |
Correct |
30 ms |
608 KB |
Output is correct |
50 |
Correct |
21 ms |
344 KB |
Output is correct |
51 |
Correct |
3 ms |
344 KB |
Output is correct |
52 |
Correct |
15 ms |
348 KB |
Output is correct |
53 |
Correct |
20 ms |
596 KB |
Output is correct |
54 |
Correct |
31 ms |
588 KB |
Output is correct |
55 |
Correct |
31 ms |
604 KB |
Output is correct |
56 |
Correct |
31 ms |
588 KB |
Output is correct |
57 |
Correct |
7 ms |
348 KB |
Output is correct |
58 |
Correct |
16 ms |
600 KB |
Output is correct |
59 |
Correct |
6 ms |
348 KB |
Output is correct |
60 |
Correct |
38 ms |
604 KB |
Output is correct |
61 |
Correct |
34 ms |
640 KB |
Output is correct |
62 |
Correct |
7 ms |
348 KB |
Output is correct |
63 |
Correct |
33 ms |
508 KB |
Output is correct |
64 |
Correct |
15 ms |
348 KB |
Output is correct |
65 |
Execution timed out |
2059 ms |
3280 KB |
Time limit exceeded |
66 |
Halted |
0 ms |
0 KB |
- |