QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#586621#7944. Max Minus MinMath4Life2020WA 12ms3640kbC++202.5kb2024-09-24 14:42:152024-09-24 14:42:15

Judging History

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

  • [2024-09-24 14:42:15]
  • 评测
  • 测评结果:WA
  • 用时:12ms
  • 内存:3640kb
  • [2024-09-24 14:42:15]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long; using pii = pair<ll,ll>;
const ll INF = INT_MAX;

vector<ll> stmax,stmin;
ll E; ll Nm;

ll e2(ll x) {
    return (1LL<<x);
}

ll l2(ll x) {
    return (31-__builtin_clz(x));
}

ll v2(ll x) {
    if (x==0) {
        return 100;
    }
    return __builtin_ctz(x);
}

ll qmax(ll a, ll b) {
    if (a>b) {
        return -INF;
    }
    ll va = v2(a); ll vb = v2(b+1);
    if (va>vb) {
        return max(qmax(a,b-e2(vb)),stmax[(b>>vb)+e2(E-vb)]);
    } else {
        return max(qmax(a+e2(va),b),stmax[(a>>va)+e2(E-va)]);
    }
}

ll qmin(ll a, ll b) {
    if (a>b) {
        return INF;
    }
    ll va = v2(a); ll vb = v2(b+1);
    if (va>vb) {
        return min(qmin(a,b-e2(vb)),stmin[(b>>vb)+e2(E-vb)]);
    } else {
        return min(qmin(a+e2(va),b),stmin[(a>>va)+e2(E-va)]);
    }
}

void solve() {
    //freopen("input.txt","r",stdin);
    ll N; cin >> N;
    stmax.clear();
    stmin.clear();
    if (N==1) {
        ll a; cin >> a;
        cout << "0\n";
        return;
    }
    E = l2(N-1)+1;
    Nm = e2(E);
    for (ll i=0;i<(2*Nm);i++) {
        stmax.push_back(0);
        stmin.push_back(0);
    }
    for (ll i=0;i<N;i++) {
        cin >> stmax[i+Nm];
        stmin[i+Nm]=stmax[i+Nm];
    }
    for (ll d=(Nm-1);d>=1;d--) {
        stmax[d]=max(stmax[2*d],stmax[2*d+1]);
        stmin[d]=min(stmin[2*d],stmin[2*d+1]);
    }
    ll ans = stmax[1]-stmin[1];
    for (ll x=0;x<N;x++) {
        ll ymin=x; ll ymax=N-1;
        while (ymin!=ymax) {
            ll yt = (ymin+ymax+1)/2;
            ll d1 = max(qmax(0,x-1),qmax(yt+1,N-1))-min(qmin(0,x-1),qmin(yt+1,N-1));
            if (d1<0) {
                ymax=yt-1; continue;
            }
            ll d2 = qmax(x,yt)-qmin(x,yt);
            //cout << "x,yt,d1,d2="<<x<<","<<yt<<","<<d1<<","<<d2<<"\n";
            ans = min(ans,max(d1,d2));
            if (d1<d2) {
                ymax=yt-1;
            } else {
                ymin=yt;
            }
        }
        ll yt = ymin-1;
        //cout << "x,yt="<<x<<","<<yt<<"\n";
        if (yt<x) {
            continue;
        }
        ll d1 = max(qmax(0,x-1),qmax(yt+1,N-1))-min(qmin(0,x-1),qmin(yt+1,N-1));
        if (d1<0) {
            continue;
        }
        ll d2 = qmax(x,yt)-qmin(x,yt);
        ans = min(ans,max(d1,d2));
    }
    cout << ans<<"\n";
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    ll T; cin >> T;
    while (T--) {
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3640kb

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: 3624kb

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

result:

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