其他分享
首页 > 其他分享> > C. Balanced Stone Heaps

C. Balanced Stone Heaps

作者:互联网

https://codeforces.com/problemset/problem/1623/C

题意:有n堆石子,第i堆石子有hi个石子,你可以从第3堆开始到第n堆,将d个石子移动到i - 1堆和2d个石子移动到i - 2堆,问最后最大的最小堆为多少。
题解:二分!!!(代码有说明!)

#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<math.h>
#include<vector>
#include<map>
#include<set>
using namespace std;

#define int long long
const int N = 2e5 + 10;
int h[N], hh[N];

signed main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		for (int i = 1; i <= n; i++) cin >> h[i];
		int l = 1, r = 1e9;
		while (l < r)//二分
		{
			int mid = l + r + 1 >> 1;
			int f = 1;
			for (int i = 1; i <= n; i++) hh[i] = h[i];//拷贝h数组,防止h数组被改变
			for (int i = n; i >= 3; i--)
			{
				if (hh[i] >= mid)//当前大于mid,所以可以将大于的部分移动到前面两堆
				{
					hh[i - 1] += min(h[i], hh[i] - mid) / 3;
					//取min的原因在于,他是从后往前的,所以对于原本的i堆应最多移动h[i]个石子
					hh[i - 2] += min(h[i], hh[i] - mid) / 3 * 2;
				}
				else
				{
					f = 0;
					break;
				}
			}
			if (hh[1] < mid || hh[2] < mid || f == 0)
			{
				r = mid - 1;
			}
			else l = mid;
		}
		cout << l << endl;
	}
	return 0;
}

标签:Heaps,Stone,min,int,石子,mid,hh,Balanced,include
来源: https://blog.csdn.net/Ja_king_/article/details/122714467