제출 #1090368

#제출 시각아이디문제언어결과실행 시간메모리
1090368raphaelp수열 (APIO14_sequence)C++14
60 / 100
106 ms131072 KiB
#include <bits/stdc++.h> using namespace std; int main() { long long N, K; cin >> N >> K; vector<long long> Tab(N), cum(N + 1); for (long long i = 0; i < N; i++) { cin >> Tab[i]; cum[i + 1] = cum[i] + Tab[i]; } vector<vector<long long>> dp(K, vector<long long>(N)), dp2(K, vector<long long>(N)); deque<vector<long long>> CHT, CHT2; CHT.push_back({0, 0, 0}); for (long long i = 0; i < K; i++) { CHT2 = {{0, 0}}; for (long long j = 0; j < N; j++) { while (CHT.size() > 1) { long long m = (CHT[1][0] - CHT[0][0]) / (CHT[1][1] - CHT[0][1]); if (cum[N] - cum[j + 1] <= m) CHT.pop_front(); else break; } dp[i][j] = cum[j + 1] * (cum[N] - cum[j + 1]) + CHT.front()[0] - CHT.front()[1] * (cum[N] - cum[j + 1]); dp2[i][j] = CHT.front()[2]; while (CHT2.size() > 1) { if (CHT2[CHT2.size() - 2][1] == CHT2[CHT2.size() - 1][1] || (double)((dp[i][j] - CHT2[CHT2.size() - 2][0]) / (cum[j + 1] - CHT2[CHT2.size() - 2][1])) > (double)((CHT2.back()[0] - CHT2[CHT2.size() - 2][0]) / (CHT2.back()[1] - CHT2[CHT2.size() - 2][1]))) CHT2.pop_back(); else break; } if (CHT2.back()[0] < dp[i][j]) CHT2.push_back({dp[i][j], cum[j + 1], j}); } swap(CHT, CHT2); } long long best = 0, best2 = 0; for (long long i = 0; i < N; i++) { if (best < dp[K - 1][i]) { best = dp[K - 1][i]; best2 = i; } } cout << best << '\n'; for (int i = K - 1; i >= 0; i--) { cout << best2 + 1 << ' '; best2 = dp2[i][best2]; } }
#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...