Submission #600959

# Submission time Handle Problem Language Result Execution time Memory
600959 2022-07-21T09:34:27 Z balbit Shortcut (IOI16_shortcut) C++14
0 / 100
2000 ms 212 KB
// 我的心裡只有你沒有他
#pragma GCC optimize("O3", "unroll-loops")
#include "shortcut.h"

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define int ll
#define pii pair <int, int >
#define f first
#define s second

#define MX(a,b) a = max(a,b)
#define MN(a,b) a = min(a,b)

#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)((x).size())

#ifdef BALBIT
#define bug(...) cerr<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do(T && x) {cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T && x, S && ...y) {cerr<<x<<", "; _do(y...);}
#else
#define bug(...)
#endif // BALBIT


#define pb push_back
#define REP(i,n) for (int i = 0; i<(n); ++i)
#define REP1(i,n) for (int i = 1; i<=(n); ++i)


const int maxn = 1e6+5;
const ll inf = 0x3f3f3f3f3f3f3f3f;

int P[maxn], D[maxn]; // position, depth

vector<pii> Aval, Bval; // A is Da + Pa, B is Db - Pb

ll C[2][2], bigB[2], sbigB[2], whoB[2];


double t1=0,t2=0;
ll thismuchmore;

bool okay(int n, ll M, ll cost) {
    // for all D[a] + P[a] - D[b] + P[b] >= M, 
    clock_t t = clock();
    bug(M, cost);
    
    REP(r,2) REP(R, 2) {
        C[r][R] = -inf;
        bigB[r] = sbigB[r] = -inf;
        whoB[r] = -1;
    }

    int j = SZ(Bval)-1;
    if (M < 1e9 * 2+10) {
        REP(i,SZ(Aval)) {
            int a = Aval[i].s;
            while (j>=0 && Aval[i].f+Bval[j].f > M) {
                // work j
                int b = Bval[j].s;
                bug(a,b,Aval[i].f + Bval[i].f);
                REP(vx,2) {
                    ll val = D[b] +(vx?-1:1)*P[b];
                    if (val > bigB[vx]) {
                        sbigB[vx] = bigB[vx];
                        whoB[vx] = b;
                        bigB[vx] = val;
                    }else if (val > sbigB[vx]) {
                        sbigB[vx] = val;
                    }
                }
                --j;
            }
            REP(vx,2) {
                ll ka = cost-M+D[a]+(vx?-P[a]:P[a]);
                REP(vy,2) {
                    int bval = whoB[vy]==a?sbigB[vy] : bigB[vy];
                    MX(C[vx][vy], (ka + bval));
                }
            }
            if (C[0][0] > -C[1][1]) return 0;
            if (C[0][1] > -C[1][0]) return 0;
        }
    }else{
        REP(i,SZ(Aval)) {
            int a = Aval[i].s;
            while (j>=0 && Aval[i].f+Bval[j].f > M) {
                int b = Bval[j].s;
                REP(vx,2) {
                    MX(bigB[vx], D[b] +(vx?-P[b]:P[b]));
                }
                --j;
            }
            REP(vx,2) {
                ll ka = cost-M+D[a]+(vx?-P[a]:P[a]);
                REP(vy,2) {
                    MX(C[vx][vy], (ka + bigB[vy]));
                }
            }
            if (C[0][0] > -C[1][1]) return 0;
            if (C[0][1] > -C[1][0]) return 0;
        }
    }
    
    t1 += (clock()-t) / (double)CLOCKS_PER_SEC;
    t = clock();

    // REP(X,2) REP(Y,2) C[X][Y] += cost-M;

    // bug(C[0][0], C[0][1], C[1][0], C[1][1]);
     t = clock();

    bool ok = 0;
    signed i00=n, i01=-1, i10=0,i11=n-1;
    
    thismuchmore = inf;

    REP(a, n) {
        ll X = P[a];
 
        // while (i00-1>=0 && P[i00-1] >= C[0][0]-X) --i00;
        // while (i10<n && P[i10] < C[1][0] + X) ++i10;
        // while (i01+1 <n && P[i01+1] <= -C[0][1] + X) ++i01;
        // while (i11 >= 0 && P[i11] > -C[1][1] - X) --i11;
 
        // if (max(i00, i10) <= min(i01, i11)) {
        //     return 1;
        // }


        ll lb = max(C[0][0]-X, C[1][0] + X);
        ll rb = min(-C[0][1]+X, -C[1][1]-X);
        // bug(X, lb, rb);
        if (lb > rb) continue;
        int it = lower_bound(P, P+n, lb) - P;
        if (it < n && P[it]>=lb && P[it] <= rb) {
            return 1;
        }else{
            // MN(thismuchmore, min(P[it]-rb, lb-P[it-1]));
        }
    }
 

    t2 += (clock()-t) / (double)CLOCKS_PER_SEC;

    return ok;

    // conditions: Pa + Da + 


}

long long find_shortcut(signed n, std::vector<signed> l, std::vector<signed> _d, signed _c)
{
    // watermelon 4A orz
    bug(1,2);
    P[0] = 0;
    REP(i,n-1) P[i+1] = P[i] + l[i];
    // P[n] = inf*2;
    REP(i,n) D[i] = _d[i];

    REP(i,n) {
        Aval.pb({D[i] + P[i], i});
        Bval.pb({D[i] - P[i], i});
    }

    sort(ALL(Aval));
    sort(ALL(Bval));

    int lb=P[n-1]/3, rb=P[n-1]+2e9+100;

    while (lb != rb) {
        ll M = (lb+rb)/2;
        if (okay(n,M,_c)) {
            rb = M;
        }else {
            bug(rb, thismuchmore + lb);
            MN(rb, thismuchmore + lb);
            lb = M+1;
        }
    }

    bug(lb);
    return lb;
}


/*


4 10
10 20 20
0 40 0 30


9 30
10 10 10 10 10 10 10 10
20 0 30 0 0 40 0 40 0

3 10
1 1
0 1000 0

*/

//
#ifdef BALBIT
signed main(){

//    bug(hi);
   int n; cin>>n;
   REP(www, 1) {
       vector<signed> l, d;
       REP(i,n-1) {
           l.pb(rand()%1000000000);
       }
       REP(i,n) {
           d.pb(rand()%1000000000);
       }
       bug(find_shortcut(n, l, d, 222));
       bug(t1,t2);
   }
}
#endif

Compilation message

shortcut.cpp: In function 'bool okay(long long int, long long int, long long int)':
shortcut.cpp:118:12: warning: unused variable 'i00' [-Wunused-variable]
  118 |     signed i00=n, i01=-1, i10=0,i11=n-1;
      |            ^~~
shortcut.cpp:118:19: warning: unused variable 'i01' [-Wunused-variable]
  118 |     signed i00=n, i01=-1, i10=0,i11=n-1;
      |                   ^~~
shortcut.cpp:118:27: warning: unused variable 'i10' [-Wunused-variable]
  118 |     signed i00=n, i01=-1, i10=0,i11=n-1;
      |                           ^~~
shortcut.cpp:118:33: warning: unused variable 'i11' [-Wunused-variable]
  118 |     signed i00=n, i01=-1, i10=0,i11=n-1;
      |                                 ^~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB n = 4, 80 is a correct answer
2 Correct 0 ms 212 KB n = 9, 110 is a correct answer
3 Correct 0 ms 212 KB n = 4, 21 is a correct answer
4 Correct 0 ms 212 KB n = 3, 4 is a correct answer
5 Correct 0 ms 212 KB n = 2, 62 is a correct answer
6 Correct 0 ms 212 KB n = 2, 3 is a correct answer
7 Correct 0 ms 212 KB n = 3, 29 is a correct answer
8 Correct 1 ms 212 KB n = 2, 3 is a correct answer
9 Correct 0 ms 212 KB n = 2, 3 is a correct answer
10 Execution timed out 2091 ms 212 KB Time limit exceeded
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB n = 4, 80 is a correct answer
2 Correct 0 ms 212 KB n = 9, 110 is a correct answer
3 Correct 0 ms 212 KB n = 4, 21 is a correct answer
4 Correct 0 ms 212 KB n = 3, 4 is a correct answer
5 Correct 0 ms 212 KB n = 2, 62 is a correct answer
6 Correct 0 ms 212 KB n = 2, 3 is a correct answer
7 Correct 0 ms 212 KB n = 3, 29 is a correct answer
8 Correct 1 ms 212 KB n = 2, 3 is a correct answer
9 Correct 0 ms 212 KB n = 2, 3 is a correct answer
10 Execution timed out 2091 ms 212 KB Time limit exceeded
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB n = 4, 80 is a correct answer
2 Correct 0 ms 212 KB n = 9, 110 is a correct answer
3 Correct 0 ms 212 KB n = 4, 21 is a correct answer
4 Correct 0 ms 212 KB n = 3, 4 is a correct answer
5 Correct 0 ms 212 KB n = 2, 62 is a correct answer
6 Correct 0 ms 212 KB n = 2, 3 is a correct answer
7 Correct 0 ms 212 KB n = 3, 29 is a correct answer
8 Correct 1 ms 212 KB n = 2, 3 is a correct answer
9 Correct 0 ms 212 KB n = 2, 3 is a correct answer
10 Execution timed out 2091 ms 212 KB Time limit exceeded
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB n = 4, 80 is a correct answer
2 Correct 0 ms 212 KB n = 9, 110 is a correct answer
3 Correct 0 ms 212 KB n = 4, 21 is a correct answer
4 Correct 0 ms 212 KB n = 3, 4 is a correct answer
5 Correct 0 ms 212 KB n = 2, 62 is a correct answer
6 Correct 0 ms 212 KB n = 2, 3 is a correct answer
7 Correct 0 ms 212 KB n = 3, 29 is a correct answer
8 Correct 1 ms 212 KB n = 2, 3 is a correct answer
9 Correct 0 ms 212 KB n = 2, 3 is a correct answer
10 Execution timed out 2091 ms 212 KB Time limit exceeded
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB n = 4, 80 is a correct answer
2 Correct 0 ms 212 KB n = 9, 110 is a correct answer
3 Correct 0 ms 212 KB n = 4, 21 is a correct answer
4 Correct 0 ms 212 KB n = 3, 4 is a correct answer
5 Correct 0 ms 212 KB n = 2, 62 is a correct answer
6 Correct 0 ms 212 KB n = 2, 3 is a correct answer
7 Correct 0 ms 212 KB n = 3, 29 is a correct answer
8 Correct 1 ms 212 KB n = 2, 3 is a correct answer
9 Correct 0 ms 212 KB n = 2, 3 is a correct answer
10 Execution timed out 2091 ms 212 KB Time limit exceeded
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB n = 4, 80 is a correct answer
2 Correct 0 ms 212 KB n = 9, 110 is a correct answer
3 Correct 0 ms 212 KB n = 4, 21 is a correct answer
4 Correct 0 ms 212 KB n = 3, 4 is a correct answer
5 Correct 0 ms 212 KB n = 2, 62 is a correct answer
6 Correct 0 ms 212 KB n = 2, 3 is a correct answer
7 Correct 0 ms 212 KB n = 3, 29 is a correct answer
8 Correct 1 ms 212 KB n = 2, 3 is a correct answer
9 Correct 0 ms 212 KB n = 2, 3 is a correct answer
10 Execution timed out 2091 ms 212 KB Time limit exceeded
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB n = 4, 80 is a correct answer
2 Correct 0 ms 212 KB n = 9, 110 is a correct answer
3 Correct 0 ms 212 KB n = 4, 21 is a correct answer
4 Correct 0 ms 212 KB n = 3, 4 is a correct answer
5 Correct 0 ms 212 KB n = 2, 62 is a correct answer
6 Correct 0 ms 212 KB n = 2, 3 is a correct answer
7 Correct 0 ms 212 KB n = 3, 29 is a correct answer
8 Correct 1 ms 212 KB n = 2, 3 is a correct answer
9 Correct 0 ms 212 KB n = 2, 3 is a correct answer
10 Execution timed out 2091 ms 212 KB Time limit exceeded
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB n = 4, 80 is a correct answer
2 Correct 0 ms 212 KB n = 9, 110 is a correct answer
3 Correct 0 ms 212 KB n = 4, 21 is a correct answer
4 Correct 0 ms 212 KB n = 3, 4 is a correct answer
5 Correct 0 ms 212 KB n = 2, 62 is a correct answer
6 Correct 0 ms 212 KB n = 2, 3 is a correct answer
7 Correct 0 ms 212 KB n = 3, 29 is a correct answer
8 Correct 1 ms 212 KB n = 2, 3 is a correct answer
9 Correct 0 ms 212 KB n = 2, 3 is a correct answer
10 Execution timed out 2091 ms 212 KB Time limit exceeded
11 Halted 0 ms 0 KB -