QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#129377#6134. Soldier GameEnergy_is_not_over#AC ✓1479ms17056kbC++177.8kb2023-07-22 17:36:152023-07-22 17:36:16

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-22 17:36:16]
  • 评测
  • 测评结果:AC
  • 用时:1479ms
  • 内存:17056kb
  • [2023-07-22 17:36:15]
  • 提交

answer

//
// Created by Barichek on 22.07.2023 11:22:46
//

#include <bits/stdc++.h>

#define F first
#define S second
#define MP make_pair
#define PB push_back

#define all(a) a.begin(), a.end()
#define len(a) (int) (a.size())
#define mp make_pair
#define pb push_back
#define fir first
#define sec second

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;

#ifdef Energy_is_not_over
#define DEBUG for (bool ____DEBUG = true; ____DEBUG; ____DEBUG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl

template<class ...Ts>
auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }

#else
#define DEBUG while (false)
#define LOG(...)
#endif

const int max_n = 1e5+10, inf = 1000111222;

struct segment_tree {
    struct node{
        int node_size;
        bool can_merge_with_right;
        array<array<int,2>,2> dp;
    };

    int rem_a[max_n];
    node mx[4*max_n];

    node merge(node l,node r,int mid)
    {
        node res;
        res.node_size=l.node_size+r.node_size;
        res.can_merge_with_right=r.can_merge_with_right;
        for (int i=0;i<2;i++){
            for (int j=0;j<2;j++){
                res.dp[i][j]=2*inf;
            }
        }
        for (int l_i=0;l_i<2;l_i++){
            for (int l_j=0;l_j<2;l_j++){
//                LOG(l_i,l_j);
                if (l.dp[l_i][l_j]==2*inf){
                    continue;
                }
                for (int r_i=0;r_i<2;r_i++){
                    for (int r_j=0;r_j<2;r_j++){
                        if (r.dp[r_i][r_j]==2*inf){
                            continue;
                        }
//                        LOG(r_i,r_j);
                        if (l.node_size==1 || r.node_size==1){
                            for (int is_merge=0;is_merge<=1;is_merge++){
                                if (!l.can_merge_with_right && is_merge){
                                    continue;
                                }
                                if (l.node_size!=1 && l_j==0 && !is_merge){
                                    continue;
                                }
                                if (r.node_size!=1 && r_i==0 && !is_merge){
                                    continue;
                                }
                                if ((l_j==1 || r_i==1) && is_merge){
                                    continue;
                                }

                                int cur_value = max({l.dp[l_i][l_j],r.dp[r_i][r_j],is_merge==0?-2*inf:rem_a[mid]+rem_a[mid+1]});
                                int new_L=l_i,new_R=r_j;
                                if (is_merge && l.node_size==1){
                                    new_L=1;
                                }
                                if (is_merge && r.node_size==1){
                                    new_R=1;
                                }

                                LOG(new_L,new_R,cur_value,l_i,l_j,r_i,r_j,is_merge);
                                res.dp[new_L][new_R]=min(res.dp[new_L][new_R],cur_value);
                            }
                        }
                        else{
                            if (l_j==0 && !l.can_merge_with_right){
                                continue;
                            }
                            if (l_j!=r_i){
                                continue;
                            }

                            int cur_value = max({l.dp[l_i][l_j],r.dp[r_i][r_j],l_j==1?-2*inf:rem_a[mid]+rem_a[mid+1]});
                            int new_L=l_i,new_R=r_j;

//                            LOG(new_L,new_R);
                            res.dp[new_L][new_R]=min(res.dp[new_L][new_R],cur_value);
                        }
                    }
                }
            }
        }
        return res;
    }

    void build(int v, int l, int r, int a[]) {
        if (l == r) {
            rem_a[l]=a[l];

            mx[v].node_size=1;
            mx[v].can_merge_with_right=1;
            for (int i=0;i<2;i++){
                for (int j=0;j<2;j++){
                    mx[v].dp[i][j]=2*inf;
                }
            }
            mx[v].dp[0][0]=-2*inf;
            mx[v].dp[1][1]=a[l];
            return;
        }
        int mid = (l + r) / 2;
        build(2 * v, l, mid, a);
        build(2 * v + 1, mid + 1, r, a);
        mx[v] = merge(mx[2 * v], mx[2 * v + 1], mid);
    }

    void forbit_one_element(int v, int l, int r, int pos) {
        if (l == r) {
            mx[v].dp[1][1]=2*inf;
            return;
        }
        int mid = (l + r) / 2;
        LOG("previously",l,r,mx[v].dp[1][1]);
        if (pos <= mid) {
            forbit_one_element(2 * v, l, mid, pos);
        } else {
            forbit_one_element(2 * v + 1, mid + 1, r, pos);
        }
        mx[v] = merge(mx[2 * v], mx[2 * v + 1], mid);
        LOG("currently",l,r,mx[v].dp[1][1]);
    }

    void forbit_two_elements(int v, int l, int r, int pos) {
        if (l == r) {
            mx[v].can_merge_with_right=0;
            return;
        }
        int mid = (l + r) / 2;
        LOG("previously",l,r,mx[v].dp[1][1]);
        if (pos <= mid) {
            forbit_two_elements(2 * v, l, mid, pos);
        } else {
            forbit_two_elements(2 * v + 1, mid + 1, r, pos);
        }
        mx[v] = merge(mx[2 * v], mx[2 * v + 1], mid);
        LOG("currently",l,r,mx[v].dp[1][1]);
    }

    node get_max(int v, int tl, int tr, int l, int r) {
        if (tl == l && tr == r) {
            return mx[v];
        }
        int mid = (tl + tr) / 2;
        if (r <= mid) {
            return get_max(2 * v, tl, mid, l, r);
        } else if (l > mid) {
            return get_max(2 * v + 1, mid + 1, tr, l, r);
        }
        return merge(get_max(2 * v, tl, mid, l, mid), get_max(2 * v + 1, mid + 1, tr, mid + 1, r), mid);
    }
};

segment_tree st;

int a[max_n];

const bool gen=0;
mt19937 rnd(4747474);

void solve()
{
    int n;
    if (gen){
        n=1e5;
        for (int i=0;i<n;i++){
            a[i]=rnd()%(int(1e9));
        }
    }
    else{
        cin>>n;
        for (int i=0;i<n;i++){
            cin>>a[i];
        }
    }
    vector<pair<int,pair<int,bool>>> vh;
    for (int i=0;i<n;i++){
        vh.pb(mp(a[i],mp(i,0)));
        if (i+1<n){
            vh.pb(mp(a[i]+a[i+1],mp(i,1)));
        }
    }
    sort(all(vh));

    st.build(1,0,n-1,a);
    ll answer=1e18;
    for (auto qq:vh){
        LOG(qq.fir,qq.sec.fir,qq.sec.sec);
        if (qq.sec.sec==0){
            LOG("st.forbit_one_element",qq.sec.fir);
            st.forbit_one_element(1,0,n-1,qq.sec.fir);
        }
        else{
            LOG("st.forbit_two_elements",qq.sec.fir);
            st.forbit_two_elements(1,0,n-1,qq.sec.fir);
        }

        LOG("forbids finished");

        int base_sum=a[qq.sec.fir]+(qq.sec.sec?a[qq.sec.fir+1]:0);
        int ans=base_sum;
        int L=qq.sec.fir,R=qq.sec.fir+(qq.sec.sec?1:0);
        if (L!=0){
            LOG("st.get_max L");
            ans=max(ans,st.get_max(1,0,n-1,0,L-1).dp[1][1]);
            LOG("st.get_max L finished");
        }
        if (R!=n-1){
            LOG("st.get_max R");
            ans=max(ans,st.get_max(1,0,n-1,R+1,n-1).dp[1][1]);
            LOG("st.get_max R finished");
        }
        LOG(ans,base_sum);
        if (ans!=2*inf){
            answer=min(answer,ll(ans)-ll(base_sum));
        }
    }

    cout<<answer<<"\n";
}

int main() {
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int tests;
    if (gen){
        tests=10;
    }
    else{
        cin>>tests;
    }
    while (tests--){
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3596kb

input:

3
5
-1 4 2 1 1
4
1 3 2 4
1
7

output:

1
2
0

result:

ok 3 number(s): "1 2 0"

Test #2:

score: 0
Accepted
time: 1306ms
memory: 16884kb

input:

10010
1
1000000000
1
-1000000000
2
1000000000 -1000000000
4
1000000000 1000000000 -1000000000 -1000000000
3
100 -100 100
16
-17 91 -19 66 100 -70 -71 76 -58 99 52 19 25 -67 -63 -32
7
-95 -26 63 -55 -19 77 -100
17
-100 72 -53 -32 8 -100 53 44 -100 -65 -81 -59 100 100 57 -47 1
11
99 10 -100 3 32 2 -26...

output:

0
0
0
2000000000
100
135
103
181
189
84
63
164
176
0
147
135
152
36
200
131
134
0
136
0
72
171
146
0
183
77
176
89
200
135
38
109
119
126
158
189
70
0
38
999804364
188
161
0
116
116
200
0
101
200
39
0
183
139
0
183
107
139
0
178
85993
126
153
168
163
96
53
96
52
126
47
130
79
0
123
188
173
33
0
83
1...

result:

ok 10010 numbers

Test #3:

score: 0
Accepted
time: 180ms
memory: 14276kb

input:

1
100000
-999999999 999999999 999999998 -999999998 -999999997 999999997 999999996 -999999996 999999995 -999999995 -999999994 999999994 -999999993 999999993 -999999992 999999992 -999999991 999999991 999999990 -999999990 999999989 -999999989 999999988 -999999988 999999987 -999999987 999999986 -9999999...

output:

0

result:

ok 1 number(s): "0"

Test #4:

score: 0
Accepted
time: 1479ms
memory: 17056kb

input:

10011
1
1000000000
1
-1000000000
2
1000000000 -1000000000
4
1000000000 1000000000 -1000000000 -1000000000
12
48 54 98 -20 -45 56 -100 78 47 23 -100 -21
19
66 41 52 17 -9 -90 -36 90 -26 66 -86 -83 -39 -83 35 78 100 -68 -62
2
-100 -23
17
89 -26 -100 -38 -14 87 32 -100 16 -31 -35 100 73 -61 -100 43 -48...

output:

0
0
0
2000000000
155
168
0
173
137
167
127
25
91
109
176
0
0
173
115
56
66
67
0
1999775909
121
166
128
77
60
146
152
78
172
110
60
200
89
160
200
130
175
79
97
1999891177
122
154
136
164
123
0
175
77
167
76
40
82
79
159
99
141
165
147
158
1999730298
0
179
31
181
192
193
47
91
164
63
65
138
100
168
1...

result:

ok 10011 numbers

Test #5:

score: 0
Accepted
time: 182ms
memory: 14464kb

input:

1
100000
50000 50000 50001 50001 50002 50002 50003 50003 50004 50004 50005 50005 50006 50006 50007 50007 50008 50008 50009 50009 50010 50010 50011 50011 50012 50012 50013 50013 50014 50014 50015 50015 50016 50016 50017 50017 50018 50018 50019 50019 50020 50020 50021 50021 50022 50022 50023 50023 500...

output:

49999

result:

ok 1 number(s): "49999"