| ||||||||||
| 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 | |||||||||
我自己测的时候挺快的啊,为什么一交就TLE.郁闷了两天..也使用了快速排序加速,还是不行#include<iostream>
//#include<fstream>
using namespace std;
int k;
int sum;
int stick[65];
int n;
bool flag=false;
bool flags[65];
//ofstream cout("c:\\2.txt",ios::out);
void judge(int subscript,int numbers,int grouptotal);
int compare(const void* a,const void* b)
{
return *(int*)b - *(int*)a ;
}
int main()
{
// ifstream cin("c:\\1.txt",ios::in);
cin>>n;
while(n)
{
int i;
sum=0;
for(i=0; i < 65; i++)stick[i]=0;
for(i=0; i < n; i++)
{
cin>>stick[i];
if(stick[i]>50)stick[i]=0;
if(stick[i]<=50)sum+=stick[i];
}
qsort(stick,n,sizeof(int),compare);
for(i=0; i < 65; i++)flags[i]=true;
k=1;
flag=false;
while(!flag)
{
if (sum%k==0)
if(sum/k<=n)
{
flag=true;
int j;
for(j=0; j<n; j++)
{
if(stick[j]>k)
{
flag=false;
break;
}
}
if(flag)
{
flag=false;
judge(0,sum/k,0);
}
if(flag)cout<<k<<endl;
}
k++;
}
cin>>n;
}
// cout.close;
return 0;
}
void judge(int subscript,int numbers,int grouptotal)
{
if (flag)return;
flags[subscript]=false;
int total;
total=grouptotal+stick[subscript];
// cout<<"组数为:"<<numbers<<" 当前和为:"<<total<<" 需求和为:"<<k<<endl;
if(total<k)
{
int i=subscript+1;
while(i<n)
{
if ((flags[i]!=false)&&(!flag))judge(i,numbers,total);
i++;
if(i>=n)
{
flags[subscript]=true;
return;
}
}
}
if(total==k)
{
if(numbers==1)
{
flag=true;
flags[subscript]=true;
return;
}
int i=0;
while(flags[i]==false)
{
i++;
if(i>=n)
{
// flags[subscript]=true;
return;
}
}
judge(i,numbers-1,0);
}
if(total>k)
{
flags[subscript]=true;
int i=subscript+1;
while(i<n)
{
if ((flags[i]!=false)&&(!flag))judge(i,numbers,total-stick[subscript]);
i++;
if(i>=n)
{
flags[subscript]=true;
return;
}
}
}
if(total<=k)flags[subscript]=true;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator