Codeforces Round #262 (Div. 2) 460C. Present

題目連接:http://codeforces.com/problemset/problem/460/C

C. 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 w contiguous 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.

Sample test(s)
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.


題意:給出n朵花的初始的高度(a1, a2, ..., an),從左到右排列,最多澆水m天,每天只能澆一次,每次可以使連續的 w 朵花的高度增加單位長度1,問最後m天澆完水後最矮的花的高度最高是達到多少。

思路:從n朵花中最矮min{ai}和最高max{ai}+m之間二分枚舉高度。

代碼:

#include <iostream>
#include <cstdio>
#include <string.h>
#define MAX 100010 
using namespace std;

int n, m, w;  // n flowers, m days, w-width
int a[MAX];

bool check(int mid)
{  // O(n)的時間處理 n-w+1 個寬爲 w 的連續區間
	int grow[MAX];  // 每盆花的高度值
	int st[MAX];
	memset(grow,0,sizeof(grow));
	memset(st,0,sizeof(st));
	for (int i = 0; i < n; i++)
		grow[i] = a[i];

	int delta = 0;  
	int moves = m;
	for (int i = 0; i < n; i++)
	{
		delta -= st[i];    // 除去對區間外澆花的影響
		grow[i] += delta;  // w-width區間內的花全部生長
		if (grow[i] < mid)
		{
			int h = mid-grow[i];
			grow[i] = mid;
			delta += h;
			st[i+w<n?i+w:n] += h;  // O(1)時間
			moves -= h;
			if (moves < 0)
				return false;
		}
	}
	return true;
}

int main()
{
	cin >> n >> m >> w;
	int left = 0x7fffffff, right = 0;
	memset(a,0,sizeof(a));
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		if (a[i] < left) left = a[i];
		if (a[i] > right) right = a[i];
	}
	right += m;
	int ans;
	while (left <= right)
	{
		int mid = (left+right)>>1;
		if (check(mid)) {
			left = mid+1, ans = mid;
		} else right = mid-1;
	}
	cout << ans << endl;
	return 0;
}

最後想說的,check()函數真的寫得很巧妙,現在理解得也不是太透徹,是特別的數據結構嗎?

每天一點點積累吧!

參考:

1. CodeforcesRound #262 (Div. 2) Editorial http://codeforces.ru/blog/entry/13465?locale=en

2. http://blog.csdn.net/accepthjp/article/details/38737429

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章