QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#387155 | #7944. Max Minus Min | wizhao196# | RE | 0ms | 0kb | C++17 | 3.6kb | 2024-04-12 08:11:13 | 2024-04-12 08:11:14 |
answer
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
vector<pair<long long, long long> > indices;
long long arr[200000];
int main()
{
int t;
cin>>t;
for(int i =0; i<t; i++)
{
int n;
cin>>n;
long long maximum = -1, minimum = 1e9 + 1;
indices.clear();
for(int j = 0; j<n; j++)
{
cin>>arr[j];
maximum = max(maximum, arr[j]);
minimum = min(minimum, arr[j]);
indices.push_back(make_pair(arr[j], j));
}
if(maximum-minimum == 0)
{
cout<<0<<endl;
continue;
}
sort(indices.begin(), indices.end());
multiset <long long> x, y;
for(int i = 0; i< n; i++)
{
x.insert(arr[i]);
}
long long ans = 1e9+1;
long long left = indices[0].second, right = indices[0].second;
y.insert(indices[0].first);
x.erase(x.find(indices[0].first));
for(int j = 0; j<n ; j++)
{
long long new_index = indices[j].second;
//cout<<j<<" "<<new_index<<" "<<left<<" "<<right<<endl;
if(new_index > right)
{
for(int t = right +1; t<=new_index; t++)
{
y.insert(arr[t]);
x.erase(x.find(arr[t]));
}
right = new_index;
}
if(new_index < left)
{
for(int t = left -1; t>=new_index; t--)
{
y.insert(arr[t]);
x.erase(x.find(arr[t]));
}
left = new_index;
}
auto f = x.end();
f--;
long long curr_x_maximum = *f;
long long curr_x_minimum = *x.begin();
auto z = y.end();
z--;
long long curr_y_maximum = *z;
long long curr_y_minimum = *y.begin();
long long diff = curr_x_maximum - curr_y_maximum;
curr_y_minimum += diff;
ans = min(ans, curr_x_maximum - min(curr_y_minimum, curr_x_minimum));
}
x.clear();
y.clear();
for(int i = 0; i< n; i++)
{
x.insert(arr[i]);
}
left = indices[n-1].second;
right = indices[n-1].second;
y.insert(indices[n-1].first);
x.erase(x.find(indices[n-1].first));
for(int j = n-1; j>=0 ; j--)
{
long long new_index = indices[j].second;
//cout<<j<<" "<<new_index<<" "<<left<<" "<<right<<endl;
if(new_index > right)
{
for(int t = right +1; t<=new_index; t++)
{
y.insert(arr[t]);
x.erase(x.find(arr[t]));
}
right = new_index;
}
if(new_index < left)
{
for(int t = left -1; t>=new_index; t--)
{
y.insert(arr[t]);
x.erase(x.find(arr[t]));
}
left = new_index;
}
auto f = x.end();
f--;
long long curr_x_maximum = *f;
long long curr_x_minimum = *x.begin();
auto z = y.end();
z--;
long long curr_y_maximum = *z;
long long curr_y_minimum = *y.begin();
long long diff = curr_y_minimum - curr_x_minimum;
curr_y_maximum -= diff;
ans = min(ans, max(curr_y_maximum, curr_x_maximum) - curr_x_minimum);
}
cout<<ans<<endl;
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
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