Submission #399905

# Submission time Handle Problem Language Result Execution time Memory
399905 2021-05-06T21:57:03 Z duality Cultivation (JOI17_cultivation) C++11
100 / 100
1853 ms 1224 KB
#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

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
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 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 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 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 208 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 204 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 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 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 208 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 6 ms 204 KB Output is correct
19 Correct 2 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 5 ms 204 KB Output is correct
22 Correct 30 ms 308 KB Output is correct
23 Correct 2 ms 204 KB Output is correct
24 Correct 84 ms 332 KB Output is correct
25 Correct 46 ms 204 KB Output is correct
26 Correct 206 ms 332 KB Output is correct
27 Correct 200 ms 332 KB Output is correct
28 Correct 105 ms 320 KB Output is correct
29 Correct 183 ms 320 KB Output is correct
30 Correct 155 ms 332 KB Output is correct
31 Correct 211 ms 332 KB Output is correct
32 Correct 193 ms 332 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 204 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 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 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 208 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 6 ms 204 KB Output is correct
19 Correct 2 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 5 ms 204 KB Output is correct
22 Correct 30 ms 308 KB Output is correct
23 Correct 2 ms 204 KB Output is correct
24 Correct 84 ms 332 KB Output is correct
25 Correct 46 ms 204 KB Output is correct
26 Correct 206 ms 332 KB Output is correct
27 Correct 200 ms 332 KB Output is correct
28 Correct 105 ms 320 KB Output is correct
29 Correct 183 ms 320 KB Output is correct
30 Correct 155 ms 332 KB Output is correct
31 Correct 211 ms 332 KB Output is correct
32 Correct 193 ms 332 KB Output is correct
33 Correct 26 ms 204 KB Output is correct
34 Correct 269 ms 380 KB Output is correct
35 Correct 263 ms 452 KB Output is correct
36 Correct 242 ms 376 KB Output is correct
37 Correct 235 ms 332 KB Output is correct
38 Correct 283 ms 332 KB Output is correct
39 Correct 265 ms 332 KB Output is correct
40 Correct 296 ms 392 KB Output is correct
41 Correct 110 ms 332 KB Output is correct
42 Correct 155 ms 332 KB Output is correct
43 Correct 224 ms 384 KB Output is correct
44 Correct 268 ms 452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 2 ms 204 KB Output is correct
3 Correct 2 ms 204 KB Output is correct
4 Correct 3 ms 204 KB Output is correct
5 Correct 2 ms 204 KB Output is correct
6 Correct 2 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 2 ms 204 KB Output is correct
9 Correct 2 ms 204 KB Output is correct
10 Correct 2 ms 204 KB Output is correct
11 Correct 2 ms 204 KB Output is correct
12 Correct 2 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 2 ms 204 KB Output is correct
15 Correct 2 ms 204 KB Output is correct
16 Correct 2 ms 204 KB Output is correct
17 Correct 2 ms 204 KB Output is correct
18 Correct 2 ms 204 KB Output is correct
19 Correct 2 ms 204 KB Output is correct
20 Correct 2 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 2 ms 204 KB Output is correct
3 Correct 2 ms 204 KB Output is correct
4 Correct 3 ms 204 KB Output is correct
5 Correct 2 ms 204 KB Output is correct
6 Correct 2 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 2 ms 204 KB Output is correct
9 Correct 2 ms 204 KB Output is correct
10 Correct 2 ms 204 KB Output is correct
11 Correct 2 ms 204 KB Output is correct
12 Correct 2 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 2 ms 204 KB Output is correct
15 Correct 2 ms 204 KB Output is correct
16 Correct 2 ms 204 KB Output is correct
17 Correct 2 ms 204 KB Output is correct
18 Correct 2 ms 204 KB Output is correct
19 Correct 2 ms 204 KB Output is correct
20 Correct 2 ms 204 KB Output is correct
21 Correct 56 ms 332 KB Output is correct
22 Correct 63 ms 332 KB Output is correct
23 Correct 61 ms 460 KB Output is correct
24 Correct 61 ms 332 KB Output is correct
25 Correct 62 ms 332 KB Output is correct
26 Correct 28 ms 204 KB Output is correct
27 Correct 67 ms 456 KB Output is correct
28 Correct 62 ms 332 KB Output is correct
29 Correct 62 ms 388 KB Output is correct
30 Correct 65 ms 376 KB Output is correct
31 Correct 61 ms 332 KB Output is correct
32 Correct 60 ms 332 KB Output is correct
33 Correct 60 ms 460 KB Output is correct
34 Correct 65 ms 376 KB Output is correct
35 Correct 63 ms 364 KB Output is correct
36 Correct 62 ms 332 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 204 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 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 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 208 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 6 ms 204 KB Output is correct
19 Correct 2 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 5 ms 204 KB Output is correct
22 Correct 30 ms 308 KB Output is correct
23 Correct 2 ms 204 KB Output is correct
24 Correct 84 ms 332 KB Output is correct
25 Correct 46 ms 204 KB Output is correct
26 Correct 206 ms 332 KB Output is correct
27 Correct 200 ms 332 KB Output is correct
28 Correct 105 ms 320 KB Output is correct
29 Correct 183 ms 320 KB Output is correct
30 Correct 155 ms 332 KB Output is correct
31 Correct 211 ms 332 KB Output is correct
32 Correct 193 ms 332 KB Output is correct
33 Correct 26 ms 204 KB Output is correct
34 Correct 269 ms 380 KB Output is correct
35 Correct 263 ms 452 KB Output is correct
36 Correct 242 ms 376 KB Output is correct
37 Correct 235 ms 332 KB Output is correct
38 Correct 283 ms 332 KB Output is correct
39 Correct 265 ms 332 KB Output is correct
40 Correct 296 ms 392 KB Output is correct
41 Correct 110 ms 332 KB Output is correct
42 Correct 155 ms 332 KB Output is correct
43 Correct 224 ms 384 KB Output is correct
44 Correct 268 ms 452 KB Output is correct
45 Correct 1 ms 204 KB Output is correct
46 Correct 2 ms 204 KB Output is correct
47 Correct 2 ms 204 KB Output is correct
48 Correct 3 ms 204 KB Output is correct
49 Correct 2 ms 204 KB Output is correct
50 Correct 2 ms 204 KB Output is correct
51 Correct 1 ms 204 KB Output is correct
52 Correct 2 ms 204 KB Output is correct
53 Correct 2 ms 204 KB Output is correct
54 Correct 2 ms 204 KB Output is correct
55 Correct 2 ms 204 KB Output is correct
56 Correct 2 ms 204 KB Output is correct
57 Correct 1 ms 204 KB Output is correct
58 Correct 2 ms 204 KB Output is correct
59 Correct 2 ms 204 KB Output is correct
60 Correct 2 ms 204 KB Output is correct
61 Correct 2 ms 204 KB Output is correct
62 Correct 2 ms 204 KB Output is correct
63 Correct 2 ms 204 KB Output is correct
64 Correct 2 ms 204 KB Output is correct
65 Correct 56 ms 332 KB Output is correct
66 Correct 63 ms 332 KB Output is correct
67 Correct 61 ms 460 KB Output is correct
68 Correct 61 ms 332 KB Output is correct
69 Correct 62 ms 332 KB Output is correct
70 Correct 28 ms 204 KB Output is correct
71 Correct 67 ms 456 KB Output is correct
72 Correct 62 ms 332 KB Output is correct
73 Correct 62 ms 388 KB Output is correct
74 Correct 65 ms 376 KB Output is correct
75 Correct 61 ms 332 KB Output is correct
76 Correct 60 ms 332 KB Output is correct
77 Correct 60 ms 460 KB Output is correct
78 Correct 65 ms 376 KB Output is correct
79 Correct 63 ms 364 KB Output is correct
80 Correct 62 ms 332 KB Output is correct
81 Correct 686 ms 588 KB Output is correct
82 Correct 680 ms 600 KB Output is correct
83 Correct 1004 ms 780 KB Output is correct
84 Correct 1095 ms 784 KB Output is correct
85 Correct 1740 ms 780 KB Output is correct
86 Correct 1782 ms 780 KB Output is correct
87 Correct 1617 ms 620 KB Output is correct
88 Correct 1801 ms 1224 KB Output is correct
89 Correct 1853 ms 1224 KB Output is correct
90 Correct 1293 ms 1156 KB Output is correct
91 Correct 1750 ms 772 KB Output is correct
92 Correct 1777 ms 780 KB Output is correct
93 Correct 1751 ms 1088 KB Output is correct
94 Correct 1828 ms 1224 KB Output is correct
95 Correct 1764 ms 764 KB Output is correct
96 Correct 1629 ms 768 KB Output is correct
97 Correct 1757 ms 772 KB Output is correct
98 Correct 72 ms 332 KB Output is correct