제출 #488159

#제출 시각아이디문제언어결과실행 시간메모리
488159ntabc05101수열 (APIO14_sequence)C++14
100 / 100
857 ms82028 KiB
#include<bits/stdc++.h> using namespace std; #define taskname "" const long long inf = 1e17; int main() { if (fopen(taskname".inp", "r")) { freopen(taskname".inp", "r", stdin); freopen(taskname".out", "w", stdout); } else { if (fopen(taskname".in", "r")) { freopen(taskname".in", "r", stdin); freopen(taskname".out", "w", stdout); } } cin.tie(0)->sync_with_stdio(0); int n, K; cin >> n >> K; long long a[n + 1]; a[0] = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; a[i] += a[i - 1]; } vector<long long> dp[2]; function<bool(int, int, int)> chk = [&](int i, int j, int t) { return (dp[0][i] - a[i] * a[i] - dp[0][j] + a[j] * a[j]) <= a[t] * (a[j] - a[i]); }; function<bool(int, int, int)> chk2 = [&](int i, int j, int t) { return (dp[0][i] - a[i] * a[i] - dp[0][j] + a[j] * a[j]) * (a[t] - a[j]) >= (dp[0][j] - a[j] * a[j] - dp[0][t] + a[t] * a[t]) * (a[j] - a[i]); }; //K++; dp[0].resize(n + 1); dp[1].resize(n + 1); int prf[n + 1][K + 1]; memset(prf, 0, sizeof(prf)); long long res = 0; dp[0][0] = 0; deque<int> deq; for (int k = 1; k <= K; k++) { deq.clear(); deq.push_back(0); for (int i = 1; i <= n; i++) { while (deq.size() > 1 && chk(deq[0], deq[1], i)) { deq.pop_front(); } int j = deq.front(); dp[1][i] = dp[0][j] + (a[i] - a[j]) * a[j]; prf[i][k] = j; while (deq.size() > 1 && chk2(deq[deq.size() - 2], deq.back(), i)) { deq.pop_back(); } deq.push_back(i); } dp[0] = dp[1]; } cout << dp[0][n] << "\n"; while (K > 0) { cout << prf[n][K] << " "; n = prf[n][K--]; } cout << "\n"; return 0; } /* 7 3 4 1 3 4 0 2 3 */

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

sequence.cpp: In function 'int main()':
sequence.cpp:41:12: warning: unused variable 'res' [-Wunused-variable]
   41 |  long long res = 0;
      |            ^~~
sequence.cpp:10:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   10 |   freopen(taskname".inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:11:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 |   freopen(taskname".out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:15:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 |    freopen(taskname".in", "r", stdin);
      |    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:16:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |    freopen(taskname".out", "w", stdout);
      |    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...