Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

求助呀!!!!!!!!!!为什么TLE了.

Posted by zhangxizheng at 2011-09-05 21:23:08 on Problem 2362
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <stack>

using namespace std;
const int maxn=40;
int A[maxn];
bool visit[maxn];
int cmp(const int a,const int b){
    return a>b;
}
bool dfs(int N,int n,int nowlens,int len){
    if(n==0&&nowlens==len) return true;
    if(nowlens==len) nowlens=0;
    for(int i=0;i<N;++i){
        if(visit[i]==true) continue;
        if(nowlens+A[i]<=len){
            visit[i]=true;
            if(dfs(N,n-1,nowlens+A[i],len)==true)
                return true;
            visit[i]=false;
            if(nowlens==0||nowlens+A[i]==0)
                break;
        }
    }
    return false;
}
int main(){
//    freopen("in.txt","r",stdin);
    int n;
    scanf("%d",&n);
    stack<int> st;
    while(n--){
        memset(visit,false, sizeof(visit));
        memset(A,0,sizeof(A));
        int m,ans;
        int sum=0;
        scanf("%d",&m);
        for(int i=0;i<m;++i){
            scanf("%d",&A[i]);
            sum+=A[i];
        }
        sort(A,A+m,cmp);
        for(int len=A[0];;++len){
            if(sum%len==0){
                if(dfs(m,m,0,len)){
                    ans=sum/len;
                    break;
                }
            }
        }
        if(ans%4==0){
            printf("yes\n");
        }
        else{
            printf("no\n");
        }
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator