Submission #330812

# Submission time Handle Problem Language Result Execution time Memory
330812 2020-11-26T16:14:42 Z two_sides Feast (NOI19_feast) C++17
4 / 100
341 ms 15980 KB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

struct block {
    ll val;
    block *prev, *next;

    block(ll val): val(val) {
        prev = next = nullptr;
    }
};

using blockpt = block*;

struct compare {
    bool operator () (const blockpt x,
    const blockpt &y) const {
        return abs(x->val) != abs(y->val) ?
        abs(x->val) < abs(y->val) : x < y;
    }
};

const int N = 3e5 + 5;

set <block*, compare> blocks;
int a[N]; block *b[N];

bool sign(const int &x) {
    return x <= 0 ? 0 : 1;
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n, k, m = 0, p = 0;
    cin >> n >> k;
    for (int i = 1; i <= n; cin >> a[i++]);
    for (int i = 1; i <= n; ) {
        int j = i; ll sum = 0;
        while (j <= n && sign(a[i])
        == sign(a[j])) sum += a[j++];
        if (sum > 0 || m) {
            b[m++] = new block(sum);
            blocks.insert(b[m - 1]);
        }
        i = j; p += sum > 0;
    }
    for (int i = 1; i < m; i++) {
        b[i]->prev = b[i - 1];
        b[i - 1]->next = b[i];
    }
    while (p > k && blocks.size()) {
        blockpt it = *blocks.begin();
        blocks.erase(it);
        if (it->val > 0) p--;
        if (it->prev) {
            blocks.erase(it->prev);
            if (it->prev->val > 0) p--;
            it->val += it->prev->val;
            it->prev = it->prev->prev;
        }
        if (it->next) {
            blocks.erase(it->next);
            if (it->next->val > 0) p--;
            it->val += it->next->val;
            it->next = it->next->next;
        }
        if (it->val > 0 ||
        (it->prev && it->next)) {
            if (it->val > 0) p++;
            blocks.insert(it);
        }
    }
    ll res = 0;
    for (auto it : blocks)
        res += max(0ll, it->val);
    cout << res << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 35 ms 3308 KB Output is correct
2 Correct 36 ms 3308 KB Output is correct
3 Correct 36 ms 3308 KB Output is correct
4 Correct 36 ms 3308 KB Output is correct
5 Correct 36 ms 3308 KB Output is correct
6 Correct 36 ms 3308 KB Output is correct
7 Correct 37 ms 3308 KB Output is correct
8 Correct 36 ms 3436 KB Output is correct
9 Correct 36 ms 3436 KB Output is correct
10 Correct 43 ms 3308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 24 ms 2668 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 341 ms 15980 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 35 ms 3308 KB Output is correct
2 Correct 36 ms 3308 KB Output is correct
3 Correct 36 ms 3308 KB Output is correct
4 Correct 36 ms 3308 KB Output is correct
5 Correct 36 ms 3308 KB Output is correct
6 Correct 36 ms 3308 KB Output is correct
7 Correct 37 ms 3308 KB Output is correct
8 Correct 36 ms 3436 KB Output is correct
9 Correct 36 ms 3436 KB Output is correct
10 Correct 43 ms 3308 KB Output is correct
11 Incorrect 24 ms 2668 KB Output isn't correct
12 Halted 0 ms 0 KB -