E. Pavel and Triangles(贪心+证明)

2 , 4 , 8 , 8 首先2,4,8这种说拼不成三角形的,因为8太大了

. \color{Red}所以Ⅰ.三条相等的边

. , \color{orange}Ⅱ.两条相等的边,一条较小的边

, 那么从前往后用木棍,优先拼等腰三角形

因为这样可以不让前面的木棍浪费

证明

使 , 要使得答案最优,也就是用掉最多的木棒

, 当能拼等腰三角形时,肯定不去拼等边三角形

, 因为拼完后都是用掉三条边,但是现在枚举到的边肯定更优

, , 可以作为底,也可以作为腰,而以前留下来的只能作为底

因此一定是优先用掉以前的边

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=3e5+10;
const int inf=-1e18;
int n,a[maxn],ans,yu;
signed main()
{
	cin >> n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		if(a[i]/2>=yu)
		{
			ans+=yu,a[i]-=yu*2;
			ans+=a[i]/3,a[i]=a[i]%3;
			yu=a[i];
		}
		else	
		{
			ans+=a[i]/2,yu-=a[i]/2;
			a[i]=a[i]%2,yu+=a[i];
		}
	}
	cout<<ans;
}

猜你喜欢

转载自blog.csdn.net/jziwjxjd/article/details/106833370