Submission #1113242

#TimeUsernameProblemLanguageResultExecution timeMemory
1113242minhvuleFeast (NOI19_feast)C++17
41 / 100
18 ms3268 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define fi first #define se second #define pb push_back #define freo(task) if(fopen(task".inp", "r")){ freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); } #define FOR(i, a, b) for(int i = (a), _b = (b); i <= _b; i ++) #define FORD(i, a, b) for(int i = (a), _b = (b); i >= _b; i --) #define bit(x, i) ((x >> i) & 1) #define oo 1e18 #define all(v) v.begin(), v.end() using ll = long long; using pii = pair<int, int>; using vi = vector<int>; // mt19937 rd(chrono::steady_clock::now().time_since_epoch().count()); // ll rand(ll l, ll r) { assert(l <= r); return uniform_int_distribution<ll>(l, r)(rd); } const int N = 1e5 + 5; const int mod = 1e9 + 7; const int base = 31; int n, k, a[N], dp[N], s[N]; /* sub5: dp O(n ^ 3) dp1[i][j] là tổng lớn nhất khi chia i món cho j thằng dp1[i][j] = max(dp1[i - 1][j], max(dp1[k - 1][j]) + (a[k] -> i) */ int dp1[305][305]; void sub5(){ FOR(i, 1, n) cin>>a[i], a[i] += a[i - 1]; FOR(i, 1, n) FOR(j, 1, k){ dp1[i][j] = dp1[i - 1][j]; FOR(t, 1, i - 1) dp1[i][j] = max(dp1[i][j], dp1[t - 1][j - 1] + a[i] - a[t - 1]); } // FOR(i, 1, n) cout<<dp1[i][k]<<" "; cout<<dp1[n][k]; } int dp2[2005][2005], g[2005][2005]; /* sub6: O(n ^ 2) dp[i][j] là tổng lớn nhất khi chia i món cho j thằng vua g[i][j] là giá tri tối ưu khi chia i món với số lượng vua bất kì từ 1..j vì có thằng vua có thể không có */ void sub6(){ FOR(i, 1, n) cin>>a[i]; FOR(i, 1, n) FOR(j, 1, k){ dp2[i][j] = max(dp2[i - 1][j] + a[i], g[i - 1][j - 1] + a[i]); g[i][j] = max(g[i - 1][j], dp2[i][j]); } int ans = 0; FOR(j, 1, k) ans = max(ans, g[n][j]); cout<<ans; } /* sub7: O(nlog) tham lam xoa cac phan tu am o dau va cuoi cua mang gop cac gia tri am duong -> mang a am duong xen ke */ int res[N], pre[N], nxt[N]; bool check[N]; vector<int> v; priority_queue<tuple<int, int, int>> pq; void sub7(){ FOR(i, 1, n) cin>>a[i]; int l = 1, r = n; while(l <= n && a[l] <= 0) l ++; while(r >= 1 && a[r] <= 0) r--; if(l > r){ cout<<0; return; } n = 0; FOR(i, l, r) a[++ n] = a[i]; // FOR(i, 1, n) cout<<a[i]<<" "; FOR(i, 1, n) s[i] = s[i - 1] + a[i]; int last = 0, cnt = 0; FOR(i, 1, n) if(i > 1 && (a[i] > 0) != (a[i - 1] > 0)){ if(a[i - 1] > 0) cnt ++; res[i - 1] = -abs(s[i - 1] - s[last]); pre[i - 1] = last; nxt[last] = i - 1; pq.push({res[i - 1], last, i - 1}); last = i - 1; } cnt ++; res[n] = -abs(s[n] - s[last]); pre[n] = last; nxt[last] = n; pq.push({res[n], last, n}); int ans = 0; while(!pq.empty() && cnt > k){ int val = get<0>(pq.top()), l = get<1>(pq.top()), r = get<2>(pq.top()); pq.pop(); if(check[l] || check[r] || val != res[r]) continue; cnt --; check[l] = check[r] = 1; ans += res[r]; int i = pre[l], j = nxt[r]; if(i < j && i >= 0 && j <= n){ pre[j] = i, nxt[i] = j; res[j] = res[j] + res[l] - res[r]; res[r] = -oo; } if(i < j && i >= 0 && j <= n && !check[j]) pq.push({res[j], i, j}); } FOR(i, 1, n) if(a[i] > 0) ans += a[i]; cout<<ans; } void solve(){ cin>>n>>k; sub7(); } int32_t main(){ ios::sync_with_stdio(false); cin.tie(NULL); // freo(""); int nTest = 1; // cin>>nTest; while(nTest --) solve(); // cerr << "\nTime: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\n"; return 0; } /* */
#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...