Submission #126352

# Submission time Handle Problem Language Result Execution time Memory
126352 2019-07-07T12:53:10 Z choikiwon Sparklers (JOI17_sparklers) C++17
100 / 100
89 ms 3088 KB
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

const int MN = 100010;

int N, K, T;
int X[MN];

bool check(int x) {
    vector<ll> le, ri;
    for(int i = 0; i < K; i++) {
        ll d = 1LL * T * x - (X[i + 1] - X[i]);
        le.push_back(d);
    }
    reverse(le.begin(), le.end());

    for(int i = K; i < N - 1; i++) {
        ll d = 1LL * T * x - (X[i + 1] - X[i]);
        ri.push_back(d);
    }
    
    vector<ll> tmp;

    ll sum = 0;
    for(int i = 0; i < le.size(); i++) {
        sum += le[i];
        if(i == (int)le.size() - 1 || ((le[i] >= 0) ^ (le[i + 1] >= 0))) tmp.push_back(sum), sum = 0;
    }

    le = tmp;
    tmp.clear();

    sum = 0;
    for(int i = 0; i < ri.size(); i++) {
        sum += ri[i];
        if(i == (int)ri.size() - 1 || ((ri[i] >= 0) ^ (ri[i + 1] >= 0))) tmp.push_back(sum), sum = 0;
    }

    ri = tmp;

    vector<pair<ll, ll> > L, R;

    ll init = 0;
    if(le.size() && le[0] >= 0) init += le[0], le.erase(le.begin());
    if(ri.size() && ri[0] >= 0) init += ri[0], ri.erase(ri.begin());
    if(le.size() % 2) le.push_back(0);
    if(ri.size() % 2) ri.push_back(0);

    for(int i = 0; i < le.size(); i += 2) {
        ll n = -le[i];
        ll p = le[i + 1];

        L.push_back({ n, n - p });

        while(L.size() >= 2 && L.back().second <= 0 && L[ (int)L.size() - 2 ].second > 0) {
            pair<ll, ll> d = L.back(); L.pop_back();
            pair<ll, ll> t = L.back(); L.pop_back();

            t.first = max(t.first, t.second + d.first);
            t.second += d.second;
            L.push_back(t);
        }
    }
    for(int i = 0; i < ri.size(); i += 2) {
        ll n = -ri[i];
        ll p = ri[i + 1];

        R.push_back({ n, n - p });

        while(R.size() >= 2 && R.back().second <= 0 && R[ (int)R.size() - 2 ].second > 0) {
            pair<ll, ll> d = R.back(); R.pop_back();
            pair<ll, ll> t = R.back(); R.pop_back();

            t.first = max(t.first, t.second + d.first);
            t.second += d.second;
            R.push_back(t);
        }
    }

    reverse(L.begin(), L.end());
    reverse(R.begin(), R.end());

    while(1) {
        if(L.size() && L.back().second <= 0 && init >= L.back().first) {
            init -= L.back().second, L.pop_back();
            continue;
        }
        if(R.size() && R.back().second <= 0 && init >= R.back().first) {
            init -= R.back().second, R.pop_back();
            continue;
        }
        break;
    }
    if(L.size() && L.back().second <= 0) return false;
    if(R.size() && R.back().second <= 0) return false;

    reverse(L.begin(), L.end());
    reverse(R.begin(), R.end());

    vector<ll> psum1(L.size());
    vector<ll> psum2(R.size());

    for(int i = 0; i < L.size(); i++) {
        psum1[i] = L[i].second;
        if(i) psum1[i] += psum1[i - 1];
    }
    for(int i = 0; i < R.size(); i++) {
        psum2[i] = R[i].second;
        if(i) psum2[i] += psum2[i - 1];
    }

    if(init < (psum1.size()? psum1.back() : 0) + (psum2.size()? psum2.back() : 0)) return false;

    vector<int> pnt1(L.size());
    vector<int> pnt2(R.size());

    for(int i = 0; i < L.size(); i++) {
        ll rem = i? init - psum1[i - 1] : init;
        if(rem < L[i].first) return false;
        rem -= L[i].first;

        int s = 0, e = (int)psum2.size() - 1, p = -1;
        while(s <= e) {
            int m = (s + e)>>1;

            if(psum2[m] <= rem) {
                p = m;
                s = m + 1;
            }
            else e = m - 1;
        }
        pnt1[i] = p;
    }

    for(int i = 0; i < R.size(); i++) {
        ll rem = i? init - psum2[i - 1] : init;
        if(rem < R[i].first) return false;
        rem -= R[i].first;

        int s = 0, e = (int)psum1.size() - 1, p = -1;
        while(s <= e) {
            int m = (s + e)>>1;

            if(psum1[m] <= rem) {
                p = m;
                s = m + 1;
            }
            else e = m - 1;
        }
        pnt2[i] = p;
    }

    int pos = (int)R.size() - 1;
    int mn1 = 1e9;
    int mn2 = 1e9;
    for(int i = (int)L.size() - 1; i >= 0; i--) {
        mn1 = min(mn1, pnt1[i]);
        while(pos >= 0 && pos > mn1) {
            mn2 = min(mn2, pnt2[pos]);
            pos--;
        }
        if(mn2 < i) return false;
    }
    return true;
}

int main() {
    scanf("%d %d %d", &N, &K, &T);
    K--;

    for(int i = 0; i < N; i++) {
        scanf("%d", &X[i]);
    }

    int s = 0, e = 1e9 / T + 1, p = -1;
    while(s <= e) {
        int m = (s + e)>>1;

        if(check(2 * m)) {
            p = m;
            e = m - 1;
        }
        else s = m + 1;
    }
    printf("%d", p);
}

Compilation message

sparklers.cpp: In function 'bool check(int)':
sparklers.cpp:27:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < le.size(); i++) {
                    ~~^~~~~~~~~~~
sparklers.cpp:36:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < ri.size(); i++) {
                    ~~^~~~~~~~~~~
sparklers.cpp:51:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < le.size(); i += 2) {
                    ~~^~~~~~~~~~~
sparklers.cpp:66:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < ri.size(); i += 2) {
                    ~~^~~~~~~~~~~
sparklers.cpp:105:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < L.size(); i++) {
                    ~~^~~~~~~~~~
sparklers.cpp:109:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < R.size(); i++) {
                    ~~^~~~~~~~~~
sparklers.cpp:119:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < L.size(); i++) {
                    ~~^~~~~~~~~~
sparklers.cpp:137:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < R.size(); i++) {
                    ~~^~~~~~~~~~
sparklers.cpp: In function 'int main()':
sparklers.cpp:170:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d %d", &N, &K, &T);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sparklers.cpp:174:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &X[i]);
         ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 380 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 252 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 2 ms 380 KB Output is correct
10 Correct 2 ms 252 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 256 KB Output is correct
13 Correct 2 ms 256 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 256 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 256 KB Output is correct
18 Correct 2 ms 256 KB Output is correct
19 Correct 2 ms 256 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 380 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 252 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 2 ms 380 KB Output is correct
10 Correct 2 ms 252 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 256 KB Output is correct
13 Correct 2 ms 256 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 256 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 256 KB Output is correct
18 Correct 2 ms 256 KB Output is correct
19 Correct 2 ms 256 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 256 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 3 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 2 ms 376 KB Output is correct
31 Correct 2 ms 376 KB Output is correct
32 Correct 2 ms 376 KB Output is correct
33 Correct 3 ms 376 KB Output is correct
34 Correct 3 ms 376 KB Output is correct
35 Correct 3 ms 376 KB Output is correct
36 Correct 2 ms 376 KB Output is correct
37 Correct 2 ms 380 KB Output is correct
38 Correct 3 ms 376 KB Output is correct
39 Correct 2 ms 376 KB Output is correct
40 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 380 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 252 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 2 ms 380 KB Output is correct
10 Correct 2 ms 252 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 256 KB Output is correct
13 Correct 2 ms 256 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 256 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 256 KB Output is correct
18 Correct 2 ms 256 KB Output is correct
19 Correct 2 ms 256 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 256 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 3 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 2 ms 376 KB Output is correct
31 Correct 2 ms 376 KB Output is correct
32 Correct 2 ms 376 KB Output is correct
33 Correct 3 ms 376 KB Output is correct
34 Correct 3 ms 376 KB Output is correct
35 Correct 3 ms 376 KB Output is correct
36 Correct 2 ms 376 KB Output is correct
37 Correct 2 ms 380 KB Output is correct
38 Correct 3 ms 376 KB Output is correct
39 Correct 2 ms 376 KB Output is correct
40 Correct 2 ms 376 KB Output is correct
41 Correct 35 ms 1508 KB Output is correct
42 Correct 3 ms 376 KB Output is correct
43 Correct 12 ms 696 KB Output is correct
44 Correct 85 ms 2800 KB Output is correct
45 Correct 78 ms 2292 KB Output is correct
46 Correct 85 ms 2632 KB Output is correct
47 Correct 81 ms 2848 KB Output is correct
48 Correct 76 ms 2340 KB Output is correct
49 Correct 88 ms 3088 KB Output is correct
50 Correct 76 ms 2376 KB Output is correct
51 Correct 86 ms 2808 KB Output is correct
52 Correct 89 ms 2700 KB Output is correct
53 Correct 88 ms 2860 KB Output is correct
54 Correct 75 ms 2904 KB Output is correct
55 Correct 77 ms 2304 KB Output is correct
56 Correct 88 ms 2748 KB Output is correct
57 Correct 86 ms 2944 KB Output is correct
58 Correct 77 ms 2400 KB Output is correct
59 Correct 49 ms 2052 KB Output is correct
60 Correct 64 ms 2408 KB Output is correct
61 Correct 66 ms 2448 KB Output is correct
62 Correct 69 ms 2596 KB Output is correct
63 Correct 63 ms 2324 KB Output is correct
64 Correct 58 ms 2080 KB Output is correct
65 Correct 59 ms 2084 KB Output is correct
66 Correct 58 ms 2044 KB Output is correct
67 Correct 57 ms 2076 KB Output is correct
68 Correct 64 ms 2400 KB Output is correct
69 Correct 65 ms 2404 KB Output is correct