QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#326160#7944. Max Minus MinScanoWA 19ms3600kbC++201.7kb2024-02-12 14:02:402024-02-12 14:02:40

Judging History

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

  • [2024-02-12 14:02:40]
  • 评测
  • 测评结果:WA
  • 用时:19ms
  • 内存:3600kb
  • [2024-02-12 14:02:40]
  • 提交

answer

#include <bits/stdc++.h>

#define pb push_back
#define forn(i,n) for(int i=0; i<n; i++)
#define el '\n'
#define sz(v) (int(v.size()))
#define d(x) cout<< #x<< " " << x<<el
#define all(v) v.begin(),v.end()
#define fi first
#define se second

using namespace std;

typedef vector<int> vi;
typedef long long ll;

const int nax = 3e5 + 20;
const int mod = 1e9 + 7;

int solve(vector<int> &a, int n){

  map<int, vector<int>> pos;
  int mx = -1e9;
  vector<int> pre(n), suf(n);
  int sum = 1e9;
  forn(i,n){
    sum = min(sum, a[i]);
    pre[i] = sum;
    pos[a[i]].pb(i);
    mx = max(mx, a[i]);
  }

  sum = 1e9;
  for(int i = n - 1; i >= 0; --i){
    sum = min(sum, a[i]);
    suf[i] = sum;
  }
  int l = -1, r = -1;
  int ans = 1e9;

  int val = 1e9;

  for(auto &e : pos){
    val = min(val, e.fi);
    int mn = 1e9;
    mn = min(mn, mx - e.fi);

    if(l == -1){
      l = e.se[0];
      r = e.se[0];
    }

    while(l >= e.se[0]){
      mn = min(mn, mx - a[l]);
      --l;
    }
    while(r <= e.se.back()){
      mn = min(mn, mx - a[r]);
      ++r;
    }
    int mn_pre = (l == -1 ? 1e9 : pre[l]);
    int mn_suf = (r == n ? 1e9 : suf[r]);
    ans = min(ans, mx - min({val + mn, mn_pre, mn_suf}));
  }
  return ans;
}

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL); cout.tie(NULL);
  int t;
  cin >> t;
  while(t--){
    int n;
    cin >> n;
    vector<int> a(n);
    int mx = 0;
    forn(i,n){
      cin >> a[i];
      mx = max(mx, a[i]);
    }
    int ans = solve(a, n);
    forn(i,n){
      a[i] = -a[i];
    }
    ans = min(ans, solve(a, n));
    cout << ans << el;

  }

  
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3600kb

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: 19ms
memory: 3544kb

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

result:

wrong answer 4th numbers differ - expected: '3', found: '1'