제출 #31277

#제출 시각아이디문제언어결과실행 시간메모리
31277nickyrio수열 (APIO14_sequence)C++14
89 / 100
459 ms89528 KiB
#include <bits/stdc++.h> using namespace std; #define K 210 #define N 100010 long long a[N], b[N], pos[N]; double x[N]; int n, k; long long A[N], s[N]; int pre[K][N]; long long dp[N]; struct Hull { int start, last; Hull(int n): start(1), last(0) {} void init() { start = 1; last = 0; } void add(int k, long long aNew, long long bNew, long long pNew) { double xNew = -1e18; while(start <= last) { if (aNew == a[last]) { if (bNew >= b[last]) { b[last] = bNew; pos[last] = pNew; } return; } xNew = 1.0 * (b[last] - bNew) / (aNew - a[last]); if (start == last || xNew >= x[last]) break; last--; } a[++last] = aNew; b[last] = bNew; x[last] = xNew; pos[last] = pNew; } long long get(int k, int xQuery) { if (start > last) return 0; while (start < last && pos[start + 1] <= xQuery && x[start + 1] <= s[xQuery]) start++; return start; } }; void trace(int k, int u) { if (k != 1) trace(k - 1, pre[k][u]); printf("%d ", u); } int main() { //freopen("SEQUENCE.inp","r",stdin); //freopen("SEQUENCE.out","w",stdout); //ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); //cin>>n>>k; scanf("%d%d",&n, &k); for (int i = 1; i<=n; i++) cin>>A[i]; for (int i = 1; i<=n; i++) s[i] = A[i] + s[i - 1]; memset(dp, 0, sizeof dp); Hull CH(n); for (int kk = 0; kk<= k - 1; kk++) { for (int i = 1; i<=n; i++) { int tmp = CH.get(kk, i); pre[kk + 1][i] = pos[tmp]; dp[i] = b[tmp] + s[i] * (a[tmp] + s[n] - s[i]); } CH.init(); for (int i = 1; i<=n; i++) { CH.add(kk + 1, s[i], dp[i] - s[n] * s[i], i); } } long long ans = k; for (int i = k + 1; i<n; i++) if (dp[ans] <= dp[i]) ans = i; printf("%lld\n",dp[ans]);//<<endl; trace(k, ans); }

컴파일 시 표준 에러 (stderr) 메시지

sequence.cpp: In function 'int main()':
sequence.cpp:61:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d",&n, &k);
                         ^
#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...