QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#356161#7944. Max Minus MinLainWA 9ms3580kbC++232.0kb2024-03-17 16:23:202024-03-17 16:23:21

Judging History

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

  • [2024-03-17 16:23:21]
  • 评测
  • 测评结果:WA
  • 用时:9ms
  • 内存:3580kb
  • [2024-03-17 16:23:20]
  • 提交

answer

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

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int tt;
  cin >> tt;
  while(tt--) {
    int n;
    cin >> n;
    vi a(n);
    for (auto& x : a) cin >> x;
    int mn = *min_element(all(a));
    int mx = *max_element(all(a));
    int ans = mx - mn;
    {
      auto check = [&](int k)->bool {
        vi b(n);
        int inc = k - mn;
        rep(i, 0, n) {
          if (a[i] < k) {
            b[i] = 1;
            if (a[i] + inc > mx) return false;
          }
        }
        int segs = 0;
        bool on = false;
        rep(i, 0, n) {
          if (b[i] == 1) {
            if (!on) {
              segs++;
            }
            on = true;
          } else {
            on = false;
          }
        }
        return segs <= 1;
      };

      int l = mn, r = mx;
      while(l < r) {
        int m = (l+r+1)/2;
        if (check(m)) {
          l = m;
        } else {
          r = m-1;
        }
      }
      ans = min(ans, mx - l);
    }
    {
      auto check = [&](int k)->bool {
        vi b(n);
        int dec = mx - k;
        rep(i, 0, n) {
          if (a[i] > k) {
            b[i] = 1;
            if (a[i] - dec < mn) return false;
          }
        }
        int segs = 0;
        bool on = false;
        rep(i, 0, n) {
          if (b[i] == 1) {
            if (!on) {
              segs++;
            }
            on = true;
          } else {
            on = false;
          }
        }
        return segs <= 1;
      };

      int l = mn, r = mx;
      while(l < r) {
        int m = (l+ r)/2;
        if (check(m)) {
          r = m;
        } else {
          l = m+1;
        }
      }
      ans = min(ans, l - mn);
    }
    cout << ans << '\n';
  }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 9ms
memory: 3580kb

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
3
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
3
2
2
1
1
4
2
1
2
2
1
1
1
2
1
2
3
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
4
2
1
2
0
1
1
1
1
1
1
2
2
3
4
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 16th numbers differ - expected: '2', found: '3'