Submission #15535

#TimeUsernameProblemLanguageResultExecution timeMemory
15535tonyjjw달리는 게임 (kriii3_E)C++14
70 / 70
228 ms32848 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...