제출 #286529

#제출 시각아이디문제언어결과실행 시간메모리
286529BeanZ수열 (APIO14_sequence)C++14
71 / 100
2103 ms82684 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define endl '\n' const int N = 1e5 + 5; ll dp[N][2]; int opt[N][205]; int a[N]; void DP(int k, int l, int r, int optl, int optr){ if (l > r) return; int mid = (l + r) >> 1; dp[mid][1] = -1e18; int bestl = mid - 1, bestr = optl; for (int i = optl; i <= min(mid - 1, optr); i++){ if (dp[mid][1] == (dp[i][0] + 1ll * a[i] * (a[mid] - a[i]))){ dp[mid][1] = dp[i][0] + 1ll * a[i] * (a[mid] - a[i]); bestl = min(bestl, i); bestr = max(bestr, i); opt[mid][k] = i; } if (dp[mid][1] < (dp[i][0] + 1ll * a[i] * (a[mid] - a[i]))){ dp[mid][1] = dp[i][0] + 1ll * a[i] * (a[mid] - a[i]); bestl = i; bestr = i; opt[mid][k] = i; } } DP(k, l, mid - 1, optl, bestr); DP(k, mid + 1, r, bestl, optr); } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); if (fopen("VietCT.INP", "r")){ freopen("VietCT.INP", "r", stdin); freopen("VietCT.OUT", "w", stdout); } int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i], a[i] = a[i] + a[i - 1]; for (int i = 1; i <= k; i++){ DP(i, 1, n, 1, n); for (int j = 1; j <= n; j++){ dp[j][0] = dp[j][1]; } } cout << dp[n][1] << endl; for (int i = k; i >= 1; i--){ cout << opt[n][i] << " "; n = opt[n][i]; } } /* 7 3 4 1 3 4 0 2 3 */

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

sequence.cpp: In function 'int main()':
sequence.cpp:38:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   38 |                 freopen("VietCT.INP", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:39:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   39 |                 freopen("VietCT.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...