제출 #87960

#제출 시각아이디문제언어결과실행 시간메모리
87960karma수열 (APIO14_sequence)C++11
49 / 100
400 ms132096 KiB
#include<bits/stdc++.h> #define For(i, a, b) for(int i = a; i <= b; ++i) #define Ford(i, a, b) for(int i = a; i >= b; --i) #define Task "test" #define ld long double #define ll long long #define X first #define Y second #define pb push_back //#define Karma using namespace std; template <typename T> inline void Cin(T &x) { char c; T sign = 1; x = 0; for (c = getchar(); c < '0' || c > '9'; c = getchar()) if (c == '-') sign = -1; for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0'; x *= sign; } typedef pair<int, int> pii; typedef pair<ll, int> plli; const int N = 1e5 + 7; const ll oo = 1e18 + 7; int n, k; static ll a[N], f[205][N], p[205][N]; void Enter() { Cin(n), Cin(k); For(i, 1, n) Cin(a[i]), a[i] += a[i - 1]; } //ll Cost(int i, int j) //{ // int sum = a[j] - a[i - 1], l = i, h = j; // if(j < i) return -oo; // while(l <= h) // { // int mid = (l + h) >> 1; // if((a[mid] - a[i - 1]) * 2 < sum) l = mid + 1; // else h = mid - 1; // } // int costl = 0, costh = 0; // if(l >= i && l <= j) costl = (a[l] - a[i - 1]) * (a[j] - a[l]); // if(h >= i && h <= j) costh = (a[h] - a[i - 1]) * (a[j] - a[h]); // return max(costh, costl); //} void DP(int g, int l, int r, int optl, int optr) { if(l > r) return; int mid = (l + r) / 2, lim = min(optr, mid - 1); f[g][mid] = -1; For(i, optl, lim) { ll newcost = f[g - 1][i] + a[i] * (a[mid] - a[i]); if(newcost > f[g][mid]) { f[g][mid] = newcost; p[g][mid] = i; } } DP(g, l, mid - 1, optl, p[g][mid]); DP(g, mid + 1, r, p[g][mid], optr); } void Solve() { For(i, 1, k) DP(i, 1, n, 1, n); cout << f[k][n] << '\n'; int ptr = n; Ford(i, k, 1) { cout << p[i][ptr] << ' '; ptr = p[i][ptr]; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifdef Karma freopen(Task".inp", "r", stdin); freopen(Task".out", "w", stdout); #endif // Karma Enter(); Solve(); return 0; }
#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...