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 to death.... 已经+了lrj黑书上的所有剪枝却依然超时。。。大牛请教 附带程序

Posted by SherlockBourne at 2007-03-30 13:08:42 on Problem 1011
#include<iostream>

using namespace std;

int n;
int que[70];
bool done[70];
bool flag;
int total, first;

bool search(int k, int last, int start)
{
    if ((total == n) && (k == first))
        return true;
    else
        if (total == n)
            return false;
    for (int i = start; i != -1; i --)
        if ((! done[i]) && (que[i] <= k) && ((k != first) || (que[i] >= last)))
        {
            total ++;
            done[i] = true;
            if (k - que[i] > 0)
                if (k == first)
                    if (search(k - que[i], que[i], i - 1))
                        return true;
                    else
                        ;
                    else
                        if (search(k - que[i], last, i - 1))
                            return true;
            if (k - que[i] == 0)
                if (search(first, last, n - 1))
                    return true;
            done[i] = false;
            total --;
            if (que[i] == k)
                return false;
        }
}

int main()
{
    while (scanf("%d", &n) != -1)
    {
        if (n == 0)
            break;
        int sum = 0;
        for (int i = 0; i != n; i ++)
        {
            scanf("%d", &que[i]);
            sum += que[i];
        }
        sort(que, que + n);
        for (int i = sum / que[n - 1]; i > 0; i --)
            if (sum % i == 0)
            {
                memset(done, false, sizeof(done));
                total = 0;
                for (int j = 0; j != n; j ++)
                    if (que[j] == sum / i)
                    {
                        total ++;
                        done[j] = true;
                    }
                first = sum / i;
                if (search(sum / i, que[n - i], n - i))
                {
                    printf("%d\n", sum / i);
                    break;
                }
            }
    }
    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