제출 #838505

#제출 시각아이디문제언어결과실행 시간메모리
838505tch1cherin수열 (APIO14_sequence)C++17
0 / 100
926 ms85380 KiB
#include <bits/stdc++.h> using namespace std; const int64_t INF = 1ll << 60; const int MAX_N = 100005, MAX_K = 205; vector<int64_t> dp, new_dp; vector<int64_t> pref, pref_sq; int path[MAX_N][MAX_K]; int64_t get_weight(int l, int r) { int64_t sum = pref[r] - pref[l]; int64_t sum_sq = pref_sq[r] - pref_sq[l]; return sum * sum - sum_sq; } void divide(int ki, int l, int r, int optl, int optr) { if (l >= r) { return; } int mid = (l + r) / 2; int opt; for (int i = optl; i < min(mid, optr); i++) { int64_t value = dp[i] + get_weight(i, mid); if (new_dp[mid] > value) { new_dp[mid] = value; opt = i; } } path[mid][ki] = opt; divide(ki, l, mid, optl, opt + 1); divide(ki, mid + 1, r, opt, optr); } int main() { cin.tie(nullptr)->sync_with_stdio(false); int N, K; cin >> N >> K; vector<int> A(N); for (int &v : A) { cin >> v; } K++; pref = pref_sq = vector<int64_t>(N + 1); for (int i = 0; i < N; i++) { pref[i + 1] = pref[i] + A[i]; pref_sq[i + 1] = pref_sq[i] + A[i] * A[i]; } memset(path, -1, sizeof path); dp = vector<int64_t>(N + 1, INF); dp[0] = 0; for (int i = 0; i < K; i++) { new_dp = vector<int64_t>(N + 1, INF); divide(i + 1, 0, N + 1, 0, N + 1); dp = new_dp; } cout << (get_weight(0, N) - dp[N]) / 2 << "\n"; int pos = N, ki = K; vector<int> ans; while (pos > 0) { ans.push_back(pos = path[pos][ki--]); } ans.pop_back(); reverse(ans.begin(), ans.end()); for (int i = 0; i < K - 1; i++) { cout << ans[i] << " \n"[i + 1 == K - 1]; } }

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

sequence.cpp: In function 'void divide(int, int, int, int, int)':
sequence.cpp:30:9: warning: 'opt' may be used uninitialized in this function [-Wmaybe-uninitialized]
   30 |   divide(ki, l, mid, optl, opt + 1);
      |   ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...