QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#405051 | #7944. Max Minus Min | ucup-team992# | WA | 15ms | 3736kb | C++20 | 1.8kb | 2024-05-05 09:13:09 | 2024-05-05 09:13:09 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define ar array
typedef int uci;
#define int long long
#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()
#define ll long long
typedef pair<int, int> pii;
typedef vector<int> vi;
void solve(){
ll N; cin >> N;
vi A(N); rep(i,0,N) cin >> A[i];
ll M = A[0], m = A[0];
rep(i,0,N) {M = max(M,A[i]); m = min(m,A[i]);}
map<ll, vi> indices;
rep(i,0,N) indices[A[i]].push_back(i);
vi uA; for(auto kv:indices) uA.push_back(kv.first);
ll ans = M-m;
ll maxr = -1;
ll l = N+1;
ll r = -1;
for(auto m2: uA) {
ll ol = l, olr=r;
for(auto i: indices[m2]) {l = min(l,i); r = max(r, i);}
if(olr<0) {
rep(i,l,r+1) maxr = max(maxr, A[i]);
} else {
rep(i,l,ol) maxr = max(maxr, A[i]);
rep(i,olr+1,r+1) maxr = max(maxr, A[i]);
}
if(maxr + m2 - m + 1 > M) break;
ll incr = M - maxr;
ans = M-min(m+incr,m2+1);
}
nextl1:
ll minr = M+1;
l = N;
r = -1;
reverse(uA.begin(), uA.end());
for(auto M2: uA) {
ll ol = l, olr=r;
for(auto i: indices[M2]) {l = min(l,i); r = max(r, i);}
if(olr<0) {
rep(i,l,r+1) minr = min(minr,A[i]);
} else {
rep(i,l,ol) minr = min(minr,A[i]);
rep(i,olr+1,r+1) minr = min(minr,A[i]);
}
if(minr - (M-M2+1) < m) break;
ll decr = minr-m;
ans = min(ans, max(M2-1, M-decr)-m);
}
nextl2:
cout << ans << '\n';
}
uci main(){
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
ll T; cin >> T;
rep(t,0,T) solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3488kb
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: 3736kb
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 3 2 0 3 2 3 1 1 3 2 3 3 2 0 2 1 2 0 3 2 2 3 3 2 3 3 3 3 2 2 3 2 2 2 2 2 3 2 1 2 1 2 3 3 2 3 2 2 3 2 2 1 2 2 2 1 2 2 1 2 3 3 2 3 2 1 3 2 2 3 2 0 2 1 3 2 1 1 2 2 2 1 3 2 1 3 3 2 1 1 3 2 3 1 1 1 2 2 1 1 3 1 2 2 3 2 2 3 2 1 2 2 2 2 2 2 1 1 2 2 2 2 2 2 2 2 1 2 4 1 1 1 3 2 2 2 1 2 3 2 2 2 2 1 3 ...
result:
wrong answer 6th numbers differ - expected: '1', found: '3'