二分答案
可以发现一定是有解的,对于一个数组,每一次操作实际上是把和-1,最后一定可以变成一个01的序列
枚举最小值,然后统计上升和下降的次数。因为是统计最大的最小值,所以在二分答案偏小时统计答案。
#include#define INF 0x3f3f3f3f#define full(a, b) memset(a, b, sizeof a)#define FAST_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)using namespace std;typedef long long ll;inline int lowbit(int x){ return x & (-x); }inline int read(){ int ret = 0, w = 0; char ch = 0; while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); } while(isdigit(ch)) ret = (ret << 3) + (ret << 1) + (ch ^ 48), ch = getchar(); return w ? -ret : ret;}inline int gcd(int a, int b){ return b ? gcd(b, a % b) : a; }inline int lcm(int a, int b){ return a / gcd(a, b) * b; }template inline T max(T x, T y, T z){ return max(max(x, y), z); }template inline T min(T x, T y, T z){ return min(min(x, y), z); }template inline A fpow(A x, B p, C lyd){ A ans = 1; for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd; return ans;}const int N = 300005;int _, n, a[N];bool calc(int val){ ll x = 0, y = 0; for(int i = 1; i <= n; i ++){ if(a[i] > val) y += (a[i] - val) / 2; else if(a[i] < val) x += val - a[i]; } return x <= y;}int main(){ for(_ = read(); _; _ --){ n = read(); for(int i = 1; i <= n; i ++) a[i] = read(); sort(a + 1, a + n + 1); int l = 0, r = 1e8; while(l < r){ int mid = (l + r + 1) >> 1; if(calc(mid)) l = mid; else r = mid - 1; } printf("%d\n", l); } return 0;}