| G | 小红的数组操作(A组、B组) |
采用二分法,确定答案的上下界,在二分检查中比较数组转化成mid最大所需的步骤与k的大小。
注意若a[i] - mid %==0无需再加1 , % >= 1需要加1
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 10;
int n, k, x;
int a[N];bool check(int mid) {int num = 0;for (int i = 1; i <= n; i++) {if (a[i] > mid)num += (a[i] - mid) / x ;if((a[i] - mid) % x > 0)num++;//!!!!}return num > k;
}signed main() {cin >> n >> k >> x;int minn = 0x3f3f3f3f, maxn = -1;for (int i = 1; i <= n; i++) {cin >> a[i];if (a[i] < minn)minn = a[i];if (a[i] > maxn)maxn = a[i];}int l = minn - k * x, r = maxn;//cout << l << " "<< r << endl;while (l < r){int mid = (l + r) >> 1;//cout << mid << endl;if (check(mid))l = mid + 1;elser = mid ;}cout << l;return 0;
}
| H | 游游的不相邻取数(A组) |
待补题