답안 #1093777

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1093777 2024-09-27T12:05:06 Z muhammad Feast (NOI19_feast) C++17
100 / 100
67 ms 19268 KB
#include <bits/stdc++.h>
using namespace std;
#define long long long
const long MOD = 1e9+7, LINF = 1e18 + 1e16;
const int INF = 1e9+1;
const double EPS = 1e-10;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
const int N = 3e5+1;

class Feast {

    int n, k, pre[N], nxt[N];
    long ans, a[N], s[N], res[N];
    priority_queue<tuple<long, int, int> > pq;
    bool mark[N];

public:

    void solve(istream &cin, ostream &cout) {
        auto input = [&] () {
            cin >> n >> k;
            vector<int> org(n);
            for(int i = 0; i < n; i++) cin >> org[i];
            int st = 0, en = n-1;
            while(st < n && org[st] <= 0) ++st;
            while(en >= 0 && org[en] <= 0) --en;
            if(st > en) {
                return false;
            }
            n = 0;
            for(int i = st; i <= en; i++) {
                a[++n] = org[i];
            }
            for(int i = 1; i <= n; i++) {
                s[i] = s[i-1] + a[i];
            }
            return true;
        };
        if(!input()) {
            cout << 0 << endl;
            return;
        }

        pre[0] = nxt[n] = INF;
        int last = 0;
        for(int i = 1; i <= n; i++) {
            if(i > 1 && (a[i] > 0) != (a[i-1] > 0)) {
                if(a[i-1] > 0) --k;
                res[i-1] = -abs(s[i-1] - s[last]);
                pre[i-1] = last;
                nxt[last] = i-1;
                pq.emplace(res[i-1], last, i-1);
                last = i-1;
            }
        }
        --k;
        res[n] = -abs(s[n] - s[last]);
        pre[n] = last;
        nxt[last] = n;
        pq.emplace(res[n], last, n);

        while(!pq.empty() && k < 0) {
            long cost;
            int l, r;
            tie(cost, l, r) = pq.top();
            pq.pop();

            if(mark[r] || mark[l] || cost != res[r]) continue;
            ++k;
            ans += res[r];
            mark[r] = mark[l] = true; // need to set mark for both l, r, as we need to prevent segments from being adjacent to each other

            int ll = pre[l], rr = nxt[r];
            if(ll < rr && ll >= 0 && rr <= n) {
                pre[rr] = ll, nxt[ll] = rr;
                res[rr] = res[rr] + res[l] - res[r];
                res[r] = -LINF;
            }

            if(ll < rr && ll >= 0 && rr <= n && !mark[ll] && !mark[rr]) {
                pq.emplace(res[rr], ll, rr);
            }
        }

        for(int i = 1; i <= n; i++) if(a[i] > 0) ans += a[i];

        cout << ans << endl;
    }
};


class Solver {
public:
    void solve(std::istream& in, std::ostream& out) {
        Feast *obj = new Feast();
        obj->solve(in, out);
        delete obj;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    Solver solver;
    std::istream& in(std::cin);
    std::ostream& out(std::cout);
    solver.solve(in, out);
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 13904 KB Output is correct
2 Correct 27 ms 13912 KB Output is correct
3 Correct 25 ms 14164 KB Output is correct
4 Correct 27 ms 13904 KB Output is correct
5 Correct 25 ms 13904 KB Output is correct
6 Correct 25 ms 13916 KB Output is correct
7 Correct 31 ms 13904 KB Output is correct
8 Correct 24 ms 13904 KB Output is correct
9 Correct 36 ms 13904 KB Output is correct
10 Correct 24 ms 13920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 12316 KB Output is correct
2 Correct 20 ms 12476 KB Output is correct
3 Correct 23 ms 12184 KB Output is correct
4 Correct 18 ms 12380 KB Output is correct
5 Correct 26 ms 13904 KB Output is correct
6 Correct 17 ms 12380 KB Output is correct
7 Correct 17 ms 12380 KB Output is correct
8 Correct 25 ms 13996 KB Output is correct
9 Correct 24 ms 13912 KB Output is correct
10 Correct 18 ms 12380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 67 ms 19148 KB Output is correct
2 Correct 61 ms 19168 KB Output is correct
3 Correct 63 ms 19148 KB Output is correct
4 Correct 62 ms 19164 KB Output is correct
5 Correct 64 ms 19152 KB Output is correct
6 Correct 66 ms 19228 KB Output is correct
7 Correct 63 ms 19136 KB Output is correct
8 Correct 67 ms 19152 KB Output is correct
9 Correct 63 ms 19136 KB Output is correct
10 Correct 67 ms 19140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 10052 KB Output is correct
2 Correct 5 ms 10076 KB Output is correct
3 Correct 7 ms 10076 KB Output is correct
4 Correct 4 ms 10076 KB Output is correct
5 Correct 5 ms 10072 KB Output is correct
6 Correct 4 ms 10328 KB Output is correct
7 Correct 5 ms 10076 KB Output is correct
8 Correct 4 ms 10076 KB Output is correct
9 Correct 4 ms 10076 KB Output is correct
10 Correct 4 ms 9980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 10052 KB Output is correct
2 Correct 5 ms 10076 KB Output is correct
3 Correct 7 ms 10076 KB Output is correct
4 Correct 4 ms 10076 KB Output is correct
5 Correct 5 ms 10072 KB Output is correct
6 Correct 4 ms 10328 KB Output is correct
7 Correct 5 ms 10076 KB Output is correct
8 Correct 4 ms 10076 KB Output is correct
9 Correct 4 ms 10076 KB Output is correct
10 Correct 4 ms 9980 KB Output is correct
11 Correct 5 ms 10072 KB Output is correct
12 Correct 4 ms 10328 KB Output is correct
13 Correct 4 ms 10076 KB Output is correct
14 Correct 4 ms 10076 KB Output is correct
15 Correct 4 ms 10012 KB Output is correct
16 Correct 4 ms 10108 KB Output is correct
17 Correct 4 ms 10076 KB Output is correct
18 Correct 4 ms 10124 KB Output is correct
19 Correct 4 ms 10072 KB Output is correct
20 Correct 4 ms 10072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 10052 KB Output is correct
2 Correct 5 ms 10076 KB Output is correct
3 Correct 7 ms 10076 KB Output is correct
4 Correct 4 ms 10076 KB Output is correct
5 Correct 5 ms 10072 KB Output is correct
6 Correct 4 ms 10328 KB Output is correct
7 Correct 5 ms 10076 KB Output is correct
8 Correct 4 ms 10076 KB Output is correct
9 Correct 4 ms 10076 KB Output is correct
10 Correct 4 ms 9980 KB Output is correct
11 Correct 5 ms 10072 KB Output is correct
12 Correct 4 ms 10328 KB Output is correct
13 Correct 4 ms 10076 KB Output is correct
14 Correct 4 ms 10076 KB Output is correct
15 Correct 4 ms 10012 KB Output is correct
16 Correct 4 ms 10108 KB Output is correct
17 Correct 4 ms 10076 KB Output is correct
18 Correct 4 ms 10124 KB Output is correct
19 Correct 4 ms 10072 KB Output is correct
20 Correct 4 ms 10072 KB Output is correct
21 Correct 5 ms 10076 KB Output is correct
22 Correct 7 ms 10168 KB Output is correct
23 Correct 6 ms 10076 KB Output is correct
24 Correct 4 ms 10076 KB Output is correct
25 Correct 4 ms 10076 KB Output is correct
26 Correct 5 ms 10076 KB Output is correct
27 Correct 4 ms 10056 KB Output is correct
28 Correct 4 ms 10076 KB Output is correct
29 Correct 5 ms 10076 KB Output is correct
30 Correct 4 ms 10192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 13904 KB Output is correct
2 Correct 27 ms 13912 KB Output is correct
3 Correct 25 ms 14164 KB Output is correct
4 Correct 27 ms 13904 KB Output is correct
5 Correct 25 ms 13904 KB Output is correct
6 Correct 25 ms 13916 KB Output is correct
7 Correct 31 ms 13904 KB Output is correct
8 Correct 24 ms 13904 KB Output is correct
9 Correct 36 ms 13904 KB Output is correct
10 Correct 24 ms 13920 KB Output is correct
11 Correct 17 ms 12316 KB Output is correct
12 Correct 20 ms 12476 KB Output is correct
13 Correct 23 ms 12184 KB Output is correct
14 Correct 18 ms 12380 KB Output is correct
15 Correct 26 ms 13904 KB Output is correct
16 Correct 17 ms 12380 KB Output is correct
17 Correct 17 ms 12380 KB Output is correct
18 Correct 25 ms 13996 KB Output is correct
19 Correct 24 ms 13912 KB Output is correct
20 Correct 18 ms 12380 KB Output is correct
21 Correct 67 ms 19148 KB Output is correct
22 Correct 61 ms 19168 KB Output is correct
23 Correct 63 ms 19148 KB Output is correct
24 Correct 62 ms 19164 KB Output is correct
25 Correct 64 ms 19152 KB Output is correct
26 Correct 66 ms 19228 KB Output is correct
27 Correct 63 ms 19136 KB Output is correct
28 Correct 67 ms 19152 KB Output is correct
29 Correct 63 ms 19136 KB Output is correct
30 Correct 67 ms 19140 KB Output is correct
31 Correct 5 ms 10052 KB Output is correct
32 Correct 5 ms 10076 KB Output is correct
33 Correct 7 ms 10076 KB Output is correct
34 Correct 4 ms 10076 KB Output is correct
35 Correct 5 ms 10072 KB Output is correct
36 Correct 4 ms 10328 KB Output is correct
37 Correct 5 ms 10076 KB Output is correct
38 Correct 4 ms 10076 KB Output is correct
39 Correct 4 ms 10076 KB Output is correct
40 Correct 4 ms 9980 KB Output is correct
41 Correct 5 ms 10072 KB Output is correct
42 Correct 4 ms 10328 KB Output is correct
43 Correct 4 ms 10076 KB Output is correct
44 Correct 4 ms 10076 KB Output is correct
45 Correct 4 ms 10012 KB Output is correct
46 Correct 4 ms 10108 KB Output is correct
47 Correct 4 ms 10076 KB Output is correct
48 Correct 4 ms 10124 KB Output is correct
49 Correct 4 ms 10072 KB Output is correct
50 Correct 4 ms 10072 KB Output is correct
51 Correct 5 ms 10076 KB Output is correct
52 Correct 7 ms 10168 KB Output is correct
53 Correct 6 ms 10076 KB Output is correct
54 Correct 4 ms 10076 KB Output is correct
55 Correct 4 ms 10076 KB Output is correct
56 Correct 5 ms 10076 KB Output is correct
57 Correct 4 ms 10056 KB Output is correct
58 Correct 4 ms 10076 KB Output is correct
59 Correct 5 ms 10076 KB Output is correct
60 Correct 4 ms 10192 KB Output is correct
61 Correct 41 ms 19160 KB Output is correct
62 Correct 34 ms 19136 KB Output is correct
63 Correct 58 ms 19096 KB Output is correct
64 Correct 38 ms 19096 KB Output is correct
65 Correct 43 ms 19216 KB Output is correct
66 Correct 54 ms 19148 KB Output is correct
67 Correct 44 ms 19236 KB Output is correct
68 Correct 63 ms 19148 KB Output is correct
69 Correct 63 ms 19172 KB Output is correct
70 Correct 60 ms 19268 KB Output is correct