제출 #846705

#제출 시각아이디문제언어결과실행 시간메모리
846705BamshooTFeast (NOI19_feast)C++14
100 / 100
105 ms30812 KiB
#include <iostream> #include <algorithm> #include <vector> #include <math.h> #include <cstdlib> #define ll long long #define FOR(i, a, b) for (int i = (a); i <= (b); ++i) #define inf -1e18 #define fi first #define se second using namespace std; typedef pair <ll, int> li; const int N = 3e5 + 9, M = 2e3 + 9; int n, k; ll a[N], pre[N], dp[M][M]; void sub1(){ // dp[0][1] = pre[1]; for (int t = 1; t <= k; ++t) { ll mx = 0; for (int i = 1; i <= n; ++i) { dp[t][i] = max(dp[t][i-1], pre[i] + mx); mx = max(mx, dp[t-1][i] - pre[i]); } } cout << dp[k][n]; } pair<ll, int> f[N]; void maximize(li &a, li b){ if (a.fi < b.fi) a = b; if (a.fi == b.fi && a.se > b.se) a = b; } li calc(ll cost){ li best = {0, 0}; for (int i = 1; i <= n; ++i) { f[i] = f[i-1]; maximize(f[i], {pre[i] + best.fi - cost , best.se + 1}); maximize(best, {f[i].fi - pre[i], f[i].se}); } return f[n]; } void sub2(){ ll lo = -1e15, hi = 1e15, ans = 0; while (lo <= hi) { ll mid = lo + hi >> 1; li tmp = calc(mid); if (tmp.se <= k) { ans = max(ans, tmp.fi + tmp.se*mid); hi = mid - 1; } else lo = mid + 1; } cout << ans; } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("nhap.inp", "r", stdin); // freopen("xuat.out", "w", stdout); cin >> n >> k; FOR(i, 1, n) { cin >> a[i]; pre[i] = pre[i-1] + a[i]; } if (max(k, n) <= 2000) sub1(); else sub2(); return 0; }

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

feast.cpp: In function 'void sub2()':
feast.cpp:53:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   53 |         ll mid = lo + hi >> 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...
#Verdict Execution timeMemoryGrader output
Fetching results...