QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#371274#7944. Max Minus Minishmeal#WA 15ms3588kbC++233.2kb2024-03-30 04:04:382024-03-30 04:04:39

Judging History

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

  • [2024-03-30 04:04:39]
  • 评测
  • 测评结果:WA
  • 用时:15ms
  • 内存:3588kb
  • [2024-03-30 04:04:38]
  • 提交

answer

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

template<class T>
struct RMQ {
        vector<vector<T>> jmp;
        RMQ(const vector<T>& V) : jmp(1, V) {
                for (int pw = 1, k = 1; pw * 2 <= V.size(); pw *= 2, ++k) {
                        jmp.emplace_back(V.size() - pw * 2 + 1);
                        for (int j = 0; j < jmp[k].size(); j++)
                                jmp[k][j] = min(jmp[k-1][j], jmp[k-1][j+pw]);
                }
        }
        T query(int a, int b) {
                assert(a < b);
                int dep = 31 - __builtin_clz(b - a);
                return min(jmp[dep][a], jmp[dep][b-(1<<dep)]);
        }
};

template<class T>
struct RMaxQ {
        vector<vector<T>> jmp;
        RMaxQ(const vector<T>& V) : jmp(1, V) {
                for (int pw = 1, k = 1; pw * 2 <= V.size(); pw *= 2, ++k) {
                        jmp.emplace_back(V.size() - pw * 2 + 1);
                        for (int j = 0; j < jmp[k].size(); j++)
                                jmp[k][j] = max(jmp[k-1][j], jmp[k-1][j+pw]);
                }
        }
        T query(int a, int b) {
                assert(a < b);
                int dep = 31 - __builtin_clz(b - a);
                return max(jmp[dep][a], jmp[dep][b-(1<<dep)]);
        }
};

void run() {
        int n; cin >> n;
        vector<int> a(n); for (int &x : a) cin >> x;
        RMQ<int> mn(a);
        RMaxQ<int> mx(a);

        vector<int> ind(n); iota(ind.begin(), ind.end(), 0);
        sort(ind.begin(), ind.end(), [&](int l, int r) {
                return a[l] < a[r];
        });

        int best = INT_MAX;
        {
                int l = ind[0], r = ind[0]+1;
                for (int i = 0; i < n-1; i++) {
                        l = min(l, ind[i]);
                        r = max(r, ind[i]+1);

                        int b = mx.query(l,r) + a[ind[i+1]] - a[ind.front()];
                        b = max(b, a[ind.back()]);
                        best = min(best, b - a[ind[i+1]]);
                }
        }

        for (int &x : a) x = -x;
        mx = RMaxQ<int>(a);
        reverse(ind.begin(), ind.end());

        {
                int l = ind[0], r = ind[0]+1;
                for (int i = 0; i < n-1; i++) {
                        l = min(l, ind[i]);
                        r = max(r, ind[i]+1);

                        int b = mx.query(l,r) + a[ind[i+1]] - a[ind.front()];
                        b = max(b, a[ind.back()]);
                        best = min(best, b - a[ind[i+1]]);
                }
        }
//      sort(ind.begin(), ind.end(), [&](int l, int r) {
//              return a[l] > a[r];
//      });
//      {
//              int l = ind[0], r = ind[0]+1;
//              for (int i = 0; i < n-1; i++) {
//                      l = min(l, ind[i]);
//                      r = max(r, ind[i]+1);
//
//                      int b = mn.query(l,r) - a[ind[i+1]] + a[ind.front()];
//                      b = min(b, a[ind.back()]);
//                      best = min(best, a[ind[i+1]] - b);
//              }
//      }

        cout << best << '\n';
}

int main() {
        cin.tie(0)->sync_with_stdio(0);
        int t; cin >> t; while (t--) run();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 15ms
memory: 3588kb

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
3
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
2
1
2
1
2
2
2
1
1
2
2
1
2
2
1
1
1
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
3
2
1
1
2
2
3
1
1
1
2
2
1
1
1
1
2
2
2
2
2
3
2
1
2
0
1
1
1
1
1
1
2
2
2
2
2
2
1
2
1
2
4
1
1
1
3
2
2
2
1
2
1
2
1
2
2
1
3
...

result:

wrong answer 3909th numbers differ - expected: '0', found: '2147483647'