Submission #591771

#TimeUsernameProblemLanguageResultExecution timeMemory
591771AlperenTShortcut (IOI16_shortcut)C++17
0 / 100
1 ms224 KiB
#include <bits/stdc++.h>
#include "shortcut.h"

using namespace std;

const long long INF = 1e18 + 5;

vector<long long> prefix, leftmax, rightmax, leftansmax, rightansmax;

long long getdist(int x, int y){
    return prefix[y - 1] - prefix[x - 1];
}

long long find_shortcut(int n, vector<int> ln, vector<int> d, int c){
    ln.insert(ln.begin(), 0), d.insert(d.begin(), 0);
    long long ans = INF;

    prefix.assign(n + 2, 0);
    leftmax.assign(n + 2, 0), rightmax.assign(n + 2, 0);
    leftansmax.assign(n + 2, 0), rightansmax.assign(n + 2, 0);

    for(int i = 1; i < n; i++) prefix[i] = prefix[i - 1] + ln[i];

    for(int i = 1; i <= n; i++){
        leftmax[i] = max(leftmax[i - 1] + ln[i - 1], (long long)d[i]); 
        leftansmax[i] = max(leftansmax[i - 1], leftmax[i - 1] + ln[i - 1] + d[i]);
    }
    for(int i = n; i >= 1; i--){
        rightmax[i] = max(rightmax[i + 1] + ln[i], (long long)d[i]); 
        rightansmax[i] = max(rightansmax[i + 1], rightmax[i + 1] + ln[i] + d[i]);
    }

    for(int l = 1; l <= n; l++){
        for(int r = l; r <= n; r++){
            long long curans = leftmax[l] + min(getdist(l, r), (long long)c) + rightmax[r];

            curans = max({curans, leftansmax[l], rightansmax[r]});

            for(int i = l + 1; i <= r - 1; i++){
                curans = max(curans, min(d[i] + getdist(l, i) + leftmax[l], d[i] + getdist(i, r) + c + leftmax[l]));
                curans = max(curans, min(d[i] + getdist(i, r) + rightmax[r], d[i] + getdist(l, i) + c + rightmax[r]));
            }

            int ptr = l + 2;
 
            for(int i = l + 1; i <= r - 1; i++){
                while(ptr + 1 <= r - 1 && min(getdist(i, ptr + 1) + d[i] + d[ptr +1], getdist(l, i) + c + getdist(ptr +1, r) + d[i] + d[ptr +1]) >= min(getdist(i, ptr) + d[i] + d[ptr], getdist(l, i) + c + getdist(ptr, r) + d[i] + d[ptr])){
                    ptr++;
                }
 
                curans = max(curans, min(getdist(i, ptr) + d[i] + d[ptr], getdist(l, i) + c + getdist(ptr, r) + d[i] + d[ptr]));
            }

            ans = min(ans, curans);
        }
    }

    return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...