QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#366262 | #7944. Max Minus Min | Infused | WA | 9ms | 4076kb | C++20 | 2.4kb | 2024-03-25 05:02:14 | 2024-03-25 05:02:14 |
Judging History
answer
#include "bits/stdc++.h"
#define ff first
#define ss second
#define pb push_back
#define ll long long
#define db puts("*****")
#define sz(x) int(x.size())
#define pii pair <int , int>
#define mid(x , y) ((x+y)>>1)
#define all(x) x.begin(),x.end()
#define y1 your_name_engraved_herein
using namespace std;
const int N = 2e5+2;
const int K = 1e3+2;
const int MOD = 1e9+7;
template<class T> bool umin(T& a, T b) { if(a > b){ a = b; return 1; } return 0; }
template<class T> bool umax(T& a, T b) { if(a < b){ a = b; return 1; } return 0; }
void solve(){
int n; scanf("%d", &n);
int lmin = 0, rmin = 0, mini = MOD;
int lmax = 0, rmax = 0, maxi = 0;
vector<int> a(n);
for(int i=0; i<n; i++){
scanf("%d", &a[i]);
mini = min(mini, a[i]);
maxi = max(maxi, a[i]);
}
for(int i=0; i<n; i++){
if(a[i] == mini){
if(!lmin) lmin = i;
rmin = i;
}
if(a[i] == maxi){
if(!lmax) lmax = i;
rmax = i;
}
}
int med = mid(maxi, mini);
int ln = lmin, rn = rmin;
while(ln > 0 && a[ln-1] <= med) ln--;
while(rn < n-1 && a[rn+1] <= med) rn++;
int lx = lmax, rx = rmax;
while(lx > 0 && a[lx-1] > med) lx--;
while(rx < n-1 && a[rx+1] > med) rx++;
int ans = 0;
auto getans = [&](int x, int y){
int minInside = *min_element(a.begin() + x, a.begin() + y + 1);
int maxInside = *max_element(a.begin() + x, a.begin() + y + 1);
int minOutside = MOD, maxOutside = 0;
for(int i=0; i<x; i++){
umin(minOutside, a[i]);
umax(maxOutside, a[i]);
}
for(int i=y+1; i<n; i++){
umin(minOutside, a[i]);
umax(maxOutside, a[i]);
}
int diff1 = maxInside - minInside;
int diff2 = maxOutside - minOutside;
if(diff2 == MOD) diff2 = 0;
return max(diff1, diff2);
};
if(lmin < lmax && rmax < rmin){
ans = getans(lx, rx);
} else if(lmax < lmin && rmin < rmax){
ans = getans(ln, rn);
} else if(min(rmin, rmax) > max(lmin, lmax)){
ans = (maxi - mini);
} else {
ans = min(getans(ln, rn), getans(lx, rx));
}
printf("%d\n", ans);
}
int main(){
int testcase = 1;
scanf("%d", &testcase);
while(testcase--){
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3860kb
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: 4076kb
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 3 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 126th numbers differ - expected: '2', found: '3'