답안 #952547

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
952547 2024-03-24T08:23:19 Z ParsaGolestani Sparklers (JOI17_sparklers) C++17
100 / 100
366 ms 11292 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int N = 100'000;
const ll INF = 10'000'000'000;

ll n, k, t, s, x[N + 10];
stack<ll> a, b;

void readInput() {
    cin >> n >> k >> t;
    for (int i = 1; i <= n; i++)
        cin >> x[i];
}

stack<ll> empSt() {
    stack<ll> st;
    return st;
}

stack<ll> revSt(stack<ll> st) {
    stack<ll> res;
    while (st.size()) {
        res.push(-st.top());
        st.pop();
    }
    return res;
}

void calcA() {
    a = empSt();
    a.push(min(INF, t * s));
    for (int i = 2; i <= k; i++) {
        a.push(x[i - 1] - x[i]);
        a.push(min(INF, t * s));
    }
}

void calcB() {
    b = empSt();
    for (int i = n; i >= k + 1; i--) {
        b.push(min(INF, t * s));
        b.push(x[i - 1] - x[i]);
    }
}

void calcStack() {
    calcA();
    calcB();
}

pair<stack<ll>, stack<pair<ll, ll>>> compress(stack<ll> st) {
    stack<pair<ll, ll>> reverseRes, res;
    stack<ll> del;
    ll sum = 0, mn = 0;
    while (st.size()) {
        sum += st.top();
        mn = min(mn, sum);
        del.push(st.top());
        st.pop();
        if (sum >= 0) {
            reverseRes.push(make_pair(sum, mn));
            del = empSt();
            sum = mn = 0;
        }
    }
    while (del.size()) {
        st.push(del.top());
        del.pop();
    }
    while (reverseRes.size()) {
        res.push(reverseRes.top());
        reverseRes.pop();
    }
    return make_pair(st, res);
}

pair<ll, pair<stack<ll>, stack<ll>>> step(ll val, stack<ll> st1, stack<ll> st2) {
    auto p1 = compress(st1), p2 = compress(st2);
    stack<ll> resStack[2] = {p1.first, p2.first};
    stack<pair<ll, ll>> cmp[2] = {p1.second, p2.second};
    while (cmp[0].size() || cmp[1].size()) {
        bool ok = false;
        for (int id = 0; id < 2; id++)
            if (cmp[id].size() && val + cmp[id].top().second >= 0) {
                val += cmp[id].top().first;
                cmp[id].pop();
                ok = true;
            }
        if (!ok)
            return make_pair(-1ll, make_pair(a, b));
    }
    return make_pair(val, make_pair(resStack[0], resStack[1]));
}

ll calcSum() {
    return n * min(INF, t * s) - x[n];
}

bool check(ll mid) {
    s = mid;
    calcStack();
    auto x = step(0, a, b);
    if (x.first == -1)
        return false;
    return step(calcSum(), revSt(x.second.first), revSt(x.second.second)).first != -1;
}

void solve() {
    ll l = -1, r = INF;
    while (r - l > 1) {
        ll mid = (l + r) >> 1;
        if (check(mid + mid))
            r = mid;
        else
            l = mid;
    }
    cout << r << flush;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    readInput();
    solve();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 456 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 456 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 3 ms 344 KB Output is correct
23 Correct 3 ms 348 KB Output is correct
24 Correct 2 ms 348 KB Output is correct
25 Correct 2 ms 344 KB Output is correct
26 Correct 2 ms 348 KB Output is correct
27 Correct 2 ms 484 KB Output is correct
28 Correct 2 ms 348 KB Output is correct
29 Correct 2 ms 488 KB Output is correct
30 Correct 2 ms 348 KB Output is correct
31 Correct 3 ms 348 KB Output is correct
32 Correct 2 ms 348 KB Output is correct
33 Correct 2 ms 348 KB Output is correct
34 Correct 2 ms 488 KB Output is correct
35 Correct 2 ms 348 KB Output is correct
36 Correct 3 ms 344 KB Output is correct
37 Correct 2 ms 348 KB Output is correct
38 Correct 2 ms 348 KB Output is correct
39 Correct 2 ms 348 KB Output is correct
40 Correct 4 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 456 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 3 ms 344 KB Output is correct
23 Correct 3 ms 348 KB Output is correct
24 Correct 2 ms 348 KB Output is correct
25 Correct 2 ms 344 KB Output is correct
26 Correct 2 ms 348 KB Output is correct
27 Correct 2 ms 484 KB Output is correct
28 Correct 2 ms 348 KB Output is correct
29 Correct 2 ms 488 KB Output is correct
30 Correct 2 ms 348 KB Output is correct
31 Correct 3 ms 348 KB Output is correct
32 Correct 2 ms 348 KB Output is correct
33 Correct 2 ms 348 KB Output is correct
34 Correct 2 ms 488 KB Output is correct
35 Correct 2 ms 348 KB Output is correct
36 Correct 3 ms 344 KB Output is correct
37 Correct 2 ms 348 KB Output is correct
38 Correct 2 ms 348 KB Output is correct
39 Correct 2 ms 348 KB Output is correct
40 Correct 4 ms 348 KB Output is correct
41 Correct 238 ms 7724 KB Output is correct
42 Correct 5 ms 600 KB Output is correct
43 Correct 58 ms 2520 KB Output is correct
44 Correct 177 ms 10428 KB Output is correct
45 Correct 165 ms 10300 KB Output is correct
46 Correct 192 ms 10416 KB Output is correct
47 Correct 188 ms 10312 KB Output is correct
48 Correct 169 ms 10360 KB Output is correct
49 Correct 191 ms 10804 KB Output is correct
50 Correct 179 ms 10228 KB Output is correct
51 Correct 181 ms 8796 KB Output is correct
52 Correct 194 ms 10856 KB Output is correct
53 Correct 167 ms 8792 KB Output is correct
54 Correct 209 ms 9708 KB Output is correct
55 Correct 188 ms 10472 KB Output is correct
56 Correct 214 ms 10328 KB Output is correct
57 Correct 172 ms 10348 KB Output is correct
58 Correct 238 ms 10360 KB Output is correct
59 Correct 366 ms 11292 KB Output is correct
60 Correct 184 ms 10640 KB Output is correct
61 Correct 186 ms 10360 KB Output is correct
62 Correct 177 ms 10436 KB Output is correct
63 Correct 185 ms 10444 KB Output is correct
64 Correct 168 ms 10356 KB Output is correct
65 Correct 173 ms 10440 KB Output is correct
66 Correct 167 ms 10252 KB Output is correct
67 Correct 167 ms 10428 KB Output is correct
68 Correct 187 ms 10424 KB Output is correct
69 Correct 175 ms 10404 KB Output is correct