帮助后人
  • 板块P1120 小木棍
  • 楼主cmrhhh
  • 当前回复3
  • 已保存回复3
  • 发布时间2024/4/10 15:23
  • 上次更新2024/4/10 18:56:03
查看原帖
帮助后人
572587
cmrhhh楼主2024/4/10 15:23
//此题实在毒瘤,附数据 
/*
优化:
0.快读快写 
1.用桶装 
2.if(ps==l){dfs(x+1,0,max1);return ;}里面的return 很关键!
3.一些卡常
4.一些变量的空间上优化
5.<=50的数据 MAXN的大小
6.玄学因素 
*/ 
#include<bits/stdc++.h>
using namespace std;
const int MAXN=520;//优化一 //优化二  用桶装 
int n,cnt,a[MAXN],l,max1=0,min1=INT_MAX;
inline int read()
{
	int r=0,f=1;char c=getchar();
	while((c<'0'||c>'9')&&(c!='-')) c=getchar();
	if(c=='-') f=-1,c=getchar();
	while(c>='0'&&c<='9') r=r*10+c-'0',c=getchar();
	return r*f; 
}
template<typename T> void print(T x){
	if(!x) return;
	print(x/10);
	putchar((x%10)+'0');
}
inline void dfs(int x,int ps,int pre){//合成第x根 拼到多少长度 上一小根长度	
	if(x==cnt+1){print(l);exit(0);} 	
	if(ps==l){dfs(x+1,0,max1);return ;}
	for(register int i=pre;i>=min1;i--){
		if(!a[i]||i+ps>l)continue;
		a[i]--;
		dfs(x,ps+i,i);
		a[i]++;
		if(!ps||ps+i==l)break ;//剪枝三 //剪枝四 
	}
	return ;
}
int main() {
	ios::sync_with_stdio(0),cin.tie(),cout.tie(0);
	n=read();
	int sum=0,t;
	for(register int i=1;i<n+1;++i){
		t=read();a[t]++;sum+=t;max1=max(max1,t);min1=min(min1,t);
	}
	for(l=min1;l<=sum;++l){
		cnt=sum/l;
		if(sum%l||cnt>n)continue; 
		dfs(1,0,max1);//INTMAX 优化五 
	}
	return 0;
}
/*
24
26 43 23 20 19 6 34 25 21 22 48 47 38 28 27 3 25 13 36 27 15 32 26 26
*/ //70
/*
59
6 6 2 1 8 1 7 8 3 6 8 1 3 5 2 5 3 1 2 4 5 3 2 4 3 7 1 3 8 1 6 3 5 6 3 1 5 2 2 3 8 4 8 5 8 3 3 2 5 7 5 2 1 8 6 8 2 7 3
*/ //10 
/*
7
20 14 13 19 10 9 3
*/ //44
/*
41
36 43 47 41 50 30 32 38 38 49 31 48 49 43 49 50 35 32 28 33 49 43 47 37 50 44 40 45 44 34 31 41 50 39 38 44 46 28 50 38 40
*/ //168
/*
63
36 48 10 23 5 49 46 41 34 42 24 47 2 17 21 50 23 25 6 9 45 22 35 5 35 35 6 35 22 14 33 13 44 25 10 5 37 4 44 16 33 35 47 49 29 18 39 5 25 39 48 4 24 38 6 6 16 42 13 1 50 18 4
*/ //96
/*
4
2 44 12 34
*/ //46
2024/4/10 15:23
加载中...