QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#53232#2103. TermityFamiglistmo#WA 2ms3800kbC++142.1kb2022-10-04 20:31:592022-10-04 20:32:03

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-10-04 20:32:03]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:3800kb
  • [2022-10-04 20:31:59]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 5;

char buf[1 << 23], *p1 = buf, *p2 = buf;
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1 ++ )
int read() {
    int s = 0, w = 1; char ch = getchar();
    while(!isdigit(ch)) { if(ch == '-') w = -1; ch = getchar(); }
    while(isdigit(ch)) s = s * 10 + (ch ^ 48), ch = getchar();
    return s * w;
}

vector<int> vec, num;
int a[N], stk[N];
int n, top, dif, sum;

void append(int x) {
    if(top <= 1) stk[++ top] = x;
    else {
        if(stk[top - 1] <= stk[top] && x <= stk[top])
            // cout << stk[top - 1] << " " << stk[top] << " " << x << endl,
            stk[top - 1] += x - stk[top], -- top;
        else stk[++ top] = x;
    }
}

void work(int l, int r) {
    if(l > r) return ;
    // cout << l << " " << r << endl;

    top = 0;
    if(r == n) for(int i = r; i >= l; -- i) append(a[i]);
    else for(int i = l; i <= r; ++ i) append(a[i]);
    // for(int i = 1; i <= top; ++ i) cout << stk[i] << " "; puts("");

    int s = 0, ptr = 1;
    if(l == 1 || r == n) {
        while(ptr + 1 <= top && stk[ptr] >= stk[ptr + 1])
            s += stk[ptr] - stk[ptr + 1], ptr += 2;
        if(ptr != 1) num.push_back(s);
    }
    
    for(int i = ptr; i <= top; ++ i)
        num.push_back(stk[i]);    
}

signed main() {
    n = read();
    vec.push_back(0);
    for(int i = 1; i <= n; ++ i) {
        a[i] = read(), sum += a[i];
        if(a[i] == 0) vec.push_back(i);
    }
    vec.push_back(n + 1);

    for(int i = 1; i < (int)vec.size(); ++ i)
        work(vec[i - 1] + 1, vec[i] - 1);
    // for(auto v : num) cout << v << " "; puts("");
    sort(num.begin(), num.end(), greater<int>());

    for(int i = 0; i < (int)num.size(); ++ i)
        if(i & 1) dif -= num[i];
        else dif += num[i];
    // for(int i = 0; i < (int)num.size(); ++ i)
    //     if(i & 1) b += num[i];
    //     else a += num[i];
    printf("%lld %lld\n", (sum + dif) / 2, (sum - dif) / 2);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3800kb

input:

1
0

output:

0 0

result:

ok single line: '0 0'

Test #2:

score: 0
Accepted
time: 2ms
memory: 3656kb

input:

2
0 17

output:

17 0

result:

ok single line: '17 0'

Test #3:

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

input:

3
0 13 0

output:

13 0

result:

ok single line: '13 0'

Test #4:

score: 0
Accepted
time: 2ms
memory: 3760kb

input:

8
1 2 0 3 7 4 0 9

output:

17 9

result:

ok single line: '17 9'

Test #5:

score: 0
Accepted
time: 2ms
memory: 3728kb

input:

10
0 0 0 1 0 0 0 5 0 0

output:

5 1

result:

ok single line: '5 1'

Test #6:

score: -100
Wrong Answer
time: 2ms
memory: 3760kb

input:

24
55 33 93 169 237 0 219 125 39 13 96 166 200 278 0 99 2 18 110 0 58 57 0 42

output:

1132 977

result:

wrong answer 1st lines differ - expected: '1125 984', found: '1132 977'