Submission #1106206

#TimeUsernameProblemLanguageResultExecution timeMemory
1106206manhlinh1501Feast (NOI19_feast)C++17
100 / 100
138 ms14560 KiB
#include<bits/stdc++.h> using namespace std; using i64 = long long; template<class X, class Y> bool minimize(X &x, const Y &y) { if (x > y) { x = y; return true; } return false; } template<class X, class Y> bool maximize(X &x, const Y &y) { if (x < y) { x = y; return true; } return false; } using pli = pair<i64, int>; const int MAXN = 3e5 + 5; const int oo32 = 1e9 + 5; const i64 oo64 = 1e18 + 5; int N, K; i64 a[MAXN]; namespace subtask1 { bool is_subtask() { for(int i = 1; i <= N; i++) { if(a[i] < 0) return false; } return true; } void solution() { if(is_subtask() == false) return; i64 ans = accumulate(a + 1, a + N + 1, 0LL); cout << ans; exit(0); } } namespace subtask2 { bool is_subtask() { int c = 0; for(int i = 1; i <= N; i++) c += (a[i] < 0); return c <= 1; } void solution() { if(is_subtask() == false) return; i64 tot = accumulate(a + 1, a + N + 1, 0LL); i64 ans = tot; for(int i = 1; i <= N; i++) ans = max(ans, tot - a[i]); cout << ans; exit(0); } } namespace subtask3 { bool is_subtask() { return K == 1; } i64 dp[MAXN]; void solution() { if(is_subtask() == false) return; for(int i = 1; i <= N; i++) dp[i] = max(dp[i - 1], 0LL) + a[i]; cout << *max_element(dp + 1, dp + N + 1); exit(0); } } namespace subtask { void solution() { for(int i = 1; i <= N; i++) a[i] += a[i - 1]; vector<vector<i64>> dp(N + 1, vector<i64>(K + 1, -oo64)); vector<i64> maxx(N + 1, 0); dp[0][0] = 0; for(int i = 1; i <= N; i++) maxx[i] = max(maxx[i - 1], -a[i]); for(int j = 1; j <= K; j++) { for(int i = 1; i <= N; i++) maximize(dp[i][j], maxx[i - 1] + a[i]); for(int i = 1; i <= N; i++) maxx[i] = max(maxx[i - 1], dp[i][j]); for(int i = 1; i <= N; i++) maxx[i] = max(maxx[i - 1], maxx[i] - a[i]); } i64 ans = 0; for(int i = 1; i <= N; i++) { for(int j = 1; j <= K; j++) maximize(ans, dp[i][j]); } cout << ans; } } namespace solver { pli dp[MAXN][2]; void calc(i64 mid) { for(int i = 0; i <= N; i++) dp[i][0] = dp[i][1] = pli(-oo64, -oo64); dp[0][0] = pli(0, 0); for(int i = 1; i <= N; i++) { dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]); dp[i][1] = max(make_pair(dp[i - 1][0].first + a[i] - mid, dp[i - 1][0].second + 1), make_pair(dp[i - 1][1].first + a[i], dp[i - 1][1].second)); } } void solution() { i64 low = 0, high = oo64; i64 ans = 0; while(low <= high) { i64 mid = (low + high) / 2; calc(mid); if(max(dp[N][0], dp[N][1]).second >= K) { ans = mid; low = mid + 1; } else high = mid - 1; } calc(ans); cout << max(dp[N][0], dp[N][1]).first + K * ans; } } signed main() { #define task "code" if(fopen(task".inp", "r")) { freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); } ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> N >> K; for(int i = 1; i <= N; i++) cin >> a[i]; subtask1::solution(); subtask3::solution(); subtask2::solution(); solver::solution(); return (0 ^ 0); }

Compilation message (stderr)

feast.cpp: In function 'int main()':
feast.cpp:130:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  130 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:131:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  131 |         freopen(task".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...
#Verdict Execution timeMemoryGrader output
Fetching results...