AtCoder - abc457_d Raise Minimum

AT_abc457_d [ABC457D] Raise Minimum

题目描述

You are given a sequence $ A = (A_1, A_2, \ldots, A_N) $ of length $ N $ and an integer $ K $ .

You can perform the following operation between $ 0 $ and $ K $ times, inclusive.

  • Choose an integer $ i $ satisfying $ 1 \le i \le N $ , and add $ i $ to $ A_i $ .

Find the maximum possible value of $ \displaystyle \min_{1 \le i \le N} A_i $ for the sequence after the operations.

输入格式

The input is given from Standard Input in the following format:

$ N $ $ K $ $ A_1 $ $ A_2 $ $ \ldots $ $ A_N $

输出格式

Output the answer.

输入输出样例 #1

输入 #1

3 3
1 2 3

输出 #1

3

输入输出样例 #2

输入 #2

4 5
10 1 10 1

输出 #2

7

输入输出样例 #3

输入 #3

20 457
8 9 10 9 8 8 4 6 8 1 5 10 2 8 2 6 8 1 6 6

输出 #3

132

说明/提示

Sample Explanation 1

For example, by choosing $ i = 1 $ twice and $ i = 2 $ once, the sequence becomes $ (3, 4, 3) $ . In this case, the minimum value is $ 3 $ .

It is impossible to make the minimum value $ 4 $ or greater, so output $ 3 $ .

Constraints

  • $ 1 \le N \le 2 \times 10^5 $
  • $ 1 \le A_i \le 10^{18} $
  • $ 1 \le K \le 10^{18} $
  • All input values are integers.

思路概述

“最小值最大”,一眼二分答案。

\(check\) 函数很简单,直接模拟,并记录进行操作的次数。

然后直接二分。

代码示例

#include <iostream>
#include <algorithm>
#define N 200010
using namespace std;
typedef long long ll;
int n;
ll k, a[N], l, r, mid;
bool check(ll val) {
	ll cnt = 0;
	for (int i = 1;i <= n;i++) {
		if (a[i] < val) {
			cnt += (val - a[i] + i - 1) / i;
			if (cnt > k) return false;
		}
	}
	return cnt <= k;
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n >> k;
	for (int i = 1;i <= n;i++) cin >> a[i];
	l = 1; r = a[1] + k + 1;
	while (l < r) {
		mid = (l + r) >> 1;
		if (check(mid)) l = mid + 1;
		else r = mid;
	}
	cout << l - 1;
	return 0;
}
// 不开long long见祖宗
posted @ 2026-05-14 20:50  naijil  阅读(19)  评论(0)    收藏  举报