Submission #15535

# Submission time Handle Problem Language Result Execution time Memory
15535 2015-07-12T08:57:35 Z tonyjjw 달리는 게임 (kriii3_E) C++14
70 / 70
228 ms 32848 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;

const int N = 1000005;
const double INF = 1.e15;

struct lineinfo {
    long long slope,yadd;
    double x_left,x_right;
    lineinfo() {}
    lineinfo(long long slope,long long yadd,double x_left,double x_right):
        slope(slope),yadd(yadd),x_left(x_left),x_right(x_right) {}
};

int n;
long long inp[N],A[N],S[N],D[N];
vector<lineinfo> convex;

long long find_val(long long x) {
    int lo = 0,hi = (int)convex.size()-1,mid = -1;
    while(lo <= hi) {
        mid = (lo + hi)/2;
        double x_left = convex[mid].x_left;
        double x_right = convex[mid].x_right;
        if(x_left <= x && x <= x_right) {
            return
                (long long)convex[mid].slope * x + convex[mid].yadd;
        }
        else {
            if(x <= x_left) {
                hi = mid-1;
            }
            else {
                lo = mid+1;
            }
        }
    }
}

int main(void) {
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    scanf("%d",&n);
    for(int i = 0; i < n; i++) {
        scanf("%lld",&inp[i]);
    }
    A[n-1] = inp[n-1];
    for(int i = n-2; i >= 0; i--) A[i] = A[i+1] + inp[i];
    S[n-1] = A[n-1];
    for(int i = n-2; i >= 0; i--) S[i] = S[i+1] + A[i];

    if(n == 1) {
        printf("%lld\n",max(inp[0],0ll));
        return 0;
    }

    // input data processed
    convex.push_back(lineinfo(0,S[0],-INF,INF));
    D[0] = max(inp[0],0ll);
    for(int i = 1; i < n; i++) {

        long long a = i, b = S[i] + D[i-1];
        while((int)convex.size() > 0) {
            long long c = convex.back().slope,d = convex.back().yadd;
            double intersect_x = (double)(b-d)/(c-a);
            if(convex.back().x_left <= intersect_x &&
                    intersect_x <= convex.back().x_right) {
                convex.back().x_right = intersect_x;
                convex.push_back(
                        lineinfo(a,b, intersect_x, INF)
                        );
                break;
            }
            convex.pop_back();
        }
        
        long long val = find_val(A[i+1]) - S[i+1] - (i+1)*A[i+1];
        //printf("val: %lld\n", val);
        D[i] = max(D[i-1],val);
        //long long val = find_val(A[i+1]) - S[i+1] - (i+1)*A[i+1];
        //D[i] = max(D[i-1], max(D[i-1] + inp[i], val));
        //printf("at %d: %lld\n", i, D[i]);
        
    }
    long long ans = 0;
    for(int i = 0; i < n; i++)
        ans = max(ans,D[i]);
    printf("%lld\n",ans);

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 32460 KB Output is correct
2 Correct 0 ms 32460 KB Output is correct
3 Correct 0 ms 32460 KB Output is correct
4 Correct 0 ms 32460 KB Output is correct
5 Correct 0 ms 32460 KB Output is correct
6 Correct 0 ms 32460 KB Output is correct
7 Correct 0 ms 32460 KB Output is correct
8 Correct 0 ms 32460 KB Output is correct
9 Correct 0 ms 32460 KB Output is correct
10 Correct 0 ms 32460 KB Output is correct
11 Correct 0 ms 32460 KB Output is correct
12 Correct 0 ms 32460 KB Output is correct
13 Correct 0 ms 32460 KB Output is correct
14 Correct 0 ms 32460 KB Output is correct
15 Correct 0 ms 32460 KB Output is correct
16 Correct 0 ms 32460 KB Output is correct
17 Correct 0 ms 32460 KB Output is correct
18 Correct 1 ms 32460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 32460 KB Output is correct
2 Correct 9 ms 32460 KB Output is correct
3 Correct 9 ms 32460 KB Output is correct
4 Correct 25 ms 32460 KB Output is correct
5 Correct 16 ms 32848 KB Output is correct
6 Correct 22 ms 32460 KB Output is correct
7 Correct 103 ms 32460 KB Output is correct
8 Correct 228 ms 32592 KB Output is correct
9 Correct 218 ms 32592 KB Output is correct