[AGC057B] 2A + x
Genius_Star · · 题解
或许更好的阅读体验。
思路:
先将
如果可以使得序列的极差
否则一定不会操作最大值,因为是倍增形的,每次操作后极差
于是只需要考虑不操作最大值的时候的解,即先将
则将
考虑怎么求距离
如果算出来这个极差
完整代码:
#include<bits/stdc++.h>
#define fi first
#define se second
#define lowbit(x) (x) & (-(x))
#define popcnt(x) __builtin_popcount(x)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int N = 1e5 + 10;
inline ll read(){
ll x = 0, f = 1;
char c = getchar();
while(c < '0' || c > '9'){
if(c == '-')
f = -1;
c = getchar();
}
while(c >= '0' && c <= '9'){
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return x * f;
}
inline void write(ll x){
if(x < 0){
putchar('-');
x = -x;
}
if(x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
struct Node{
ll l, r;
inline bool operator<(const Node&rhs)const{
return l < rhs.l;
}
}A[N];
ll ans;
ll n, X, cnt;
ll a[N];
int main(){
n = read(), X = read();
for(int i = 1; i <= n; ++i)
a[i] = read();
sort(a + 1, a + n + 1);
ans = a[n] - a[1];
for(int i = 1; i < n; ++i){
bool flag = 0;
ll l = a[i], r = a[i];
ll L = 0, R = 0;
while(1){
if(l <= a[n] && a[n] <= r){
flag = 1;
break;
}
if(l > a[n]){
L = (r - X) >> 1;
R = l;
break;
}
l = (l << 1), r = (r << 1) + X;
}
if(flag)
continue;
A[++cnt] = {L, R};
// cerr << L << ' ' << R << '\n';
}
sort(A + 1, A + cnt + 1);
A[++cnt] = {a[n], a[n]};
ll mx = a[n];
for(int i = 1; i <= cnt; ++i){
ans = min(ans, mx - A[i].l);
mx = max(mx, A[i].r);
}
write(ans < X ? 0 : ans);
return 0;
}