QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#326168 | #7944. Max Minus Min | javillamip | WA | 12ms | 3772kb | C++17 | 3.9kb | 2024-02-12 14:35:30 | 2024-02-12 14:35:30 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define forn(i,n) for(long long i=0; i< (long long)n; ++i)
#define for1(i,n) for(long long i=1; i<= (long long)n; ++i)
#define fore(i,l,r) for(long long i=(long long)l; i<= (long long)r; ++i)
#define ford(i,n) for(long long i=(long long)(n) - 1; i>= 0; --i)
#define fored(i,l,r) for(long long i=(long long)r; i>= (long long)l; --i)
#define pb push_back
#define el '\n'
#define d(x) cout<< #x<< " " << x<<el
#define ri(n) scanf("%d",&n)
#define sz(v) int(v.size())
#define all(v) v.begin(),v.end()
typedef long long ll;
typedef double ld;
typedef pair<long long,long long> ii;
typedef pair<ll,ll> pll;
typedef tuple<long long, long long, long long> iii;
typedef vector<long long> vi;
typedef vector<ii> vii;
typedef vector<ll> vll;
typedef vector<ld> vd;
const long long inf = 1e9;
const long long nax = 1e5+200;
const ld pi = acos(-1);
const ld eps= 1e-9;
long long dr[] = {1,-1,0, 0,1,-1,-1, 1};
long long dc[] = {0, 0,1,-1,1, 1,-1,-1};
struct stree{
long long neutro = 2e9, n, l, r, pos, val; vi t;
stree(long long n): n(n), t(n << 2){}
stree(const vi& a): n(sz(a)), t(n<<2){ build(1, 0, n-1, a); }
inline long long oper(long long a, long long b){ return min(a, b); }
void build(long long v, long long tl, long long tr, const vi& a){// solo para el 2. constructor
if(tl == tr){ t[v] = a[tl]; return; }
long long tm = (tl + tr) >> 1;
build(v << 1, tl, tm, a), build((v << 1) | 1, tm+1, tr, a);
t[v] = oper(t[v << 1], t[(v << 1) | 1]);
}
long long query(long long v, long long tl, long long tr){
if(tl > r || tr < l) return neutro; // estoy fuera
if(l <= tl && tr <= r) return t[v];
long long tm = (tl + tr) >> 1;
return oper(query(v << 1, tl, tm), query((v << 1) | 1, tm+1, tr));
}
void upd(long long v, long long tl, long long tr){
if(tl == tr){ t[v] = val; return; }
long long tm = (tl + tr) >> 1;
if(pos <= tm) upd(v << 1, tl, tm);
else upd((v << 1) | 1, tm+1, tr);
t[v] = oper(t[v << 1], t[(v << 1) | 1]);
}
void upd(long long idx, long long num){ pos = idx, val = num, upd(1, 0, n-1);}
long long query(long long ql, long long qr){ if(ql>qr) return 2e9;l = ql, r = qr; return query(1, 0, n-1);}
};
long long best(vector<long long> v){
stree st(v);
long long n = v.size();
long long mn = 2e9;
long long mni = 0;
long long mx = -2e9;
for(long long i = 0 ; i < n ; ++i){
if(v[i] < mn){
mn = v[i];
mni = i;
}
mx = max(mx,v[i]);
}
long long r = n-1;
long long l = 0;
for(long long i = mni+1 ; i < n ; ++i){
if(v[i] == mx){
r = i-1;
break;
}
}
for(long long i = mni-1 ; i >= 0 ; --i){
if(v[i] == mx){
l = i+1;
break;
}
}
long long mx2 = -2e9;
long long ll=mni;
long long rr=mni;
long long ans = -2e9;
bool entered = false;
bool b = false;
while(ll >=0 || rr <= n-1){
entered=true;
if(ll>=0){
mx2 = max(mx2,v[ll]);
}
if(rr<=n-1){
mx2 = max(mx2,v[rr]);
}
// cout<<"mx2: "<<mx2<<'\n';
long long bst = min({st.query(0,ll-1),st.query(rr+1,n), mn + (mx-mx2)});
ans = max(ans,bst);
if(b)ll--;
else rr++;
b = !b;
}
long long mxx = -2e9;
for(long long i = l ; i<=r ; ++i){
mxx=max(mxx,v[i]);
}
for(long long i = l ; i<=r ; ++i){
v[i] += mx-mxx;
}
long long mxxx = -2e9;
long long mnnn = 2e9;
for(long long val : v){
mxxx = max(mxxx,val);
mnnn = min(mnnn,val);
}
if(entered) {
// cout<<"x"<<mx - ans<<' '<<mxxx-mnnn<<'\n';
return min((mx-ans),mxxx-mnnn);
}
else return mxxx-mnnn;
}
int main(){
ios_base::sync_with_stdio(false),
cin.tie(NULL);
long long t; cin>>t;
while(t--){
long long n; cin>>n;
vector<long long> v(n);
vector<long long> v2(n);
for(long long i = 0 ; i < n ; ++i){
cin>>v[i];
v2[i] = -v[i];
}
cout<<min(best(v), best(v2))<<'\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3772kb
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: 12ms
memory: 3644kb
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 3 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 2 2 1 2 2 1 1 1 2 1 2 3 1 2 2 3 2 2 1 1 2 1 2 3 3 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 3 1 2 0 1 3 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 3 1 3 ...
result:
wrong answer 15th numbers differ - expected: '1', found: '3'