CF1856C To Become Max 题解 贪心

To Become Max

传送门

You are given an array of integers a a a of length n n n.

In one operation you:

  • Choose an index i i i such that 1 ≤ i ≤ n − 1 1 \le i \le n - 1 1≤i≤n−1 and a i ≤ a i + 1 a_i \le a_{i + 1} ai≤ai+1.
  • Increase a i a_i ai by 1 1 1.

Find the maximum possible value of max ⁡ ( a 1 , a 2 , ... a n ) \max(a_1, a_2, \ldots a_n) max(a1,a2,...an) that you can get after performing this operation at most k k k times.

Input

Each test contains multiple test cases. The first line of input contains a single integer t t t ( 1 ≤ t ≤ 100 1 \le t \le 100 1≤t≤100) --- the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers n n n and k k k ( 2 ≤ n ≤ 1000 2 \le n \le 1000 2≤n≤1000, 1 ≤ k ≤ 1 0 8 1 \le k \le 10^{8} 1≤k≤108) --- the length of the array a a a and the maximum number of operations that can be performed.

The second line of each test case contains n n n integers a 1 , a 2 , ... , a n a_1,a_2,\ldots,a_n a1,a2,...,an ( 1 ≤ a i ≤ 1 0 8 1 \le a_i \le 10^{8} 1≤ai≤108) --- the elements of the array a a a.

It is guaranteed that the sum of n n n over all test cases does not exceed 1000 1000 1000.

Output

For each test case output a single integer --- the maximum possible maximum of the array after performing at most k k k operations.

Example

input

6
3 4
1 3 3
5 6
1 3 4 5 1
4 13
1 1 3 179
5 3
4 3 2 2 2
5 6
6 5 4 1 5
2 17
3 5

output

4
7
179
5
7
6

Note

In the first test case, one possible optimal sequence of operations is: [ 1 , 3 , 3 ] → [ 2 , 3 , 3 ] → [ 2 , 4 , 3 ] → [ 3 , 4 , 3 ] → [ 4 , 4 , 3 ] [\textcolor{red}{1}, 3, 3] \rightarrow [2, \textcolor{red}{3}, 3] \rightarrow [\textcolor{red}{2}, 4, 3] \rightarrow [\textcolor{red}{3}, 4, 3] \rightarrow [4, 4, 3] [1,3,3]→[2,3,3]→[2,4,3]→[3,4,3]→[4,4,3].

In the second test case, one possible optimal sequence of operations is: [ 1 , 3 , 4 , 5 , 1 ] → [ 1 , 4 , 4 , 5 , 1 ] → [ 1 , 5 , 4 , 5 , 1 ] → [ 1 , 5 , 5 , 5 , 1 ] → [ 1 , 5 , 6 , 5 , 1 ] → [ 1 , 6 , 6 , 5 , 1 ] → [ 1 , 7 , 6 , 5 , 1 ] [1, \textcolor{red}{3}, 4, 5, 1] \rightarrow [1, \textcolor{red}{4}, 4, 5, 1] \rightarrow [1, 5, \textcolor{red}{4}, 5, 1] \rightarrow [1, 5, \textcolor{red}{5}, 5, 1] \rightarrow [1, \textcolor{red}{5}, 6, 5, 1] \rightarrow [1, \textcolor{red}{6}, 6, 5, 1] \rightarrow [1, 7, 6, 5, 1] [1,3,4,5,1]→[1,4,4,5,1]→[1,5,4,5,1]→[1,5,5,5,1]→[1,5,6,5,1]→[1,6,6,5,1]→[1,7,6,5,1].

以上来自 C o d e F o r c e s 以上来自CodeForces 以上来自CodeForces

题目翻译

给你一个长度为 n n n 的整数数组 a a a 。

在一次操作中,你

  • 选择一个索引 i i i ,使 1 ≤ i ≤ n − 1 1 \le i \le n - 1 1≤i≤n−1 和 a i ≤ a i + 1 a_i \le a_{i + 1} ai≤ai+1 .
  • 将 a i a_i ai 增加 1 1 1 。

求最多执行 k k k 次这一操作后, max ⁡ ( a 1 , a 2 , ... a n ) \max(a_1, a_2, \ldots a_n) max(a1,a2,...an) 的最大可能值。

输入格式

每个测试包含多个测试用例。第一行输入包含一个整数 t t t ( 1 ≤ t ≤ 100 1 \le t \le 100 1≤t≤100 ) - 测试用例的个数。测试用例说明如下。

每个测试用例的第一行包含两个整数 n n n 和 k k k ( 2 ≤ n ≤ 1000 2 \le n \le 1000 2≤n≤1000 , 1 ≤ k ≤ 1 0 8 1 \le k \le 10^{8} 1≤k≤108 )--数组的长度 a a a 和可执行的最大操作数。

每个测试用例的第二行包含 n n n 个整数 a 1 , a 2 , ... , a n a_1,a_2,\ldots,a_n a1,a2,...,an ( 1 ≤ a i ≤ 1 0 8 1 \le a_i \le 10^{8} 1≤ai≤108 ) - 数组 a a a 的元素。

保证所有测试用例的 n n n 之和不超过 1000 1000 1000 。

输出格式

对每个测试用例输出一个整数,即数组最多执行 k k k 次操作后的最大值。

提示

在第一个测试案例中,一个可能的最佳操作顺序是 [ 1 , 3 , 3 ] → [ 2 , 3 , 3 ] → [ 2 , 4 , 3 ] → [ 3 , 4 , 3 ] → [ 4 , 4 , 3 ] [\textcolor{red}{1}, 3, 3] \rightarrow [2, \textcolor{red}{3}, 3] \rightarrow [\textcolor{red}{2}, 4, 3] \rightarrow [\textcolor{red}{3}, 4, 3] \rightarrow [4, 4, 3] [1,3,3]→[2,3,3]→[2,4,3]→[3,4,3]→[4,4,3] .

在第二个测试案例中,一个可能的最佳操作序列是: [ 1 , 3 , 4 , 5 , 1 ] → [ 1 , 4 , 4 , 5 , 1 ] → [ 1 , 5 , 4 , 5 , 1 ] → [ 1 , 5 , 5 , 5 , 1 ] → [ 1 , 5 , 6 , 5 , 1 ] → [ 1 , 6 , 6 , 5 , 1 ] → [ 1 , 7 , 6 , 5 , 1 ] [1, \textcolor{red}{3}, 4, 5, 1] \rightarrow [1, \textcolor{red}{4}, 4, 5, 1] \rightarrow [1, 5, \textcolor{red}{4}, 5, 1] \rightarrow [1, 5, \textcolor{red}{5}, 5, 1] \rightarrow [1, \textcolor{red}{5}, 6, 5, 1] \rightarrow [1, \textcolor{red}{6}, 6, 5, 1] \rightarrow [1, 7, 6, 5, 1] [1,3,4,5,1]→[1,4,4,5,1]→[1,5,4,5,1]→[1,5,5,5,1]→[1,5,6,5,1]→[1,6,6,5,1]→[1,7,6,5,1].

翻译: D e e p L 翻译:DeepL 翻译:DeepL

解题思路

先看数据范围: 2 ≤ n ≤ 1000 2 \le n \le 1000 2≤n≤1000,那么直接使用贪心。

暴力思想

枚举最终成为答案的数 a i a_i ai。一开始肯定要利用 a i + 1 a_{i+1} ai+1 让 a i a_i ai 变得尽可能大,这样的代价是最小的。当 a i > a i + 1 a_i>a_{i+1} ai>ai+1 时怎么办?利用 a i + 2 a_{i+2} ai+2 让 a i + 1 a_{i+1} ai+1 变大。

优化(贪心)

如果操作次数足够多,那么最终的序列肯定是形如 x , x − 1 , ⋯ , a n + 1 , a n x,x−1,⋯,a_n+1,a_n x,x−1,⋯,an+1,an 的。也就是说,依次枚举 j ∈ [ i + 1 , n ] j \in [i+1,n] j∈[i+1,n],当 a j ≥ a j − 1 a_j\ge a_{j−1} aj≥aj−1 时,至多就可以让 a i a_i ai 变大 a j − a j − 1 + 1 a_j-a_{j−1}+1 aj−aj−1+1,此时更新答案即可。如果不满足, a j a_j aj 在后续的更新过程中肯定要变成 a j − 1 − 1 a_{j−1}−1 aj−1−1,否则无法使 a i a_i ai 变得更大,提前更新 a j a_j aj 即可。

时间复杂度 O ( n 2 ) O(n^2) O(n2),写二分的是不是惊呆了。

AC Code

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int Maxn = 1000 + 5;
int n, k, a[Maxn];
int b[Maxn];
int ans;
inline int Binary_search(int n, int k);
inline void solve();
inline void work() {
	int T;
	cin >> T;
	while (T--) {
		solve();
	}
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	work();
	return 0;
}
inline void solve() {
	cin >> n >> k;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	ans = Binary_search(n, k);
	cout << ans << endl;
}
inline int Binary_search(int n, int k) {
	int tmp, t;
	int ans = a[n];
	for (int i = n - 1; i >= 1; i--) {
		for (int j = 1; j <= n; j++) {
			b[j] = a[j];
		}
		t = k;
		for (int j = i + 1; j <= n && t > 0; j++) {
			if (b[j] < b[j - 1]) {
				t -= b[j - 1] - 1 - b[j];
				b[j] = b[j - 1] - 1;
			} else {
				tmp = min(1ll * (b[j] - b[j - 1] + 1) * (j - i), t);
				t -= tmp;
				b[i] += tmp / (j - i);
			}
			ans = max(ans, b[i]);
		}
	}
	return ans;
}

吐槽一下洛谷对于该题的提示的染色问题。详见此处

相关推荐
迷迭所归处15 分钟前
C++ —— 关于vector
开发语言·c++·算法
leon62544 分钟前
优化算法(一)—遗传算法(Genetic Algorithm)附MATLAB程序
开发语言·算法·matlab
CV工程师小林1 小时前
【算法】BFS 系列之边权为 1 的最短路问题
数据结构·c++·算法·leetcode·宽度优先
Navigator_Z1 小时前
数据结构C //线性表(链表)ADT结构及相关函数
c语言·数据结构·算法·链表
Aic山鱼1 小时前
【如何高效学习数据结构:构建编程的坚实基石】
数据结构·学习·算法
white__ice2 小时前
2024.9.19
c++
天玑y2 小时前
算法设计与分析(背包问题
c++·经验分享·笔记·学习·算法·leetcode·蓝桥杯
姜太公钓鲸2332 小时前
c++ static(详解)
开发语言·c++
菜菜想进步2 小时前
内存管理(C++版)
c语言·开发语言·c++
sjsjs112 小时前
【数据结构-一维差分】力扣1893. 检查是否区域内所有整数都被覆盖
数据结构·算法·leetcode