# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1103145 |
2024-10-20T09:22:45 Z |
DuongKiet |
Feast (NOI19_feast) |
C++17 |
|
441 ms |
23368 KB |
#include <bits/stdc++.h>
#define N 300005
#define inf (long long) 1e17
#define ll long long
#define int long long
#define ii pair <int,long long>
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define pb push_back
using namespace std;
const unsigned ll Mod = 1000000007LL;
int n, k, a[N];
void init(){
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> a[i];
}
bool ifsub1(){
for (int i = 1; i <= n; i++)
if (a[i] < 0) return 0;
return 1;
}
void sub1(){
int ans = 0;
for (int i = 1; i <= n; i++)
ans += a[i];
cout << ans << '\n';
}
int pos;
bool ifsub2(){
int cnt = 0;
for (int i = 1; i <= n; i++)
if (a[i] < 0) {pos = a[i]; cnt++;}
return (cnt < 2);
}
void sub2(){
int sum = 0, suml = 0, am = 0;
bool kt = 0;
for (int i = 1; i <= n; i++){
if (a[i] < 0) {
kt = 1;
am = a[i];
}
if (!kt) suml += a[i];
sum += a[i];
}
if (k > 1)
cout << sum - am << '\n';
else cout << max(suml, max(sum, sum - suml - am));
}
void sub3(){
int ans = 0, res = 0;
for (int i = 1; i <= n; i++) {
res = max(0ll, res + a[i]);
ans = max(ans, res);
}
cout << ans << '\n';
}
//int pa[N], sz[N];
//int findpa(int u){
//
// if (u == pa[u]) return u;
// return pa[u] = findpa(pa[u]);
//}
//void Union(int a, int b){
//
// a = findpa(a);
// b = findpa(b);
// if (a == b) return;
// if (sz[a] < sz[b]) swap(a,b);
// pa[b] = a;
// sz[a] += sz[b];
// sum[a] += sum[b];
//}
//vector <ii> d;
//void Add(int u){
// d.pb({u, sum[u]});
//}
//void sub6(){
// for (int i = 1; i <= n; i++){
// pa[i] = i;
// sz[i] = 1;
// sum[i] = a[i];
// }
// int parduong, param;
// bool task = 0;
// if (a[1] >= 0) {parduong = 1; task = 1;}
// else param = 1;
// for (int i = 1; i <= n; i++){
// if (a[i] >= 0) {
// if (task)
// Union(parduong, i);
// else parduong = i;
// task = 1;
// }
// else {
// if (!task) Union(param, i);
// else param = i;
// task = 0;
// }
// }
// int curpa = findpa(1);
// Add(curpa);
// for (int i = 1; i <= n; i++){
// int u = findpa(i);
// if (u != curpa){
// curpa = u;
// Add(u);
// }
// }
// for (int i = 2; i < d.size(); i++){
// if (d[i].se > 0 && d[i-2].se > 0){
// g.pb({d[i].fi, d[i - 2].fi, d[i].se + d[i].se });
// }
// }
//}
pair<long double, int> dp[N + 1][2];
bool check(long double res) {
dp[0][0] = {0, 0}, dp[0][1] = {-inf, 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][1].first + a[i], dp[i - 1][1].second),
make_pair(dp[i - 1][0].first + a[i] - res, dp[i - 1][0].second + 1));
}
return max(dp[n][0], dp[n][1]).second >= k;
};
void solve(){
long double l = 0, r = 1e14;
while (l + 1 < r) {
int mid = (l + r) / 2;
if (check(mid)) {
l = mid;
} else {
r = mid;
}
}
long double res = l;
check(res);
cout << ((long long) round(res * k + max(dp[n][0], dp[n][1]).first));
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
#define nametask "PRESUFF"
if(fopen(nametask".inp", "r")) {
freopen(nametask".inp", "r", stdin);
freopen(nametask".out", "w", stdout);
}
init();
// if (ifsub1()) return sub1(), 0;
// if (ifsub2()) return sub2(), 0;
// if (k == 1) return sub3(), 0;
// if (k <= n && n <= 2000) return sub6(), 0;
solve();
return 0;
}
/// Duong Anh Kiet from Kien Giang ///
/*
6 1
-1 -2 -1 2 -1 2
*/
Compilation message
feast.cpp: In function 'int main()':
feast.cpp:158:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
158 | freopen(nametask".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:159:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
159 | freopen(nametask".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
266 ms |
21328 KB |
Output is correct |
2 |
Correct |
268 ms |
21324 KB |
Output is correct |
3 |
Correct |
269 ms |
21584 KB |
Output is correct |
4 |
Correct |
263 ms |
21328 KB |
Output is correct |
5 |
Correct |
275 ms |
21328 KB |
Output is correct |
6 |
Correct |
251 ms |
21328 KB |
Output is correct |
7 |
Correct |
257 ms |
21460 KB |
Output is correct |
8 |
Correct |
260 ms |
21468 KB |
Output is correct |
9 |
Correct |
260 ms |
21320 KB |
Output is correct |
10 |
Correct |
257 ms |
21496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
365 ms |
21328 KB |
Output is correct |
2 |
Correct |
361 ms |
21584 KB |
Output is correct |
3 |
Correct |
358 ms |
21328 KB |
Output is correct |
4 |
Correct |
296 ms |
21328 KB |
Output is correct |
5 |
Correct |
340 ms |
21328 KB |
Output is correct |
6 |
Correct |
352 ms |
21328 KB |
Output is correct |
7 |
Correct |
309 ms |
21584 KB |
Output is correct |
8 |
Correct |
323 ms |
21328 KB |
Output is correct |
9 |
Correct |
317 ms |
21328 KB |
Output is correct |
10 |
Correct |
289 ms |
21584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
392 ms |
21328 KB |
Output is correct |
2 |
Correct |
379 ms |
21528 KB |
Output is correct |
3 |
Correct |
389 ms |
21452 KB |
Output is correct |
4 |
Correct |
381 ms |
21328 KB |
Output is correct |
5 |
Correct |
437 ms |
21320 KB |
Output is correct |
6 |
Correct |
435 ms |
21568 KB |
Output is correct |
7 |
Correct |
417 ms |
21592 KB |
Output is correct |
8 |
Correct |
410 ms |
21328 KB |
Output is correct |
9 |
Correct |
416 ms |
21596 KB |
Output is correct |
10 |
Correct |
422 ms |
21584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
2 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
1 ms |
2384 KB |
Output is correct |
14 |
Correct |
1 ms |
2556 KB |
Output is correct |
15 |
Correct |
1 ms |
2384 KB |
Output is correct |
16 |
Correct |
1 ms |
2384 KB |
Output is correct |
17 |
Correct |
1 ms |
2384 KB |
Output is correct |
18 |
Correct |
1 ms |
2384 KB |
Output is correct |
19 |
Correct |
1 ms |
2384 KB |
Output is correct |
20 |
Correct |
1 ms |
2384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
2 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
1 ms |
2384 KB |
Output is correct |
14 |
Correct |
1 ms |
2556 KB |
Output is correct |
15 |
Correct |
1 ms |
2384 KB |
Output is correct |
16 |
Correct |
1 ms |
2384 KB |
Output is correct |
17 |
Correct |
1 ms |
2384 KB |
Output is correct |
18 |
Correct |
1 ms |
2384 KB |
Output is correct |
19 |
Correct |
1 ms |
2384 KB |
Output is correct |
20 |
Correct |
1 ms |
2384 KB |
Output is correct |
21 |
Correct |
3 ms |
2640 KB |
Output is correct |
22 |
Correct |
2 ms |
2640 KB |
Output is correct |
23 |
Correct |
2 ms |
2640 KB |
Output is correct |
24 |
Correct |
2 ms |
2808 KB |
Output is correct |
25 |
Correct |
2 ms |
2640 KB |
Output is correct |
26 |
Correct |
2 ms |
2640 KB |
Output is correct |
27 |
Correct |
2 ms |
2640 KB |
Output is correct |
28 |
Correct |
2 ms |
2640 KB |
Output is correct |
29 |
Correct |
2 ms |
2892 KB |
Output is correct |
30 |
Correct |
2 ms |
2640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
266 ms |
21328 KB |
Output is correct |
2 |
Correct |
268 ms |
21324 KB |
Output is correct |
3 |
Correct |
269 ms |
21584 KB |
Output is correct |
4 |
Correct |
263 ms |
21328 KB |
Output is correct |
5 |
Correct |
275 ms |
21328 KB |
Output is correct |
6 |
Correct |
251 ms |
21328 KB |
Output is correct |
7 |
Correct |
257 ms |
21460 KB |
Output is correct |
8 |
Correct |
260 ms |
21468 KB |
Output is correct |
9 |
Correct |
260 ms |
21320 KB |
Output is correct |
10 |
Correct |
257 ms |
21496 KB |
Output is correct |
11 |
Correct |
365 ms |
21328 KB |
Output is correct |
12 |
Correct |
361 ms |
21584 KB |
Output is correct |
13 |
Correct |
358 ms |
21328 KB |
Output is correct |
14 |
Correct |
296 ms |
21328 KB |
Output is correct |
15 |
Correct |
340 ms |
21328 KB |
Output is correct |
16 |
Correct |
352 ms |
21328 KB |
Output is correct |
17 |
Correct |
309 ms |
21584 KB |
Output is correct |
18 |
Correct |
323 ms |
21328 KB |
Output is correct |
19 |
Correct |
317 ms |
21328 KB |
Output is correct |
20 |
Correct |
289 ms |
21584 KB |
Output is correct |
21 |
Correct |
392 ms |
21328 KB |
Output is correct |
22 |
Correct |
379 ms |
21528 KB |
Output is correct |
23 |
Correct |
389 ms |
21452 KB |
Output is correct |
24 |
Correct |
381 ms |
21328 KB |
Output is correct |
25 |
Correct |
437 ms |
21320 KB |
Output is correct |
26 |
Correct |
435 ms |
21568 KB |
Output is correct |
27 |
Correct |
417 ms |
21592 KB |
Output is correct |
28 |
Correct |
410 ms |
21328 KB |
Output is correct |
29 |
Correct |
416 ms |
21596 KB |
Output is correct |
30 |
Correct |
422 ms |
21584 KB |
Output is correct |
31 |
Correct |
1 ms |
2384 KB |
Output is correct |
32 |
Correct |
1 ms |
2384 KB |
Output is correct |
33 |
Correct |
1 ms |
2384 KB |
Output is correct |
34 |
Correct |
1 ms |
2384 KB |
Output is correct |
35 |
Correct |
1 ms |
2384 KB |
Output is correct |
36 |
Correct |
1 ms |
2384 KB |
Output is correct |
37 |
Correct |
1 ms |
2384 KB |
Output is correct |
38 |
Correct |
1 ms |
2384 KB |
Output is correct |
39 |
Correct |
1 ms |
2384 KB |
Output is correct |
40 |
Correct |
1 ms |
2384 KB |
Output is correct |
41 |
Correct |
2 ms |
2384 KB |
Output is correct |
42 |
Correct |
1 ms |
2384 KB |
Output is correct |
43 |
Correct |
1 ms |
2384 KB |
Output is correct |
44 |
Correct |
1 ms |
2556 KB |
Output is correct |
45 |
Correct |
1 ms |
2384 KB |
Output is correct |
46 |
Correct |
1 ms |
2384 KB |
Output is correct |
47 |
Correct |
1 ms |
2384 KB |
Output is correct |
48 |
Correct |
1 ms |
2384 KB |
Output is correct |
49 |
Correct |
1 ms |
2384 KB |
Output is correct |
50 |
Correct |
1 ms |
2384 KB |
Output is correct |
51 |
Correct |
3 ms |
2640 KB |
Output is correct |
52 |
Correct |
2 ms |
2640 KB |
Output is correct |
53 |
Correct |
2 ms |
2640 KB |
Output is correct |
54 |
Correct |
2 ms |
2808 KB |
Output is correct |
55 |
Correct |
2 ms |
2640 KB |
Output is correct |
56 |
Correct |
2 ms |
2640 KB |
Output is correct |
57 |
Correct |
2 ms |
2640 KB |
Output is correct |
58 |
Correct |
2 ms |
2640 KB |
Output is correct |
59 |
Correct |
2 ms |
2892 KB |
Output is correct |
60 |
Correct |
2 ms |
2640 KB |
Output is correct |
61 |
Correct |
436 ms |
21476 KB |
Output is correct |
62 |
Correct |
412 ms |
23368 KB |
Output is correct |
63 |
Correct |
440 ms |
23112 KB |
Output is correct |
64 |
Correct |
437 ms |
23212 KB |
Output is correct |
65 |
Correct |
441 ms |
23368 KB |
Output is correct |
66 |
Correct |
423 ms |
21320 KB |
Output is correct |
67 |
Correct |
416 ms |
23116 KB |
Output is correct |
68 |
Correct |
397 ms |
23364 KB |
Output is correct |
69 |
Correct |
387 ms |
23368 KB |
Output is correct |
70 |
Correct |
391 ms |
23124 KB |
Output is correct |