This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
int S[300],E[300];
vi vv;
vector<pair<pii,int> > vv2;
int pos[300],occ[300];
int ll[300],rr[300];
int main() {
int i;
int R,C,N;
scanf("%d %d %d",&R,&C,&N);
for (i = 0; i < N; i++) scanf("%d %d",&S[i],&E[i]),S[i]--,E[i]--;
int j,k;
LLI ans = 2e9;
for (i = 0; i < N; i++) {
int u = S[i];
vv.pb(0),vv.pb(R-1);
for (j = 0; j < N; j++) vv.pb(max(S[j]-u-1,0));
sort(vv.begin(),vv.end());
vv.resize(unique(vv.begin(),vv.end())-vv.begin());
for (j = 0; j < vv.size(); j++) {
vpii v;
vi v2;
for (k = 0; k < N; k++) {
if ((vv[j] >= S[k]-u) && (vv[j] <= S[k])) v.pb(mp(0,E[k])),v2.pb(E[k]);
else if (vv[j] > S[k]) v.pb(mp(vv[j]-S[k],E[k])),v2.pb(E[k]);
}
sort(v.begin(),v.end()),sort(v2.begin(),v2.end());
v2.resize(unique(v2.begin(),v2.end())-v2.begin());
for (k = 0; k < v.size(); k++) {
pos[k] = lower_bound(v2.begin(),v2.end(),v[k].second)-v2.begin();
occ[pos[k]]++;
}
int L = 0,R = v2.size()-1;
int l = v2[0],r = C-v2.back()-1,lr = 0;
for (k = 0; k < v2.size(); k++) ll[k] = k-1,rr[k] = k+1;
for (k = 1; k < v2.size(); k++) lr = max(lr,v2[k]-v2[k-1]-1);
vv2.pb(mp(mp(2e9,l),0)),vv2.pb(mp(mp(2e9,r),1)),vv2.pb(mp(mp(2e9,lr),2));
for (k = v.size()-1; k >= 0; k--) {
occ[pos[k]]--;
if (k == 0) vv2.pb(mp(mp(v[k].first-1,2e9),0));
else {
if (occ[L] == 0) {
while (occ[L] == 0) L++;
l = v2[L];
vv2.pb(mp(mp(v[k].first-1,l),0));
}
if (occ[R] == 0) {
while (occ[R] == 0) R--;
r = C-v2[R]-1;
vv2.pb(mp(mp(v[k].first-1,r),1));
}
if (occ[pos[k]] == 0) {
if (ll[pos[k]] != -1) rr[ll[pos[k]]] = rr[pos[k]];
if (rr[pos[k]] != v2.size()) ll[rr[pos[k]]] = ll[pos[k]];
if ((ll[pos[k]] != -1) && (rr[pos[k]] != v2.size())) {
if (v2[rr[pos[k]]]-v2[ll[pos[k]]]-1 > lr) {
lr = v2[rr[pos[k]]]-v2[ll[pos[k]]]-1;
vv2.pb(mp(mp(v[k].first-1,lr),2));
}
}
}
}
}
}
int l = 0,r = 0,lr = 0;
sort(vv2.begin(),vv2.end());
for (j = vv2.size()-1; j >= 0; j--) {
ans = min(ans,(LLI) u+vv2[j].first.first+1+max((LLI) l+r,(LLI) lr));
if (vv2[j].second == 0) l = max(l,vv2[j].first.second);
else if (vv2[j].second == 1) r = max(r,vv2[j].first.second);
else lr = max(lr,vv2[j].first.second);
}
vv.clear(),vv2.clear();
}
printf("%lld\n",ans);
return 0;
}
Compilation message (stderr)
cultivation.cpp: In function 'int main()':
cultivation.cpp:77:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for (j = 0; j < vv.size(); j++) {
| ~~^~~~~~~~~~~
cultivation.cpp:86:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for (k = 0; k < v.size(); k++) {
| ~~^~~~~~~~~~
cultivation.cpp:92:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for (k = 0; k < v2.size(); k++) ll[k] = k-1,rr[k] = k+1;
| ~~^~~~~~~~~~~
cultivation.cpp:93:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for (k = 1; k < v2.size(); k++) lr = max(lr,v2[k]-v2[k-1]-1);
| ~~^~~~~~~~~~~
cultivation.cpp:111:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
111 | if (rr[pos[k]] != v2.size()) ll[rr[pos[k]]] = ll[pos[k]];
| ~~~~~~~~~~~^~~~~~~~~~~~
cultivation.cpp:112:63: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
112 | if ((ll[pos[k]] != -1) && (rr[pos[k]] != v2.size())) {
| ~~~~~~~~~~~^~~~~~~~~~~~
cultivation.cpp:66:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
66 | scanf("%d %d %d",&R,&C,&N);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
cultivation.cpp:67:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
67 | for (i = 0; i < N; i++) scanf("%d %d",&S[i],&E[i]),S[i]--,E[i]--;
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |