#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;
multiset<int> SS;
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;
for (k = 0; k < N; k++) {
if ((vv[j] >= S[k]-u) && (vv[j] <= S[k])) v.pb(mp(0,E[k]));
else if (vv[j] > S[k]) v.pb(mp(vv[j]-S[k],E[k]));
}
sort(v.begin(),v.end());
for (k = 0; k < v.size(); k++) SS.insert(v[k].second);
int l = *SS.begin(),r = C-*SS.rbegin()-1,lr = 0;
for (auto it = SS.begin(); it != SS.end(); it++) {
auto it2 = it;
it2++;
if (it2 != SS.end()) lr = max(lr,*it2-*it-1);
}
vv2.pb(mp(mp(R,l),0)),vv2.pb(mp(mp(R,r),1)),vv2.pb(mp(mp(R,lr),2));
for (k = v.size()-1; k >= 0; k--) {
auto it = SS.find(v[k].second);
it = SS.erase(it);
auto it2 = it;
if ((it2 != SS.begin()) && (it != SS.end())) lr = max(lr,*it-*(--it2)-1);
if (!SS.empty()) l = max(l,*SS.begin()),r = max(r,C-*SS.rbegin()-1);
else l = max(l,(int) 2e9);
vv2.pb(mp(mp(v[k].first-1,l),0)),vv2.pb(mp(mp(v[k].first-1,r),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--) {
if (vv2[j].first.first < R) ans = min(ans,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
cultivation.cpp: In function 'int main()':
cultivation.cpp:76:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for (j = 0; j < vv.size(); j++) {
| ~~^~~~~~~~~~~
cultivation.cpp:83: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]
83 | for (k = 0; k < v.size(); k++) SS.insert(v[k].second);
| ~~^~~~~~~~~~
cultivation.cpp:65:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
65 | scanf("%d %d %d",&R,&C,&N);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
cultivation.cpp:66:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
66 | for (i = 0; i < N; i++) scanf("%d %d",&S[i],&E[i]),S[i]--,E[i]--;
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
208 KB |
Output is correct |
7 |
Correct |
1 ms |
240 KB |
Output is correct |
8 |
Correct |
1 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
208 KB |
Output is correct |
7 |
Correct |
1 ms |
240 KB |
Output is correct |
8 |
Correct |
1 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
2 ms |
204 KB |
Output is correct |
18 |
Correct |
22 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
204 KB |
Output is correct |
20 |
Correct |
2 ms |
204 KB |
Output is correct |
21 |
Correct |
18 ms |
380 KB |
Output is correct |
22 |
Correct |
85 ms |
460 KB |
Output is correct |
23 |
Correct |
2 ms |
204 KB |
Output is correct |
24 |
Correct |
246 ms |
588 KB |
Output is correct |
25 |
Correct |
131 ms |
592 KB |
Output is correct |
26 |
Correct |
658 ms |
792 KB |
Output is correct |
27 |
Correct |
626 ms |
784 KB |
Output is correct |
28 |
Correct |
318 ms |
588 KB |
Output is correct |
29 |
Correct |
591 ms |
788 KB |
Output is correct |
30 |
Correct |
460 ms |
588 KB |
Output is correct |
31 |
Correct |
644 ms |
788 KB |
Output is correct |
32 |
Correct |
598 ms |
788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
208 KB |
Output is correct |
7 |
Correct |
1 ms |
240 KB |
Output is correct |
8 |
Correct |
1 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
2 ms |
204 KB |
Output is correct |
18 |
Correct |
22 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
204 KB |
Output is correct |
20 |
Correct |
2 ms |
204 KB |
Output is correct |
21 |
Correct |
18 ms |
380 KB |
Output is correct |
22 |
Correct |
85 ms |
460 KB |
Output is correct |
23 |
Correct |
2 ms |
204 KB |
Output is correct |
24 |
Correct |
246 ms |
588 KB |
Output is correct |
25 |
Correct |
131 ms |
592 KB |
Output is correct |
26 |
Correct |
658 ms |
792 KB |
Output is correct |
27 |
Correct |
626 ms |
784 KB |
Output is correct |
28 |
Correct |
318 ms |
588 KB |
Output is correct |
29 |
Correct |
591 ms |
788 KB |
Output is correct |
30 |
Correct |
460 ms |
588 KB |
Output is correct |
31 |
Correct |
644 ms |
788 KB |
Output is correct |
32 |
Correct |
598 ms |
788 KB |
Output is correct |
33 |
Correct |
60 ms |
340 KB |
Output is correct |
34 |
Correct |
632 ms |
788 KB |
Output is correct |
35 |
Correct |
599 ms |
792 KB |
Output is correct |
36 |
Correct |
551 ms |
788 KB |
Output is correct |
37 |
Correct |
541 ms |
588 KB |
Output is correct |
38 |
Correct |
641 ms |
788 KB |
Output is correct |
39 |
Correct |
615 ms |
804 KB |
Output is correct |
40 |
Correct |
653 ms |
792 KB |
Output is correct |
41 |
Correct |
299 ms |
588 KB |
Output is correct |
42 |
Correct |
427 ms |
588 KB |
Output is correct |
43 |
Correct |
536 ms |
792 KB |
Output is correct |
44 |
Correct |
654 ms |
788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
204 KB |
Output is correct |
2 |
Correct |
3 ms |
204 KB |
Output is correct |
3 |
Correct |
3 ms |
332 KB |
Output is correct |
4 |
Correct |
3 ms |
332 KB |
Output is correct |
5 |
Correct |
3 ms |
332 KB |
Output is correct |
6 |
Correct |
4 ms |
204 KB |
Output is correct |
7 |
Correct |
2 ms |
204 KB |
Output is correct |
8 |
Correct |
3 ms |
292 KB |
Output is correct |
9 |
Correct |
3 ms |
204 KB |
Output is correct |
10 |
Correct |
3 ms |
296 KB |
Output is correct |
11 |
Correct |
3 ms |
332 KB |
Output is correct |
12 |
Correct |
3 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
296 KB |
Output is correct |
14 |
Correct |
3 ms |
204 KB |
Output is correct |
15 |
Correct |
3 ms |
204 KB |
Output is correct |
16 |
Correct |
3 ms |
332 KB |
Output is correct |
17 |
Correct |
3 ms |
336 KB |
Output is correct |
18 |
Correct |
3 ms |
204 KB |
Output is correct |
19 |
Correct |
3 ms |
332 KB |
Output is correct |
20 |
Correct |
3 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
204 KB |
Output is correct |
2 |
Correct |
3 ms |
204 KB |
Output is correct |
3 |
Correct |
3 ms |
332 KB |
Output is correct |
4 |
Correct |
3 ms |
332 KB |
Output is correct |
5 |
Correct |
3 ms |
332 KB |
Output is correct |
6 |
Correct |
4 ms |
204 KB |
Output is correct |
7 |
Correct |
2 ms |
204 KB |
Output is correct |
8 |
Correct |
3 ms |
292 KB |
Output is correct |
9 |
Correct |
3 ms |
204 KB |
Output is correct |
10 |
Correct |
3 ms |
296 KB |
Output is correct |
11 |
Correct |
3 ms |
332 KB |
Output is correct |
12 |
Correct |
3 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
296 KB |
Output is correct |
14 |
Correct |
3 ms |
204 KB |
Output is correct |
15 |
Correct |
3 ms |
204 KB |
Output is correct |
16 |
Correct |
3 ms |
332 KB |
Output is correct |
17 |
Correct |
3 ms |
336 KB |
Output is correct |
18 |
Correct |
3 ms |
204 KB |
Output is correct |
19 |
Correct |
3 ms |
332 KB |
Output is correct |
20 |
Correct |
3 ms |
332 KB |
Output is correct |
21 |
Correct |
131 ms |
612 KB |
Output is correct |
22 |
Correct |
145 ms |
588 KB |
Output is correct |
23 |
Correct |
122 ms |
588 KB |
Output is correct |
24 |
Correct |
121 ms |
588 KB |
Output is correct |
25 |
Correct |
142 ms |
612 KB |
Output is correct |
26 |
Correct |
92 ms |
592 KB |
Output is correct |
27 |
Correct |
135 ms |
592 KB |
Output is correct |
28 |
Correct |
144 ms |
588 KB |
Output is correct |
29 |
Correct |
140 ms |
624 KB |
Output is correct |
30 |
Correct |
142 ms |
632 KB |
Output is correct |
31 |
Correct |
149 ms |
604 KB |
Output is correct |
32 |
Correct |
139 ms |
588 KB |
Output is correct |
33 |
Correct |
112 ms |
588 KB |
Output is correct |
34 |
Correct |
138 ms |
588 KB |
Output is correct |
35 |
Correct |
138 ms |
588 KB |
Output is correct |
36 |
Correct |
128 ms |
560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
208 KB |
Output is correct |
7 |
Correct |
1 ms |
240 KB |
Output is correct |
8 |
Correct |
1 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
2 ms |
204 KB |
Output is correct |
18 |
Correct |
22 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
204 KB |
Output is correct |
20 |
Correct |
2 ms |
204 KB |
Output is correct |
21 |
Correct |
18 ms |
380 KB |
Output is correct |
22 |
Correct |
85 ms |
460 KB |
Output is correct |
23 |
Correct |
2 ms |
204 KB |
Output is correct |
24 |
Correct |
246 ms |
588 KB |
Output is correct |
25 |
Correct |
131 ms |
592 KB |
Output is correct |
26 |
Correct |
658 ms |
792 KB |
Output is correct |
27 |
Correct |
626 ms |
784 KB |
Output is correct |
28 |
Correct |
318 ms |
588 KB |
Output is correct |
29 |
Correct |
591 ms |
788 KB |
Output is correct |
30 |
Correct |
460 ms |
588 KB |
Output is correct |
31 |
Correct |
644 ms |
788 KB |
Output is correct |
32 |
Correct |
598 ms |
788 KB |
Output is correct |
33 |
Correct |
60 ms |
340 KB |
Output is correct |
34 |
Correct |
632 ms |
788 KB |
Output is correct |
35 |
Correct |
599 ms |
792 KB |
Output is correct |
36 |
Correct |
551 ms |
788 KB |
Output is correct |
37 |
Correct |
541 ms |
588 KB |
Output is correct |
38 |
Correct |
641 ms |
788 KB |
Output is correct |
39 |
Correct |
615 ms |
804 KB |
Output is correct |
40 |
Correct |
653 ms |
792 KB |
Output is correct |
41 |
Correct |
299 ms |
588 KB |
Output is correct |
42 |
Correct |
427 ms |
588 KB |
Output is correct |
43 |
Correct |
536 ms |
792 KB |
Output is correct |
44 |
Correct |
654 ms |
788 KB |
Output is correct |
45 |
Correct |
2 ms |
204 KB |
Output is correct |
46 |
Correct |
3 ms |
204 KB |
Output is correct |
47 |
Correct |
3 ms |
332 KB |
Output is correct |
48 |
Correct |
3 ms |
332 KB |
Output is correct |
49 |
Correct |
3 ms |
332 KB |
Output is correct |
50 |
Correct |
4 ms |
204 KB |
Output is correct |
51 |
Correct |
2 ms |
204 KB |
Output is correct |
52 |
Correct |
3 ms |
292 KB |
Output is correct |
53 |
Correct |
3 ms |
204 KB |
Output is correct |
54 |
Correct |
3 ms |
296 KB |
Output is correct |
55 |
Correct |
3 ms |
332 KB |
Output is correct |
56 |
Correct |
3 ms |
332 KB |
Output is correct |
57 |
Correct |
1 ms |
296 KB |
Output is correct |
58 |
Correct |
3 ms |
204 KB |
Output is correct |
59 |
Correct |
3 ms |
204 KB |
Output is correct |
60 |
Correct |
3 ms |
332 KB |
Output is correct |
61 |
Correct |
3 ms |
336 KB |
Output is correct |
62 |
Correct |
3 ms |
204 KB |
Output is correct |
63 |
Correct |
3 ms |
332 KB |
Output is correct |
64 |
Correct |
3 ms |
332 KB |
Output is correct |
65 |
Correct |
131 ms |
612 KB |
Output is correct |
66 |
Correct |
145 ms |
588 KB |
Output is correct |
67 |
Correct |
122 ms |
588 KB |
Output is correct |
68 |
Correct |
121 ms |
588 KB |
Output is correct |
69 |
Correct |
142 ms |
612 KB |
Output is correct |
70 |
Correct |
92 ms |
592 KB |
Output is correct |
71 |
Correct |
135 ms |
592 KB |
Output is correct |
72 |
Correct |
144 ms |
588 KB |
Output is correct |
73 |
Correct |
140 ms |
624 KB |
Output is correct |
74 |
Correct |
142 ms |
632 KB |
Output is correct |
75 |
Correct |
149 ms |
604 KB |
Output is correct |
76 |
Correct |
139 ms |
588 KB |
Output is correct |
77 |
Correct |
112 ms |
588 KB |
Output is correct |
78 |
Correct |
138 ms |
588 KB |
Output is correct |
79 |
Correct |
138 ms |
588 KB |
Output is correct |
80 |
Correct |
128 ms |
560 KB |
Output is correct |
81 |
Correct |
1667 ms |
1984 KB |
Output is correct |
82 |
Correct |
1659 ms |
1992 KB |
Output is correct |
83 |
Execution timed out |
2070 ms |
1992 KB |
Time limit exceeded |
84 |
Halted |
0 ms |
0 KB |
- |