답안 #421729

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
421729 2021-06-09T11:26:52 Z balbit Shortcut (IOI16_shortcut) C++14
0 / 100
1 ms 332 KB
#include "shortcut.h"

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

#define ll long long
#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;
ll p[maxn];
ll d[maxn];
int C;

ll gt(int a, int b, int x, int y) {
    if (a>b) swap(a,b);
    if (x>y) swap(x,y);
    return d[x] + d[y] + min((p[y] - p[x]), C + abs(p[x] - p[a]) + abs(p[y]-p[b]));
}

const ll inf = 1ll<<61;

ll Ld[maxn],Rd[maxn];

ll go(vector<ll> v, ll CD, int stp) {
//    {
//        ll re = 0;
//        REP(i, SZ(v)) REP(j, i) {
//            ll hi = min(p[i+stp]-p[j+stp], CD-p[i+stp]+p[j+stp]) + v[i] + v[j];
//            MX(re, hi);
//        }
//        return re;
//    }
    deque<pair<ll, int> > stk;
    ll bst = -inf;
    int j = -1;
    ll re = 0;
    REP(i, SZ(v )) {
        while (j + 1 < i && (p[i+stp] - p[j+1+stp] ) * 2 > CD) {
            ++j;
            MX(bst, v[j] + p[j+stp]);
        }
        MX(re, bst - p[i+stp] + v[i] + CD);
        while (!stk.empty() && stk.front().s <= j) stk.pop_front();
        if (!stk.empty())
            MX(re, p[i+stp] + v[i] + stk.front().f);

        while (!stk.empty() && stk.back().f < v[i] - p[i+stp] ) stk.pop_back();
        stk.pb({v[i] - p[i+stp],i});
    }
    bug(SZ(v), re);
    return re;
}

ll pfL[maxn], pfR[maxn];

long long find_shortcut(int n, std::vector<int> l, std::vector<int> _d, int _c)
{
    C = _c;
    p[0] = 0;
    REP(i,n) d[i] =_d[i];
    for (int i = 1; i<n; ++i) {
        p[i] = l[i-1] + p[i-1];
    }
    ll re = inf;
    ll rt = 0;
    pii pp;
//    REP(b,n) REP(a,b) {
//        ll t = 0;
//        REP(y,n) REP(x,y) {
//            MX(t, gt(a,b,x,y));
//        }
//        if (t < re) {
//            pp = {a,b};
//        }
//        MN(re, t);
//    }

    pii dia;
    {
        // find diameter
        pair<ll, int> bst = {-inf, -1};
        rt = 0;
        for (int i = 0; i<n; ++i) {
            ll hi = d[i] + p[i] + bst.f;
            if (hi > rt) {
                dia = {bst.s, i};
                rt = hi;
            }
            if (d[i] - p[i] > bst.f) {
                bst = max(bst, {d[i] - p[i] , i});
            }
            pfL[i] = rt;
        }
        rt = 0;
        ll tb = -inf;
        for (int i = n-1; i>=0; --i) {
            ll hi = d[i] - p[i] + tb;
            MX(rt, hi);
            pfR[i] = rt;
        }
    }

    {
        // build Ld
        REP(i,n) {
            Ld[i] = d[i];
            if (i) {
                MX(Ld[i], Ld[i-1] + p[i] - p[i-1]);
            }
        }
        // build Rd
        for (int i = n-1; i>=0; --i) {
            Rd[i] = d[i];
            if (i != n-1) {
                MX(Rd[i], Rd[i+1] + p[i+1] - p[i]);
            }
        }
    }

//    assert(dia.f <= pp.f);
//    assert(dia.s >= pp.s);
    bug(pp.f, pp.s);
    {
        ll r2 = inf;
        for (int a = dia.f; a < dia.s; ++a) {
            for (int b = a+1; b<=dia.s; ++b) {
//                int j = a-1;
                ll CD = C + p[b] - p[a];
                ll t = Ld[a] + Rd[b] + C;
                MX(t, pfL[a]);
                MX(t, pfR[b]);
                vector<ll> hi;
                for (int x = a; x <= b; ++x ) {
                    hi.pb(d[x]);

                    if (x == a) {
                        MX(hi.back(), Ld[x]);
                    }
                    if (x == b) {
                        MX(hi.back(), Rd[x]);
                    }
                }
                MX(t, go(hi, CD, a) );
                MN(r2, t);
            }
        }
        bug(r2, re);
        return r2;

    }
    bug(rt);
    bug(dia.f, dia.s);
    bug(pp.f , pp.s );
    return re;
}



//
//signed main(){
////    ll hi = find_shortcut(9, {10, 10, 10, 10, 10, 10, 10, 10},
////{20, 0, 30, 0, 0, 40, 0, 40, 0}, 30);
////    bug(hi);
//    int n; cin>>n;
//    REP(www, 100) {
//        vector<int> l, d;
//        REP(i,n-1) {
//            l.pb(rand()%1000000);
//        }
//        REP(i,n) {
//            d.pb(rand()%1000000);
//        }
//        bug(find_shortcut(n, l, d, 1));
//    }
//}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB n = 4, 80 is a correct answer
2 Correct 1 ms 332 KB n = 9, 110 is a correct answer
3 Correct 1 ms 332 KB n = 4, 21 is a correct answer
4 Incorrect 1 ms 332 KB n = 3, incorrect answer: jury 4 vs contestant 5
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB n = 4, 80 is a correct answer
2 Correct 1 ms 332 KB n = 9, 110 is a correct answer
3 Correct 1 ms 332 KB n = 4, 21 is a correct answer
4 Incorrect 1 ms 332 KB n = 3, incorrect answer: jury 4 vs contestant 5
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB n = 4, 80 is a correct answer
2 Correct 1 ms 332 KB n = 9, 110 is a correct answer
3 Correct 1 ms 332 KB n = 4, 21 is a correct answer
4 Incorrect 1 ms 332 KB n = 3, incorrect answer: jury 4 vs contestant 5
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB n = 4, 80 is a correct answer
2 Correct 1 ms 332 KB n = 9, 110 is a correct answer
3 Correct 1 ms 332 KB n = 4, 21 is a correct answer
4 Incorrect 1 ms 332 KB n = 3, incorrect answer: jury 4 vs contestant 5
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB n = 4, 80 is a correct answer
2 Correct 1 ms 332 KB n = 9, 110 is a correct answer
3 Correct 1 ms 332 KB n = 4, 21 is a correct answer
4 Incorrect 1 ms 332 KB n = 3, incorrect answer: jury 4 vs contestant 5
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB n = 4, 80 is a correct answer
2 Correct 1 ms 332 KB n = 9, 110 is a correct answer
3 Correct 1 ms 332 KB n = 4, 21 is a correct answer
4 Incorrect 1 ms 332 KB n = 3, incorrect answer: jury 4 vs contestant 5
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB n = 4, 80 is a correct answer
2 Correct 1 ms 332 KB n = 9, 110 is a correct answer
3 Correct 1 ms 332 KB n = 4, 21 is a correct answer
4 Incorrect 1 ms 332 KB n = 3, incorrect answer: jury 4 vs contestant 5
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB n = 4, 80 is a correct answer
2 Correct 1 ms 332 KB n = 9, 110 is a correct answer
3 Correct 1 ms 332 KB n = 4, 21 is a correct answer
4 Incorrect 1 ms 332 KB n = 3, incorrect answer: jury 4 vs contestant 5