# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
330795 |
2020-11-26T15:26:23 Z |
ThaiBaHung |
Feast (NOI19_feast) |
C++14 |
|
54 ms |
10620 KB |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 4;
int n, k;
int a[N];
typedef pair < long long, int > ii;
vector < ii > sum;
set < int > ms;
int L[N], R[N];
long long to[N];
int cs[N];
bool vs[N];
int main()
{
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("t.inp","r",stdin); freopen("t.out","w",stdout);
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> a[i], to[i] = to[i - 1] + a[i];
long long cur = 0;
int dem = 0;
a[0] = -2e9;
for (int i = 1; i <= n; i++)
{
if (a[i] < 0)
{
if (a[i - 1] >= 0) cs[i - 1] = dem, R[dem] = i - 1, sum.push_back({cur, dem});
cur = 0;
continue;
}
else
{
if (a[i - 1] < 0)
{
cur = a[i];
dem++;
L[dem] = i;
cs[i] = dem;
}
else
{
cur += a[i];
}
}
}
if (cur > 0) sum.push_back({cur, dem}), R[dem] = n;
sort(sum.begin(), sum.end(), greater < ii > ());
long long res = 0;
for (int i = 0; i < min(k, int(sum.size())); i++)
res += sum[i].first, vs[sum[i].second] = true,
ms.insert(L[sum[i].second]),
ms.insert(R[sum[i].second]);
memset(vs, false, sizeof vs);
for (int i = k; i < sum.size(); i++)
{
int id = sum[i].second;
long long val = sum[i].first;
if (vs[id]) continue;
auto it_low = ms.lower_bound(R[id]);
auto it_hi = ms.lower_bound(L[id]);
long long cur = 0;
if (id > 1)
{
it_hi--;
cur = max(cur, val + to[L[id] - 1] - to[*it_hi]);
}
if (id < dem && it_low != ms.end())
cur = max(cur, val + to[*it_low - 1] - to[R[id]]);
//cout << id << " " << *it_hi << " " << *it_low << '\n';
if (id > 1 && cur == val + to[L[id] - 1] - to[*it_hi])
{
for (int t = cs[*it_hi]; t <= id; t++)
vs[t] = true;
}
else
if (id < dem && cur == val + to[*it_low - 1] - to[R[id]])
for (int t = id; t <= cs[*it_low]; t++)
vs[t] = true;
if (cur)
{
res += cur;
//cout << id << " " << cur << '\n';
ms.insert(L[id]);
ms.insert(R[id]);
}
}
cout << res;
}
Compilation message
feast.cpp: In function 'int main()':
feast.cpp:66:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for (int i = k; i < sum.size(); i++)
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
6892 KB |
Output is correct |
2 |
Correct |
37 ms |
7020 KB |
Output is correct |
3 |
Correct |
37 ms |
7020 KB |
Output is correct |
4 |
Correct |
46 ms |
7020 KB |
Output is correct |
5 |
Correct |
37 ms |
7020 KB |
Output is correct |
6 |
Correct |
40 ms |
6820 KB |
Output is correct |
7 |
Correct |
37 ms |
6892 KB |
Output is correct |
8 |
Correct |
37 ms |
7020 KB |
Output is correct |
9 |
Correct |
37 ms |
6892 KB |
Output is correct |
10 |
Correct |
36 ms |
6912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
5240 KB |
Output is correct |
2 |
Correct |
28 ms |
5356 KB |
Output is correct |
3 |
Correct |
30 ms |
5228 KB |
Output is correct |
4 |
Correct |
25 ms |
5228 KB |
Output is correct |
5 |
Correct |
38 ms |
6892 KB |
Output is correct |
6 |
Correct |
27 ms |
5228 KB |
Output is correct |
7 |
Correct |
26 ms |
5356 KB |
Output is correct |
8 |
Correct |
37 ms |
7020 KB |
Output is correct |
9 |
Correct |
38 ms |
6892 KB |
Output is correct |
10 |
Correct |
25 ms |
5228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
54 ms |
10620 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
620 KB |
Output is correct |
2 |
Correct |
1 ms |
620 KB |
Output is correct |
3 |
Incorrect |
1 ms |
620 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
620 KB |
Output is correct |
2 |
Correct |
1 ms |
620 KB |
Output is correct |
3 |
Incorrect |
1 ms |
620 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
620 KB |
Output is correct |
2 |
Correct |
1 ms |
620 KB |
Output is correct |
3 |
Incorrect |
1 ms |
620 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
6892 KB |
Output is correct |
2 |
Correct |
37 ms |
7020 KB |
Output is correct |
3 |
Correct |
37 ms |
7020 KB |
Output is correct |
4 |
Correct |
46 ms |
7020 KB |
Output is correct |
5 |
Correct |
37 ms |
7020 KB |
Output is correct |
6 |
Correct |
40 ms |
6820 KB |
Output is correct |
7 |
Correct |
37 ms |
6892 KB |
Output is correct |
8 |
Correct |
37 ms |
7020 KB |
Output is correct |
9 |
Correct |
37 ms |
6892 KB |
Output is correct |
10 |
Correct |
36 ms |
6912 KB |
Output is correct |
11 |
Correct |
25 ms |
5240 KB |
Output is correct |
12 |
Correct |
28 ms |
5356 KB |
Output is correct |
13 |
Correct |
30 ms |
5228 KB |
Output is correct |
14 |
Correct |
25 ms |
5228 KB |
Output is correct |
15 |
Correct |
38 ms |
6892 KB |
Output is correct |
16 |
Correct |
27 ms |
5228 KB |
Output is correct |
17 |
Correct |
26 ms |
5356 KB |
Output is correct |
18 |
Correct |
37 ms |
7020 KB |
Output is correct |
19 |
Correct |
38 ms |
6892 KB |
Output is correct |
20 |
Correct |
25 ms |
5228 KB |
Output is correct |
21 |
Incorrect |
54 ms |
10620 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |