QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#478392 | #7944. Max Minus Min | embusca# | WA | 21ms | 3884kb | C++20 | 3.2kb | 2024-07-14 22:00:31 | 2024-07-14 22:00:31 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rp(i,a,b) for(ll i=a;i<b;i++)
const ll LOG = 24, INF = 1e10;
ll n;
vector<pair<ll, ll>> indices;
vector<ll> nums;
struct sparse{
vector<vector<array<ll, 2>>> table;
void init(){
table.resize(n+1, vector<array<ll, 2>>(LOG));
for (ll i = 0; i < n; i++){
table[i][0] = {indices[i].second, indices[i].second};
}
for (ll j = 1; (1 << j) <= n; j++) {
for (ll 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(ll l, ll r, ll val, ll 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.assign(n, 0);
rp(i, 0, n) cin >> nums[i];
indices.assign(n, {0, 0});
rp(i, 0, n){
indices[i].first = nums[i];
indices[i].second = i;
}
sort(indices.begin(), indices.end());
sparse table;
table.init();
ll l = 0, r = 1e9, resp1 = 1e9, resp2 = 1e9;
while(l <= r){
ll L = 0, R = n-1, id = -1;
ll mid = (l+r)/2;
while(L <= R){
ll MID = (L+R)/2;
if(indices[MID].first-indices[0].first > mid){
id = MID;
R = MID-1;
}
else L = MID+1;
}
ll 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";
ll 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){
ll L = 0, R = n-1, id = -1;
ll mid = (l+r)/2;
while(L <= R){
ll MID = (L+R)/2;
if(indices.back().first-indices[MID].first > mid){
id = MID;
L = MID+1;
}
else R = MID-1;
}
ll li, ri;
if(id != -1){
auto temp = table.query(0, id);
li = temp[0], ri = temp[1];
}
ll val;
if(id != -1) val = indices.back().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: 3748kb
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: 21ms
memory: 3884kb
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 3 3 2 0 1 1 2 0 3 2 2 3 2 2 2 3 3 2 2 1 2 2 2 2 2 2 1 2 1 2 1 3 2 2 1 1 2 2 1 2 2 1 1 1 2 1 2 2 1 2 2 3 2 2 1 1 2 1 2 3 2 0 2 1 1 2 1 1 2 2 2 1 3 2 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 2 0 1 1 1 1 1 1 2 2 2 2 2 2 1 2 1 2 4 1 1 1 3 2 2 2 1 2 1 2 1 2 2 1 3 ...
result:
wrong answer 16th numbers differ - expected: '2', found: '3'