QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#558812#7944. Max Minus Minshstyle#WA 10ms5932kbC++201.9kb2024-09-11 18:40:012024-09-11 18:40:01

Judging History

你现在查看的是最新测评结果

  • [2024-09-11 18:40:01]
  • 评测
  • 测评结果:WA
  • 用时:10ms
  • 内存:5932kb
  • [2024-09-11 18:40:01]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=1919810;
typedef long long ll;


ll a[N];
ll n;
int b[N];

ll mx,mn;


bool check1(ll mid){
    ll mx1=0;
    for(int i=1;i<=n;i++){
        if(a[i]<mid){
            b[i]=0;
            mx1=max(mx1,a[i]);
        }
        else b[i]=1;
    }
    //cout<<mx1<<endl;
    //for(int i=1;i<=n;i++) cout<<b[i]<<" ";
    //cout<<endl;
    ll need=mid-mn;
    if(mx1+need>mx){
        return false;
    }
    int j=1;
    int cnt0=0;
    for(int i=1;i<=n;i++){
        if(b[i]!=b[j]){
            if(b[j]==0) cnt0++;
            j=i;
        }
    }
    //cout<<cnt0<<endl;
    if(a[j]==0) cnt0++;
    return cnt0<=1;


}


ll gao1(){
    ll l=mn,r=mx;
    while(l<r){
        ll mid=l+r+1>>1;
        if(check1(mid)) l=mid;
        else r=mid-1;
    }
    //cout<<l<<endl;
    return mx-l;
}


bool check2(ll mid){
    ll mn1=1e18;
    for(int i=1;i<=n;i++){
        if(a[i]>mid) {
            b[i]=1;
            mn1=min(mn1,a[i]);
        }else b[i]=0;
    }

    ll need=mx-mid;
    if(mn1-need<mn) return false;


    int j=1;
    int cnt1=0;
    for(int i=1;i<=n;i++){
        if(b[i]!=b[j]){
            if(b[j]==1) cnt1++;
            j=i;
        }
    }
    if(b[j]==1) cnt1++;
    return cnt1<=1;

}


ll gao2(){
    ll l=mn,r=mx;
    while(l<r){
        ll mid=l+r>>1;
        if(check2(mid)) r=mid;
        else l=mid+1;
    }
    //cout<<l<<endl;
    return l-mn;
}

void __(){
    mn=1e18,mx=0;
    scanf("%lld",&n);
    for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
    for(int i=1;i<=n;i++){
        mx=max(mx,a[i]);
        mn=min(mn,a[i]);
    }
    if(n==1) puts("0");
    else{
        ll ans=1e18;
        //check1(100);
        ans=min(ans,gao1());
        ans=min(ans,gao2());
        printf("%lld\n",ans);
    }
}

int main(){
    int _;
    cin>>_;
    while(_--){
        __();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5932kb

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: 10ms
memory: 5860kb

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
1
1
1
2
0
2
2
3
1
0
2
1
2
1
2
0
1
1
2
0
2
2
1
3
2
2
2
1
3
2
2
1
2
2
2
2
2
2
1
2
1
1
1
3
2
2
1
1
2
1
1
1
1
1
1
1
2
1
1
3
1
2
2
3
2
2
1
1
2
1
2
3
2
0
2
1
1
1
1
1
2
2
2
1
3
1
1
2
3
2
1
1
2
2
3
1
1
1
2
2
1
1
1
1
2
2
2
2
2
4
2
1
1
0
1
1
1
1
1
1
2
2
2
2
1
1
1
2
1
2
4
1
1
1
2
2
1
2
1
2
1
2
1
2
2
1
3
...

result:

wrong answer 4th numbers differ - expected: '3', found: '1'