UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#199724#2606. 取石子Anonyme100896ms26360kbC++111.7kb2023-12-19 09:23:072023-12-19 12:50:18

answer

#include<bits/stdc++.h>
using namespace std;

#define QwQ330AwA return 0
#define ll long long

const int N = 1000005;
int n;
int pre[N], suf[N];
ll w[N];
bool vis[N];
bool instk[N];
queue <int> pos;

void ck(int x) {
    if (x < 1 || x > n) return ;
    if (pre[x] == 0) return ;
    if (suf[x] == n + 1) return ;
    if (w[pre[x]] <= w[x] && w[suf[x]] <= w[x] && !instk[x]) pos.push(x), instk[x] = 1;
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    ll sum = 0;
    for (int i = 1; i <= n; i++) cin >> w[i], sum += w[i];
    for (int i = 1; i <= n; i++) pre[i] = i - 1, suf[i] = i + 1;
    for (int i = 2; i < n; i++) ck(i);
//    cout << pos.size() << endl;
    while (!pos.empty()) {
        int x = pos.front();
        pos.pop();
        //cout << x << endl;
        instk[x] = 0;
        if (vis[x]) continue;
        if (pre[x] == 0 || suf[x] == n + 1) continue;
        if (w[pre[x]] > w[x] || w[suf[x]] > w[x]) continue;
        w[x] = w[pre[x]] + w[suf[x]] - w[x];
        int l = pre[pre[x]], r = suf[suf[x]];
        vis[pre[x]] = 1;
        vis[suf[x]] = 1;
        suf[l] = x;
        pre[r] = x;
        pre[x] = l;
        suf[x] = r;
        ck(l);
        ck(r);
        ck(x);
    }
    vector <ll> val;
    for (int i = 1; i <= n; i++) if (!vis[i]) val.push_back(w[i]);
//    cout << endl;
    sort(val.begin(), val.end());
    reverse(val.begin(), val.end());
    ll ans = 0;
    for (int i = 0; i < (int)val.size(); i++) {
        if (i % 2 == 0) ans += val[i];
        else ans -= val[i];
    }
    assert((sum + ans) % 2 == 0);
    cout << (sum + ans) / 2;
    QwQ330AwA;
}

详细

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 5
Accepted

Test #1:

score: 5
Accepted
time: 2ms
memory: 1292kb

input:

20
34609193 3798235 593299601 475645303 888195631 659454499 551037978 596032266 394004781 307877719 ...

output:

5176695914

result:

ok single line: '5176695914'

Test #2:

score: 0
Accepted
time: 0ms
memory: 1296kb

input:

20
257837297 981007596 247719754 572613679 890525438 193092208 382562531 813994741 736265810 9411386...

output:

5999233923

result:

ok single line: '5999233923'

Test #3:

score: 0
Accepted
time: 0ms
memory: 1296kb

input:

20
728075060 566324636 299668530 361377217 995240088 303157214 468025638 233 693207134 233 43707398 ...

output:

5655468652

result:

ok single line: '5655468652'

Subtask #2:

score: 16
Accepted

Test #4:

score: 16
Accepted
time: 0ms
memory: 1392kb

input:

5000
831868113 83247241 7008654 71622129 276964969 769109493 716836035 409056337 945188562 701720531...

output:

1270112927250

result:

ok single line: '1270112927250'

Test #5:

score: 0
Accepted
time: 0ms
memory: 1392kb

input:

5000
536798484 742306105 665837174 306216323 848574467 177488740 356435718 404320671 381065694 75267...

output:

1236468303162

result:

ok single line: '1236468303162'

Test #6:

score: 0
Accepted
time: 1ms
memory: 1392kb

input:

5000
561205591 773453801 163616606 94979862 472927133 287520978 441866057 164826861 371112170 803598...

output:

1242244929142

result:

ok single line: '1242244929142'

Subtask #3:

score: 23
Accepted

Test #7:

score: 23
Accepted
time: 144ms
memory: 26320kb

input:

1000000
999998295 999997633 999997288 999996499 999995245 999993138 999989907 999989340 999988536 99...

output:

249810344147441

result:

ok single line: '249810344147441'

Test #8:

score: 0
Accepted
time: 142ms
memory: 26360kb

input:

1000000
999998340 999997539 999997129 999996027 999995490 999990654 999987196 999985596 999985211 99...

output:

249912278151408

result:

ok single line: '249912278151408'

Test #9:

score: 0
Accepted
time: 225ms
memory: 26324kb

input:

1000000
999999396 999998733 999992278 999989607 999983083 999975800 999975116 999956557 999955866 99...

output:

249932893118783

result:

ok single line: '249932893118783'

Subtask #4:

score: 56
Accepted

Test #10:

score: 56
Accepted
time: 133ms
memory: 20084kb

input:

1000000
985047382 509193839 986465526 583993613 889445095 656624416 109977936 722619615 924421147 71...

output:

250065483816919

result:

ok single line: '250065483816919'

Test #11:

score: 0
Accepted
time: 128ms
memory: 20092kb

input:

1000000
309366426 786066196 259041687 594566187 112300217 85424331 323093652 693198640 485030445 759...

output:

250138218389114

result:

ok single line: '250138218389114'

Test #12:

score: 0
Accepted
time: 121ms
memory: 20096kb

input:

1000000
615867790 223539795 478898165 782641633 313755728 441721617 704485768 458874871 57982460 447...

output:

250377842811779

result:

ok single line: '250377842811779'