【Codeforces Round 262 (Div 2)C】【二分】浇花使得最小的尽可能大

本文探讨了一种通过二分搜索和贪心算法解决编程问题的方法,具体场景是在有限时间内通过浇水操作来最大化最小花朵的高度。通过实例分析,展示了如何应用算法解决实际问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Present
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Little beaver is a beginner programmer, so informatics is his favorite subject. Soon his informatics teacher is going to have a birthday and the beaver has decided to prepare a present for her. He planted n flowers in a row on his windowsill and started waiting for them to grow. However, after some time the beaver noticed that the flowers stopped growing. The beaver thinks it is bad manners to present little flowers. So he decided to come up with some solutions.

There are m days left to the birthday. The height of the i-th flower (assume that the flowers in the row are numbered from 1 to n from left to right) is equal to ai at the moment. At each of the remaining m days the beaver can take a special watering and water wcontiguous flowers (he can do that only once at a day). At that each watered flower grows by one height unit on that day. The beaver wants the height of the smallest flower be as large as possible in the end. What maximum height of the smallest flower can he get?

Input

The first line contains space-separated integers nm and w (1 ≤ w ≤ n ≤ 105; 1 ≤ m ≤ 105). The second line contains space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).

Output

Print a single integer — the maximum final height of the smallest flower.

Examples
input
6 2 3
2 2 2 2 1 1
output
2
input
2 5 1
5 8
output
9
Note

In the first sample beaver can water the last 3 flowers at the first day. On the next day he may not to water flowers at all. In the end he will get the following heights: [2, 2, 2, 3, 2, 2]. The smallest flower has height equal to 2. It's impossible to get height 3 in this test.



#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 2e5 + 10 , M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int n, m, w;
int a[N];
int v[N];
bool check(int len)
{
	MS(v, 0);
	int use = 0;
	int val = 0;
	for (int i = 1; i <= n; ++i)
	{
		val += v[i];
		if (a[i] + val < len)
		{
			int need = len - (a[i] + val);
			use += need;
			if (use > m)return 0;
			val += need;
			v[i + w] -= need;
		}
	}
	return 1;
}
int main()
{
	while (~scanf("%d%d%d", &n,&m,&w))
	{
		int l = 1e9;
		int r = 1e9 + m;
		for (int i = 1; i <= n; ++i)
		{
			scanf("%d", &a[i]);
			gmin(l, a[i]);
		}
		while (l < r)
		{
			int mid = (l + r + 1) >> 1;
			if (check(mid))l = mid;
			else r = mid - 1;
		}
		printf("%d\n", l);
	}
	return 0;
}
/*
【trick&&吐槽】
1,使得最小的尽可能大,显然就是二分。
2,细节不要写错了。

【题意】
有一排花,n(1e5)朵。
每朵花都有一个成长度a[](1<=a[]<=1e9)
我们可以浇花m(1e5)次,每次连续的长度为w(1e5)的一段,
我们想使得最小成长度的花的成长度尽可能高,输出。

【类型】
二分 贪心

【分析】
显然可以二分答案+贪心。
然后就没有然后了。
注意二分细节,防止爆int

*/


### Codeforces Round 1028 (Div. 2) 题目概述 Codeforces Round 1028 (Div. 2) 是一场包含多个算法问题的比赛,涵盖了从简单到复杂的不同难度级别。以下是该比赛的部分题目内容及简要说明: #### A. Gellyfish and Flower 在本题中,有两个角色:Gellyfish 和 Flower。每个角色有两滴血量,分别用 \(a\) 和 \(c\) 表示 Gellyfish 的血量,用 \(b\) 和 \(d\) 表示 Flower 的血量[^3]。 获胜条件是攻击对方的最低血量,并比较双方的最低血量值。如果 Gellyfish 的最低血量小于 Flower 的最低血量,则 Gellyfish 获胜;否则,Flower 获胜。 #### B. Problem - B - Codeforces B 题的具体内容未完全提供,但通常涉及数组、字符串或其他数据结构的操作。可以参考比赛页面获取完整描述。 #### C. Rectangles C 题要求处理矩形的相关问题。具体来说,给定一些矩形的边长信息,需要判断某些条件是否满足。时间限制为每组测试用例 2 秒,内存限制为 256 MB[^2]。输入和输出均为标准格式,详细规则可参考比赛页面。 ### 示例代码(A 题) 以下是一个实现 A 题逻辑的 C++ 程序: ```cpp #include <iostream> #include <algorithm> using namespace std; int main() { int t; cin >> t; while (t--) { int a, b, c, d; cin >> a >> b >> c >> d; int mina = min(a, c); int minb = min(b, d); if (mina < minb) cout << "Flower" << endl; else cout << "Gellyfish" << endl; } } ``` 此代码通过多次循环读取输入并计算两个角色的最低血量,最终输出获胜者名称。 ### 注意事项 - 比赛中的所有题目均可以通过官方链接访问,例如 [Codeforces Round 1028 (Div. 2)](http://codeforces.com/contest/1028)[^2]。 - 每道题目的详细规则和样例输入输出可在对应页面找到。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值