Submission #126351

# Submission time Handle Problem Language Result Execution time Memory
126351 2019-07-07T12:52:21 Z choikiwon Sparklers (JOI17_sparklers) C++17
100 / 100
88 ms 3988 KB
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

const int MN = 100010;

void print(vector<ll> &a) {
    for(int i = 0; i < a.size(); i++) {
        cout << a[i] << ' ';
    }
    cout << endl;
}

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

bool check(int x) {

    //cout << "check : " << x << endl;

    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);
    }

    /*
    print(le);
    print(ri);
    //*/

    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;

    /*
    print(le);
    print(ri);
    //*/

    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());

    /*
    for(int i = 0; i < L.size(); i++) {
        cout << "(" << L[i].first << ", " << L[i].second << ") ";
    }
    cout << endl;
    for(int i = 0; i < R.size(); i++) {
        cout << "(" << R[i].first << ", " << R[i].second << ") ";
    }
    cout << endl;
    //*/

    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());

    //cout << init << endl;

    /*
    for(int i = 0; i < L.size(); i++) {
        cout << "(" << L[i].first << ", " << L[i].second << ") ";
    }
    cout << endl;
    for(int i = 0; i < R.size(); i++) {
        cout << "(" << R[i].first << ", " << R[i].second << ") ";
    }
    cout << endl;
    //*/

    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--;

    //N = 1000, K = 510, T = 1;

    for(int i = 0; i < N; i++) {
        scanf("%d", &X[i]);
        /*
        X[i] = rand() + rand();
        if(i) X[i] += X[i - 1];
        //*/
    }

    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 'void print(std::vector<long long int>&)':
sparklers.cpp:9:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < a.size(); i++) {
                    ~~^~~~~~~~~~
sparklers.cpp: In function 'bool check(int)':
sparklers.cpp:42:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < le.size(); i++) {
                    ~~^~~~~~~~~~~
sparklers.cpp:51:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < ri.size(); i++) {
                    ~~^~~~~~~~~~~
sparklers.cpp:71:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < le.size(); i += 2) {
                    ~~^~~~~~~~~~~
sparklers.cpp:86:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < ri.size(); i += 2) {
                    ~~^~~~~~~~~~~
sparklers.cpp:149:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < L.size(); i++) {
                    ~~^~~~~~~~~~
sparklers.cpp:153:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < R.size(); i++) {
                    ~~^~~~~~~~~~
sparklers.cpp:163:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < L.size(); i++) {
                    ~~^~~~~~~~~~
sparklers.cpp:181: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:214: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:220: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 504 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 380 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 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 376 KB Output is correct
14 Correct 2 ms 256 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 256 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 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 380 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 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 376 KB Output is correct
14 Correct 2 ms 256 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 256 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 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 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 3 ms 376 KB Output is correct
27 Correct 3 ms 376 KB Output is correct
28 Correct 3 ms 376 KB Output is correct
29 Correct 3 ms 376 KB Output is correct
30 Correct 3 ms 376 KB Output is correct
31 Correct 3 ms 376 KB Output is correct
32 Correct 3 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 3 ms 376 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 504 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 380 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 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 376 KB Output is correct
14 Correct 2 ms 256 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 256 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 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 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 3 ms 376 KB Output is correct
27 Correct 3 ms 376 KB Output is correct
28 Correct 3 ms 376 KB Output is correct
29 Correct 3 ms 376 KB Output is correct
30 Correct 3 ms 376 KB Output is correct
31 Correct 3 ms 376 KB Output is correct
32 Correct 3 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 3 ms 376 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 2212 KB Output is correct
42 Correct 4 ms 504 KB Output is correct
43 Correct 12 ms 888 KB Output is correct
44 Correct 86 ms 3716 KB Output is correct
45 Correct 77 ms 3132 KB Output is correct
46 Correct 86 ms 3656 KB Output is correct
47 Correct 81 ms 3720 KB Output is correct
48 Correct 80 ms 3308 KB Output is correct
49 Correct 87 ms 3988 KB Output is correct
50 Correct 77 ms 3224 KB Output is correct
51 Correct 87 ms 3728 KB Output is correct
52 Correct 85 ms 3704 KB Output is correct
53 Correct 85 ms 3752 KB Output is correct
54 Correct 75 ms 3672 KB Output is correct
55 Correct 76 ms 3168 KB Output is correct
56 Correct 87 ms 3692 KB Output is correct
57 Correct 88 ms 3904 KB Output is correct
58 Correct 76 ms 3300 KB Output is correct
59 Correct 49 ms 2248 KB Output is correct
60 Correct 65 ms 3404 KB Output is correct
61 Correct 63 ms 3252 KB Output is correct
62 Correct 71 ms 3520 KB Output is correct
63 Correct 64 ms 3316 KB Output is correct
64 Correct 58 ms 3040 KB Output is correct
65 Correct 59 ms 3048 KB Output is correct
66 Correct 58 ms 2980 KB Output is correct
67 Correct 57 ms 3036 KB Output is correct
68 Correct 67 ms 3336 KB Output is correct
69 Correct 64 ms 3360 KB Output is correct