为什么最后一个点t了?
  • 板块P1120 小木棍
  • 楼主MerlinLee
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/10/8 19:31
  • 上次更新2024/10/8 21:17:55
查看原帖
为什么最后一个点t了?
1227659
MerlinLee楼主2024/10/8 19:31
#include<bits/stdc++.h>
using namespace std;
const int N=65;
int n,a[N],tmp,cnt=0,mx=-1,sum=0;
int len,ok=0,vis[N];
inline int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0' or ch>'9')
    {
        if(ch=='-')
          f=-1;
        ch=getchar();
    }
    while(ch>='0' and ch<='9')
      x=x*10+ch-'0',ch=getchar();
    return x*f;
}
bool cmp(int a,int b)
{
	return a>b;
}
void dfs(int tot,int l,int now)
{
	if(ok)
	  return;
	if(tot==cnt and l==0)
	{
		ok=1;
		return;
	}
	int last=-1;
	for(int i=now;i<=cnt;i++)
	{
		if(vis[i]==0 and l+a[i]<=len)
		{
			if(l+a[i]==last)
			  continue;
			last=l+a[i];
			vis[i]=1;
			if(l+a[i]==len)
			  dfs(tot+1,0,1);
			else
			  dfs(tot+1,l+a[i],i+1);
			vis[i]=0;
			if(l==0 or l+a[i]==len)
			  break;
		}
	}
}
int main()
{
	n=read();
	for(int i=1;i<=n;i++)
	{
		tmp=read();
		if(tmp<=50)
		{
			a[++cnt]=tmp;
			mx=max(mx,tmp);
			sum+=tmp;
		}
	}
	sort(a+1,a+1+cnt,cmp);
	for(len=mx;len<=sum;len++)
	{
		if(sum%len==0)
		{
			ok=0;
			dfs(0,0,1);
			if(ok)
			  break;
		}
	}
	printf("%d",len);
	return 0;
}
2024/10/8 19:31
加载中...