QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#384426 | #7944. Max Minus Min | Giga_Cronos# | WA | 25ms | 13880kb | C++20 | 4.2kb | 2024-04-09 23:23:58 | 2024-04-09 23:23:58 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
/// MACROS
#define int long long
#define itn int
#define str string
#define sz size
#define ll long long
#define ull unsigned long long
#define ld long double
#define ii pair<int, int>
#define fs first
#define sc second
#define vi vector<int>
#define vii vector<ii>
#define pb push_back
#define pf push_front
#define fl '\n'
#define el endl
#define eps 1e-12
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
/// FUNCTIONS
#define db(x) cerr << #x << ": " << (x) << '\n';
#define lg2(x) __lg(x)
#define pi acos(-1)
#define YN(x) cout << ((x) ? ("YES") : ("NO")) << fl;
#define yn(x) cout << ((x) ? ("Yes") : ("No")) << fl;
#define precision(x) cout.setf(ios::fixed);cout.precision(x);
#define FL cout<<fl;
#define EL cout<<el;
#define ESP cout<<" ";
void print(int &x){cout<<x;}
void print(ii x){cout<<x.fs<<" "<<x.sc;}
void print(const auto &v){for(auto it: v){print(it);cout<<" ";}cout<<fl;}
void printp(const auto &v){for(auto it: v){print((ii)it);cout<<fl;}}
vi read(int cnt){vi v(cnt);for(int i=0;i<cnt;i++)cin>>v[i];return v;}
vii readp(int cnt){vii v(cnt);for(int i=0;i<cnt;i++)cin>>v[i].fs>>v[i].sc;return v;}
/////////////////////////////////////////////////////////////////////////////////////
template <typename T>
T s_prev(const std::set<T>& s, const T& val, const T& defaultValue) {
auto it=s.lower_bound(val);
if (it==s.begin()) return defaultValue;
--it;
return *it;
}
template <typename T>
T s_next(const std::set<T>& s, const T& val, const T& defaultValue) {
auto it=s.upper_bound(val);
if (it==s.end()) return defaultValue;
return *it;
}
/////////////////////////////////////////////////////////////////////////////////////
itn n;
vi arr;
int spt[25][1000006];
void build(){
for(int i=0;i<n;i++){spt[0][i]=arr[i];}
for(int i=1;i<25;i++){
for(int j=0;j+((int)1<<i)-1<n;j++){
spt[i][j]=min(spt[i-1][j],spt[i-1][j+((int)1<<(i-1))]);
}
}
}
int qmin(int l,int r){
if(l>r)return 1e12;
int lg=lg2(r-l+1);
return min(spt[lg][l],spt[lg][r-((int)1<<lg)+1]);
}
int spt2[25][1000006];
void build2(){
for(int i=0;i<n;i++){spt2[0][i]=arr[i];}
for(int i=1;i<25;i++){
for(int j=0;j+((int)1<<i)-1<n;j++){
spt2[i][j]=max(spt2[i-1][j],spt2[i-1][j+((int)1<<(i-1))]);
}
}
}
int qmax(int l,int r){
if(l>r)return -1e12;
int lg=lg2(r-l+1);
return max(spt2[lg][l],spt2[lg][r-((int)1<<lg)+1]);
}
void solve(){
cin>>n;
arr=read(n);
build();
build2();
int miv=1e12;
for(int i=0;i<n;i++){
miv=min(miv,arr[i]);
}
itn ans=1e12;
for(int i=0;i<n;i++){
int j=n-1;
for(int mask=20;mask>=0;mask--){
int nxt=j-((int)1<<mask);
if(nxt>=i && qmax(nxt+1,n-1)<arr[i]){
j=nxt;
}
}
// cout<<i<<" "<<j<<fl;
vi t;
if(i==0 && j==n-1)continue;
int mic=1e12;
if(i){t.pb(qmin(0,i-1));mic=min(mic,qmin(0,i-1));}
if(i)t.pb(qmax(0,i-1));
if(j!=n-1){t.pb(qmin(j+1,n-1));mic=min(mic,qmin(j+1,n-1));}
if(j!=n-1)t.pb(qmax(j+1,n-1));
t.pb(mic + qmax(i,j)-qmin(i,j));
sort(all(t));
ans=min(ans,t.back()-t[0]);
}
for(int i=0;i<n;i++){
int j=n-1;
for(int mask=20;mask>=0;mask--){
int nxt=j-((int)1<<mask);
if(nxt>=i && qmin(nxt+1,n-1)>arr[i]){
j=nxt;
}
}
// cout<<i<<" "<<j<<fl;
vi t;
if(i==0 && j==n-1)continue;
int mic=-1e12;
if(i){t.pb(qmin(0,i-1));}
if(i){t.pb(qmax(0,i-1));mic=max(mic,qmax(0,i-1));}
if(j!=n-1){t.pb(qmin(j+1,n-1));}
if(j!=n-1){t.pb(qmax(j+1,n-1));mic=max(mic,qmax(j+1,n-1));}
t.pb(mic - (qmax(i,j)-qmin(i,j)) );
sort(all(t));
ans=min(ans,t.back()-t[0]);
}
cout<<ans<<fl;
}
int32_t main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int tc=1;
cin>>tc;
while(tc--){
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 13844kb
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: 25ms
memory: 13880kb
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 3 2 1 2 1 2 1 2 2 2 1 1 2 2 1 2 2 1 1 1 2 2 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 2 1 2 2 2 1 1 1 2 2 2 2 2 3 2 1 2 0 3 1 1 1 1 1 2 3 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 40th numbers differ - expected: '2', found: '3'