QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#366730#7944. Max Minus MinmilmillinWA 11ms4056kbC++142.8kb2024-03-25 06:49:532024-03-25 06:49:53

Judging History

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

  • [2024-03-25 06:49:53]
  • 评测
  • 测评结果:WA
  • 用时:11ms
  • 内存:4056kb
  • [2024-03-25 06:49:53]
  • 提交

answer

#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

constexpr int INF = 1e9 + 10;

void f() {
    int n;
    scanf("%d", &n);
    vector<int> tbl(n);
    int mx = -1;
    int mn = INF;
    int mx_id1 = -1;
    int mx_id2 = -1;
    int mn_id1 = -1;
    int mn_id2 = -1;
    vector<int> pref_mn(n);
    vector<int> pref_mx(n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &tbl[i]);
        if (tbl[i] > mx) {
            mx = tbl[i];
            mx_id1 = mx_id2 = i;
        } else if (tbl[i] == mx) {
            mx_id2 = i;
        }
        if (tbl[i] < mn) {
            mn = tbl[i];
            mn_id1 = mn_id2 = i;
        } else if (tbl[i] == mn) {
            mn_id2 = i;
        }
        pref_mn[i] = mn;
        pref_mx[i] = mx;
    }
    vector<int> suff_mn(n);
    vector<int> suff_mx(n);
    int _mn = INF;
    int _mx = -1;
    for (int i = n - 1; i >= 0; i--) {
        _mn = min(_mn, tbl[i]);
        _mx = max(_mx, tbl[i]);
        suff_mn[i] = _mn;
        suff_mx[i] = _mx;
    }

    int mn_mx = mx;
    int mx_mn = mn;
    for (int i = mx_id1; i <= mx_id2; i++) mn_mx = min(mn_mx, tbl[i]);
    for (int i = mn_id1; i <= mn_id2; i++) mx_mn = max(mx_mn, tbl[i]);

    int lo = 0;
    int hi = mx - mn;
    while (lo < hi) {
        int mid = (lo + hi) / 2;
        bool work = false;

        // mx
        int cl = mx_id1;
        int cr = mx_id2;
        int c_min = mn_mx;
        while (cl - 1 >= 0 && mx - min(c_min, tbl[cl - 1]) <= mid) {
            c_min = min(c_min, tbl[cl - 1]);
            cl--;
        }
        while (cr + 1 < n && mx - min(c_min, tbl[cr + 1]) <= mid) {
            c_min = min(c_min, tbl[cr + 1]);
            cr++;
        }
        work |= (mx - c_min <= mid) && (max((cl - 1 > 0) ? pref_mx[cl - 1] : -1, (cr + 1 < n) ? suff_mx[cr + 1] : -1) - min((cl - 1 > 0) ? pref_mn[cl - 1] : INF, (cr + 1 < n) ? suff_mn[cr + 1] : INF)) <= mid;

        if (!work) {
            cl = mn_id1;
            cr = mn_id2;
            int c_max = mx_mn;
            while (cl - 1 >= 0 && max(c_max, tbl[cl - 1]) - mn <= mid) {
                c_max = max(c_max, tbl[cl - 1]);
                cl--;
            }
            while (cr + 1 < n && max(c_max, tbl[cr + 1]) - mn <= mid) {
                c_max = max(c_max, tbl[cr + 1]);
                cr++;
            }
            work |= (c_max - mn <= mid) && (max((cl - 1 > 0) ? pref_mx[cl - 1] : -1, (cr + 1 < n) ? suff_mx[cr + 1] : -1) - min((cl - 1 > 0) ? pref_mn[cl - 1] : INF, (cr + 1 < n) ? suff_mn[cr + 1] : INF)) <= mid;
        }

        if (!work) {
            lo = mid + 1;
        } else {
            hi = mid;
        }
    }
    printf("%d\n", lo);
}

int main() {
    int q;
    scanf("%d", &q);
    while (q--) f();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 4056kb

input:

4
3
42 42 42
4
1 2 2 1
5
1 100 1 100 1
6
1 2 3 4 5 6

output:

0
0
99
2

result:

ok 4 number(s): "0 0 99 2"

Test #2:

score: -100
Wrong Answer
time: 11ms
memory: 3848kb

input:

19530
6
2 3 3 3 4 3
6
2 4 4 2 5 5
6
2 5 2 5 1 3
6
4 4 1 2 4 1
5
5 4 5 3 3
5
2 2 1 5 1
6
3 1 2 4 2 3
5
5 5 4 4 5
6
5 3 3 1 4 2
6
3 3 2 4 2 4
6
1 2 4 5 2 5
5
3 4 5 5 3
6
4 3 3 3 4 3
6
1 5 1 2 3 1
5
5 5 2 1 4
6
1 2 5 3 5 2
6
4 5 2 4 5 2
5
2 4 2 4 1
4
2 3 3 3
6
1 2 2 1 4 5
6
3 2 1 3 3 2
6
2 1 1 2 5 3
6
...

output:

1
2
3
3
1
1
2
0
2
2
3
1
1
2
1
2
3
2
0
1
1
2
0
3
2
2
3
2
2
2
3
3
2
2
1
2
2
2
2
2
2
1
1
1
2
1
2
2
2
1
1
2
2
1
2
2
1
1
0
2
1
2
2
1
2
2
3
2
2
1
1
2
1
2
3
2
0
2
1
1
2
1
1
2
2
2
1
3
2
1
2
2
2
0
1
2
2
3
1
1
1
2
2
1
0
1
1
2
2
2
1
1
3
2
1
2
0
1
1
1
1
1
0
2
2
2
2
2
2
1
2
1
1
4
1
1
1
3
2
2
2
1
2
1
2
1
2
1
1
3
...

result:

wrong answer 9th numbers differ - expected: '3', found: '2'