Submission #1055719

# Submission time Handle Problem Language Result Execution time Memory
1055719 2024-08-13T04:27:36 Z GrindMachine Sparklers (JOI17_sparklers) C++17
50 / 100
107 ms 262144 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
 
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
 
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)
 
template<typename T>
void amin(T &a, T b) {
    a = min(a,b);
}
 
template<typename T>
void amax(T &a, T b) {
    a = max(a,b);
}
 
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
 
/*
 
refs:
edi
stress tested to figure out the bug (ans = 0 case was bugged)
ac code used for stress testing: https://oj.uz/submission/1043916
 
*/
 
const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

template<typename T>
struct segtree {
    // https://codeforces.com/blog/entry/18051

    /*=======================================================*/

    struct data {
        ll mn,mx;
    };

    data neutral = {inf2,-inf2};

    data merge(data &left, data &right) {
        data curr;
        curr.mn = min(left.mn,right.mn);
        curr.mx = max(left.mx,right.mx);
        return curr;
    }

    void create(int i, T v) {
        tr[i] = {v,v};
    }

    void modify(int i, T v) {
        tr[i] = {v,v};
    }

    /*=======================================================*/

    int n;
    vector<data> tr;

    segtree() {

    }

    segtree(int siz) {
        init(siz);
    }

    void init(int siz) {
        n = siz;
        tr.assign(2 * n, neutral);
    }

    void build(vector<T> &a, int siz) {
        rep(i, siz) create(i + n, a[i]);
        rev(i, n - 1, 1) tr[i] = merge(tr[i << 1], tr[i << 1 | 1]);
    }

    void pupd(int i, T v) {
        modify(i + n, v);
        for (i = (i + n) >> 1; i; i >>= 1) tr[i] = merge(tr[i << 1], tr[i << 1 | 1]);
    }

    data query(int l, int r) {
        data resl = neutral, resr = neutral;

        for (l += n, r += n; l <= r; l >>= 1, r >>= 1) {
            if (l & 1) resl = merge(resl, tr[l++]);
            if (!(r & 1)) resr = merge(tr[r--], resr);
        }

        return merge(resl, resr);
    }
};

void solve(int test_case)
{
    ll n,k,t; cin >> n >> k >> t;
    vector<ll> a(n+5);
    rep1(i,n) cin >> a[i];
    vector<ll> b(n+5);
    segtree<ll> st(n+5);

    auto ok = [&](ll s){
        bool dp[n+5][n+5];
        memset(dp,0,sizeof dp);
        dp[k][k] = 1;
        ll mul = 2*t*s;

        vector<ll> vals;
        rep1(i,n){
            __int128 val = (__int128)i*mul-a[i];
            vals.pb(val);
        }

        sort(all(vals));
        vals.resize(unique(all(vals))-vals.begin());

        rep1(i,n){
            __int128 val = (__int128)i*mul-a[i];
            b[i] = lower_bound(all(vals),val)-vals.begin();        
        }

        st.build(b,n+1);
    
        vector<ll> lv;
        lv.pb(k);
        rev(i,k-1,1){
            if(b[i] < b[lv.back()]){
                lv.pb(i);
            }
        }

        vector<ll> rv;
        rv.pb(k);
        for(int i = k+1; i <= n; ++i){
            if(b[i] > b[rv.back()]){
                rv.pb(i);
            }
        }

        ll ptr1 = 0, ptr2 = 0;

        while(ptr1+1 < sz(lv) and ptr2+1 < sz(rv)){
            // move left
            if(st.query(lv[ptr1+1],lv[ptr1]).mx <= b[rv[ptr2]]){
                ptr1++;
            }
            // move right
            else{
                if(st.query(rv[ptr2],rv[ptr2+1]).mn >= b[lv[ptr1]]){
                    ptr2++;
                }
                else{
                    return false;
                }
            }            
        }

        while(ptr1+1 < sz(lv)){
            // move left
            if(st.query(lv[ptr1+1],lv[ptr1]).mx <= b[rv[ptr2]]){
                ptr1++;
            }
            else{
                return false;
            }
        }

        while(ptr2+1 < sz(rv)){
            // move right
            if(st.query(rv[ptr2],rv[ptr2+1]).mn >= b[lv[ptr1]]){
                ptr2++;
            }
            else{
                return false;
            }
        }

        int lstop = lv.back(), rstop = rv.back();
        lv.clear(), rv.clear();
        lv.pb(1);
        for(int i = 2; i <= lstop; ++i){
            if(b[i] <= b[lv.back()]){
                lv.pb(i);
            }
        }
        rv.pb(n);
        rev(i,n-1,rstop){
            if(b[i] >= b[rv.back()]){
                rv.pb(i);
            }
        }

        ptr1 = 0, ptr2 = 0;

        while(ptr1+1 < sz(lv) and ptr2+1 < sz(rv)){
            // move left
            if(st.query(lv[ptr1],lv[ptr1+1]).mx <= b[rv[ptr2]]){
                ptr1++;
            }
            // move right
            else{
                if(st.query(rv[ptr2+1],rv[ptr2]).mn >= b[lv[ptr1]]){
                    ptr2++;
                }
                else{
                    return false;
                }
            }            
        }

        while(ptr1+1 < sz(lv)){
            // move left
            if(st.query(lv[ptr1],lv[ptr1+1]).mx <= b[rv[ptr2]]){
                ptr1++;
            }
            else{
                return false;
            }
        }

        while(ptr2+1 < sz(rv)){
            // move right
            if(st.query(rv[ptr2+1],rv[ptr2]).mn >= b[lv[ptr1]]){
                ptr2++;
            }
            else{
                return false;
            }
        }

        return true;

        /*

        rev(l,n,1){
            for(int r = l; r <= n; ++r){
                ll len = r-l+1;
                if(!dp[l][r]) conts;
 
                // l --> l-1
                if(l-1){
                    if(b[l-1] <= b[r]){
                        dp[l-1][r] = 1;
                    }
                }
 
                // r --> r+1
                if(r+1 <= n){
                    if(b[r+1] >= b[l]){
                        dp[l][r+1] = 1;
                    }
                }
            }
        }
 
        return dp[1][n];

        */
    };

    ll lo = 0, hi = inf1;
    ll ans = -1;
 
    while(lo <= hi){
        ll mid = (lo+hi) >> 1;
        if(ok(mid)){
            ans = mid;
            hi = mid-1;
        }
        else{
            lo = mid+1;
        }
    }
 
    cout << ans << endl;
}
 
int main()
{
    fastio;
 
    int t = 1;
    // cin >> t;
 
    rep1(i, t) {
        solve(i);
    }
 
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 456 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 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 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 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 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 456 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 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 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 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 0 ms 348 KB Output is correct
22 Correct 2 ms 920 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 3 ms 1372 KB Output is correct
26 Correct 3 ms 1372 KB Output is correct
27 Correct 4 ms 1372 KB Output is correct
28 Correct 3 ms 1372 KB Output is correct
29 Correct 3 ms 1524 KB Output is correct
30 Correct 2 ms 1372 KB Output is correct
31 Correct 2 ms 1372 KB Output is correct
32 Correct 3 ms 1372 KB Output is correct
33 Correct 3 ms 1528 KB Output is correct
34 Correct 3 ms 1372 KB Output is correct
35 Correct 3 ms 1372 KB Output is correct
36 Correct 3 ms 1372 KB Output is correct
37 Correct 2 ms 1372 KB Output is correct
38 Correct 3 ms 1448 KB Output is correct
39 Correct 3 ms 1372 KB Output is correct
40 Correct 2 ms 1372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 456 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 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 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 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 0 ms 348 KB Output is correct
22 Correct 2 ms 920 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 3 ms 1372 KB Output is correct
26 Correct 3 ms 1372 KB Output is correct
27 Correct 4 ms 1372 KB Output is correct
28 Correct 3 ms 1372 KB Output is correct
29 Correct 3 ms 1524 KB Output is correct
30 Correct 2 ms 1372 KB Output is correct
31 Correct 2 ms 1372 KB Output is correct
32 Correct 3 ms 1372 KB Output is correct
33 Correct 3 ms 1528 KB Output is correct
34 Correct 3 ms 1372 KB Output is correct
35 Correct 3 ms 1372 KB Output is correct
36 Correct 3 ms 1372 KB Output is correct
37 Correct 2 ms 1372 KB Output is correct
38 Correct 3 ms 1448 KB Output is correct
39 Correct 3 ms 1372 KB Output is correct
40 Correct 2 ms 1372 KB Output is correct
41 Runtime error 107 ms 262144 KB Execution killed with signal 9
42 Halted 0 ms 0 KB -