#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define int long long
using namespace std;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int r, c, n;
cin >> r >> c >> n;
vector<array<int, 2>> a(n);
for(int i = 0; i < n; ++i){
cin >> a[i][0] >> a[i][1];
--a[i][0];
--a[i][1];
}
vector<int> dpu;
sort(a.begin(), a.end());
for(int i = 0; i < n; ++i){
dpu.push_back(a[i][0] + 1);
dpu.push_back(r - a[i][0]);
for(int j = i + 1; j < n; ++j){
dpu.push_back(a[j][0] - a[i][0] + 1);
}
for(int j = 0; j < n; ++j){
dpu.push_back(a[j][0] + r - a[i][0]);
}
}
sort(dpu.begin(), dpu.end());
dpu.erase(unique(dpu.begin(), dpu.end()), dpu.end());
auto sol = [&](int dulen)->int{
int rv = (int)1e18;
auto insol = [&](int stpos){
int lmx = 0, rmx = 0, lenmx = 0;
for(int i = stpos; i < stpos + r; ++i){
vector<int> x;
for(int j = 0; j < n; ++j){
if(a[j][0] > i - dulen && a[j][0] <= i){
x.push_back(a[j][1]);
}
}
sort(x.begin(), x.end());
if(!(int)x.size()){
lenmx = (int)1e9;
}
else{
lmx = max(lmx, x[0]);
rmx = max(rmx, c - x.back() - 1);
for(int j = 0; j + 1 < (int)x.size(); ++j){
lenmx = max(lenmx, x[j + 1] - x[j]);
}
}
}
rv = min(rv, max(lenmx - 1, lmx + rmx));
};
// for(int i = 0; i < r; ++i){
// insol(i);
// }
for(int i = 0; i < n; ++i){
insol(a[i][0]);
}
return rv;
};
int ans = (int)1e18;
for(auto&dulen:dpu){
// cout << dulen << endl;
ans = min(ans, dulen - 1 + sol(dulen));
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 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 |
0 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 |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 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 |
0 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 |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
2 ms |
212 KB |
Output is correct |
18 |
Correct |
40 ms |
340 KB |
Output is correct |
19 |
Correct |
16 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
58 ms |
356 KB |
Output is correct |
22 |
Correct |
386 ms |
596 KB |
Output is correct |
23 |
Correct |
13 ms |
344 KB |
Output is correct |
24 |
Correct |
1495 ms |
980 KB |
Output is correct |
25 |
Correct |
700 ms |
852 KB |
Output is correct |
26 |
Execution timed out |
2067 ms |
2512 KB |
Time limit exceeded |
27 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 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 |
0 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 |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
2 ms |
212 KB |
Output is correct |
18 |
Correct |
40 ms |
340 KB |
Output is correct |
19 |
Correct |
16 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
58 ms |
356 KB |
Output is correct |
22 |
Correct |
386 ms |
596 KB |
Output is correct |
23 |
Correct |
13 ms |
344 KB |
Output is correct |
24 |
Correct |
1495 ms |
980 KB |
Output is correct |
25 |
Correct |
700 ms |
852 KB |
Output is correct |
26 |
Execution timed out |
2067 ms |
2512 KB |
Time limit exceeded |
27 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2053 ms |
212 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2053 ms |
212 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 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 |
0 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 |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
2 ms |
212 KB |
Output is correct |
18 |
Correct |
40 ms |
340 KB |
Output is correct |
19 |
Correct |
16 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
58 ms |
356 KB |
Output is correct |
22 |
Correct |
386 ms |
596 KB |
Output is correct |
23 |
Correct |
13 ms |
344 KB |
Output is correct |
24 |
Correct |
1495 ms |
980 KB |
Output is correct |
25 |
Correct |
700 ms |
852 KB |
Output is correct |
26 |
Execution timed out |
2067 ms |
2512 KB |
Time limit exceeded |
27 |
Halted |
0 ms |
0 KB |
- |