| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
什么BT数据,官方数据都秒过了,提交还是TLE,怎么回事啊?FOR C++
#include <stdio.h>
#include <memory.h>
#define MaxN 65
#define MaxL 70
int n;
int a[MaxN],max_a,sum_a;
int i,j;
int ps[MaxL*MaxN];
int pm;
int counts[MaxL];
int len;
int PreJudge(int kx)
{
int ix,jx;
int ax[MaxN],m;
m=0;
for (ix=1;ix<=MaxL-1;ix++)
{
if (counts[ix]) pm=ix;
for (jx=1;jx<=counts[ix];jx++)
{
m++;
ax[m]=ix;
}
}
memset(ps,0,sizeof(ps));
ps[0]=1;
for (ix=1;ix<=m;ix++)
{
for (jx=kx*len;jx>=0;jx--) if (ps[jx-ax[ix]]) ps[jx]=1;
}
for (ix=1;ix<=kx;ix++) if (ps[ix*len]==0) return 0;
return 1;
}
int Solve(int nx);
int Grope(int nx)
{
int num[MaxN];
int sum;
int px,pmc;
sum=0;
memset(num,0,sizeof(num));
px=1;
num[px]=pm+1;
pmc=counts[pm];
while (px>0)
{
if (sum>=len)
{
if (sum==len && pmc>counts[pm])
{
if (Solve(nx-1))
{
/*
for (int ix=1;ix<=px-2;ix++) printf("%d+",num[ix]);
printf("%d=%d\n",num[px-1],len);
*/
return 1;
}
}
px--;
counts[num[px]]++;
sum=sum-num[px];
}
else
{
do {num[px]--;} while (counts[num[px]]<=0 && num[px]>=1);
if (num[px]<1)
{
px--;
counts[num[px]]++;
sum=sum-num[px];
}
else if (sum+num[px]<=len && ps[len-sum-num[px]])
{
sum=sum+num[px];
counts[num[px]]--;
px++;
num[px]=num[px-1]+1;
}
}
}
return 0;
}
int Solve(int nx)
{
if (nx==0) return 1;
if (PreJudge(1)==0) return 0;
if (Grope(nx)) return 1;
return 0;
}
int main()
{
scanf("%d",&n);
while (n)
{
memset(a,0,sizeof(a));
memset(counts,0,sizeof(counts));
max_a=0;
sum_a=0;
for (i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if (a[i]>max_a) max_a=a[i];
sum_a=sum_a+a[i];
counts[a[i]]++;
}
for (len=max_a;len<=sum_a;len++)
{
if (sum_a%len==0)
{
if (PreJudge(sum_a/len))
{
/*printf("?%d\n",len);*/
if (Solve(sum_a/len))
{
printf("%d\n",len);
break;
}
}
}
}
scanf("%d",&n);
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator