Submission #366903

#TimeUsernameProblemLanguageResultExecution timeMemory
366903mickeyandkakaFeast (NOI19_feast)C++17
100 / 100
164 ms17676 KiB
//{{{ #include <bits/stdc++.h> using namespace std; using LL = long long; using VLL = vector<LL>; using vi = vector<int>; using pii = pair<int, int>; #define sz(x) (int)((x).size()) #define all(x) (x).begin(), (x).end() #define clr(a, b) memset(a, b, sizeof(a)) #ifdef LOCAL #include "prettyprint.hpp" // clang-format off void _print() { cerr << "]\033[0m\n"; } template <typename T> void __print(T x) { cerr << x; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } #define debug(x...) cerr << "\033[1;34m[" << #x << "] = \033[1;32m["; _print(x) #define debug_arr(x...) cerr << "\033[1;34m[" << #x << "] = \033[1;32m" << (x) << "\033[0m\n" #else #define debug(x...) #define debug_arr(x...) #endif // clang-format on //}}} const int N = 3e5 + 10; // const int N = 10; int n, k; LL a[N]; LL s[N]; LL dp[N]; LL cnt[N]; LL minf[N]; LL minf_p[N]; int check(LL c) { dp[0] = 0, cnt[0] = 0; minf[0] = dp[0] - s[0]; minf_p[0] = 0; for (int i = 1; i <= n; i++) { dp[i] = dp[i - 1]; cnt[i] = cnt[i - 1]; if (minf[i - 1] + s[i] - c > dp[i]) { dp[i] = minf[i - 1] + s[i] - c; // debug(i, dp[i]); cnt[i] = cnt[minf_p[i - 1]] + 1; } else if (minf[i - 1] + s[i] - c == dp[i]) { if (cnt[i] < cnt[minf_p[i - 1]] + 1) { cnt[i] = cnt[minf_p[i - 1]] + 1; } } minf[i] = minf[i - 1]; minf_p[i] = minf_p[i - 1]; if (dp[i] - s[i] > minf[i]) { minf[i] = dp[i] - s[i]; minf_p[i] = i; } } // if (cnt[n] == 0) return k + 1; return cnt[n]; } // const int NN = 1e3 + 10; // LL dp2[NN][NN]; int main() { #ifdef LOCAL freopen("in", "r", stdin); // freopen("out", "w", stdout); #endif ios::sync_with_stdio(false); cin.tie(0); while (cin >> n >> k) { for (int i = 1; i <= n; ++i) cin >> a[i]; s[0] = 0; for (int i = 1; i <= n; ++i) s[i] = s[i - 1] + a[i]; LL lo = 0, hi = 1e15; bool fd = false; while (lo + 1 < hi) { LL mid = lo + (hi - lo) / 2; if (check(mid) >= k) //最大的Mid >= k { lo = mid; fd = true; } else { hi = mid; } } LL c = lo; if (check(hi) >= k) c = hi; int v = check(c); LL ans = dp[n] + c * k; v = check(0); if (cnt[n] <= k) { LL ans2 = dp[n]; ans = max(ans, ans2); } cout << ans << endl; } return 0; }

Compilation message (stderr)

feast.cpp: In function 'int main()':
feast.cpp:98:14: warning: variable 'fd' set but not used [-Wunused-but-set-variable]
   98 |         bool fd = false;
      |              ^~
feast.cpp:116:13: warning: variable 'v' set but not used [-Wunused-but-set-variable]
  116 |         int v = check(c);
      |             ^
#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...