【NOIP2016提高A组8.11】钱仓

分析

发现,一定有一个点作为起点,所有的路径都不经过这个起点。
接着贪心求答案,
如果\(c_i>1\),将其中\(c_i-1\)个钱往后“铺”。
易证\(x^2+y^2<=(x+y)^2\)
那么维护一个队列,先进先出,就能保证最小。

#include <cmath>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
const int maxlongint=2147483647;
const int mo=1000000007;
const int N=150005;
using namespace std;
int d[N],c[N*2],n,m,tot,mak[N];
long long ans;
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&c[i]);
		c[i+n]=c[i];
	}
	for(int i=1;i<=n;i++)
	{
		tot=ans=0;
		int sum=0,head=1;
		for(int j=1;j<=n;j++)
		{
			mak[j]=c[i+j-1];
			sum+=mak[j];
			if(sum<j) 
			{
				ans=-1;
				break;
			}
		}
		if(ans>=0)
		for(int j=1;j<=n;j++)
		{
			while(mak[j]--) 
			{
				d[++tot]=j;
			}
			mak[j]++;
			ans+=(j-d[head])*(j-d[head]);
			head++;
		}
		if(ans>=0) 
		{
			printf("%lld",ans);
			return 0;
		}
	}
}

相关文章:

  • 2021-10-07
  • 2021-07-24
  • 2021-11-25
  • 2021-08-19
  • 2021-06-11
  • 2021-05-25
  • 2021-10-21
  • 2021-08-17
猜你喜欢
  • 2021-11-19
  • 2021-11-27
  • 2021-12-01
  • 2021-12-11
  • 2021-11-24
  • 2021-10-08
  • 2022-01-20
相关资源
相似解决方案