您的位置:首页 > 其它

poj sticks 木棍 枚举+搜索+小技巧

2015-12-03 18:29 267 查看
这大概是我复赛前看过的题了,那个时候的我其实还不是很会打搜索,甚至认为搜索,枚举的效率都是很低的,是不能够把题AC的,后来为了改变这种看法(其实是因为我发现一些我想不出的题的AC程序里大多有搜索,枚举的算法,格外揪心,没想到竟这么简单)。才做了一些搜索的题。这就是那些年曾令我揪心的题之一,想不出,去百度,这么简单,心碎了。的确搜索如果剪枝做的好的话,是可以把一些题目AC的。

、 好吧!可打完后提交的第一次还是TLE了,心好累。去别人的博客园看了一下,其实大家都差不多。仔细一看才发现别人是降序的,而我是升序的。这估计就是这题的一点小技巧吧!弄完后,AC了,既然经验在此,就积累了吧,毕竟积少成多。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<set>
#define maxn 70
#define rep(i,j,k) for(int i = j; i <= k; i++)
#define st stick[i]
using namespace std;

int stick[maxn] = {0}, n, used[maxn] = {0}, ch, mu;
int unable[maxn] = {0};

bool bmp(int a,int b)
{
return a > b;
}

inline int read()
{
int s = 0, t = 1;
char c = getchar();
while( !isdigit(c) ){
if( c == '-' ) t = -1;
c = getchar();
}
while( isdigit(c) ){
s = s * 10 + c - '0';
c = getchar();
}
return s * t;
}

bool match(int sheng,int x,int ci)
{
if( !sheng ) ci++, x = 0, sheng = ch;
if( ci == mu ) return 1;
rep(i,x,n-1){
if( st <= sheng && !used[i] ){
used[i] = 1;
if( match(sheng-st,i+1,ci) ) return 1;
else if( x == 0 )  {
used[i] = 0; return 0;
}
used[i] = 0;
int j = i; while( stick[j+1] == stick[i] ) j++; i = j;
}
}
return 0;
}

int main()
{
n;
while( scanf("%d", &n) == 1 && n ){
int sum = 0;
rep(i,0,n-1){
st = read();
sum += st;
used[i] = 0;
}
sort(stick,stick+n,bmp);
int maxl = stick[0];
rep(i,maxl,sum){
if( sum % i == 0 ){
ch = i;
mu = sum / i;
if( match(i,0,0) ){
cout<<i<<endl;
break;
}
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: