QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#478346#7944. Max Minus Minembusca#WA 19ms3680kbC++173.2kb2024-07-14 21:11:242024-07-14 21:11:26

Judging History

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

  • [2024-07-14 21:11:26]
  • 评测
  • 测评结果:WA
  • 用时:19ms
  • 内存:3680kb
  • [2024-07-14 21:11:24]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rp(i,a,b)        for(int i=a;i<b;i++)

ll LOG = 24, INF = 1e10;
int n;
vector<pair<int, int>> indices;
vector<ll> nums;
struct sparse{
    vector<vector<array<ll, 2>>> table;
 
    void init(){
        table.resize(n+1, vector<array<ll, 2>>(LOG));
        for (int i = 0; i < n; i++){
            table[i][0] = {indices[i].second, indices[i].second};
        }
 
        for (int j = 1; (1 << j) <= n; j++) {
            for (int i = 0; i + (1 << j) <= n; i++) {
                table[i][j][0] = min(table[i][j - 1][0], table[i + (1 << (j - 1))][j - 1][0]);
                table[i][j][1] = max(table[i][j - 1][1], table[i + (1 << (j - 1))][j - 1][1]);
            }
        }
        
    }
    array<ll, 2> query(ll L, ll R) {
        ll j = log2(R - L + 1);
        return {min(table[L][j][0], table[R - (1 << j) + 1][j][0]), max(table[L][j][1], table[R - (1 << j) + 1][j][1])};
    }
};

bool veri(int l, int r, int val, int mid){
    ll mx=0, mn=1e9;
    rp(i, 0, n){
        if(l <= i && i <= r){
            mx = max(mx, nums[i]+val);
            mn = min(mn, nums[i]+val);
        }
        else{
            mx = max(mx, nums[i]);
            mn = min(mn, nums[i]);
        }
    }
    return mx-mn <= mid;
}

void solvetask(){
    cin >> n;
    nums.resize(n);
    rp(i, 0, n) cin >> nums[i];
    indices.resize(n);
    rp(i, 0, n){
        indices[i].first = nums[i];
        indices[i].second = i;
    }
    sort(indices.begin(), indices.end());
    sparse table;
    table.init();
    int l = 0, r = 1e9, resp1 = 1e9, resp2 = 1e9;
    while(l <= r){
        int L = 0, R = n-1, id = -1;
        int mid = (l+r)/2;
        while(L <= R){
            int MID = (L+R)/2;
            if(indices[MID].first-indices[0].first > mid){
                id = MID;
                R = MID-1;
            }
            else L = MID+1;
        }
        int li, ri;
        if(id != -1){
            auto temp = table.query(id, n-1);
            li = temp[0], ri = temp[1];
        }
        //cout << li << " " << ri << " " << resp1 << " " << mid << " " << id << "\n";
        int val;
        if(id != -1) val = indices[0].first - indices[id].first;
        if(id == -1 || veri(li, ri, val, mid)){
            resp1 = mid;
            r = mid-1;
        }
        else l = mid+1;
    }

    l = 0; r = 1e9;
    while(l <= r){
        int L = 0, R = n-1, id = -1;
        int mid = (l+r)/2;
        while(L <= R){
            int MID = (L+R)/2;
            if(indices.back().first-indices[MID].first > mid){
                id = MID;
                R = MID-1;
            }
            else L = MID+1;
        }
        int li, ri;
        if(id != -1){
            auto temp = table.query(0, id);
            li = temp[0], ri = temp[1];
        }
        
        int val;
        if(id != -1) val = indices[0].first - indices[id].first;
        if(id == -1 || veri(li, ri, val, mid)){
            resp2 = mid;
            r = mid-1;
        }
        else l = mid+1;
    }
    cout << min(resp1, resp2) << "\n";
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    ll t = 1;
    cin >> t;
    while(t--) solvetask();
}

详细

Test #1:

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

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: 19ms
memory: 3680kb

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

result:

wrong answer 2nd numbers differ - expected: '2', found: '1'