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,直接dfs反而32msAC??求大牛指教。

Posted by Moon_1st at 2011-02-05 11:57:47 on Problem 1167
直接dfs版:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;

const int N = 60;
const int M = 1000;

struct node
{
    int start, interval, times;
}path[M];

int n, ans, tot, ct[N];

int cmp(const void *a, const void *b)
{
    node *aa, *bb;
    aa = (node *)a;
    bb = (node *)b;
    return bb->times - aa->times;
}

bool check(int s, int t)
{
    int i;
    for(i = s; i < N; i += t)
        if(!ct[i])
            return false;
    return true;
}

void dfs(int index, int sum)
{
    int i, j, tmp, k;
    if(n == 0)
    {
        if(sum < ans)  ans = sum;
        return;
    }
    for(i = index; i<tot && path[i].times>n; i++);
    for(k = i; k < tot; k++)
    {
        if(sum+n/path[k].times >= ans)  return;
        if(check(path[k].start, path[k].interval))
        {
            tmp = path[k].interval;
            for(j = path[k].start; j < N; j += tmp)
            {
                ct[j]--;
                n--;
            }
            dfs(k, sum+1);
            for(j = path[k].start; j < N; j += tmp)
            {
                ct[j]++;
                n++;
            }
        }
    }
}

int main()
{
    int i, x, j;
    scanf("%d", &n);
    memset(ct, 0, sizeof(ct));
    for(i = 0; i < n; i++)
    {
        scanf("%d", &x);
        ct[x]++;
    }
    tot = 0;
    for(i = 0; i <= 29; i++)
    {
        if(!ct[i])  continue;
        for(j = i+1; j <= 59-i; j++)
            if(check(i, j))
            {
                path[tot].start = i;
                path[tot].interval = j;
                path[tot].times = 1 + (59-i)/j;
                tot++;
            }
    }
    qsort(path, tot, sizeof(path[0]), cmp);
    ans = 17;
    dfs(0, 0);
    printf("%d\n", ans);
    return 0;
}
迭代加深版(做了一点小修改):
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;

const int N = 60;
const int M = 1000;

struct node
{
    int start, interval, times;
}path[M];

int n, ans, tot, dd, ct[N];

int cmp(const void *a, const void *b)
{
    node *aa, *bb;
    aa = (node *)a;
    bb = (node *)b;
    return bb->times - aa->times;
}

bool check(int s, int t)
{
    int i;
    for(i = s; i < N; i += t)
        if(!ct[i])
            return false;
    return true;
}

void dfs(int index, int sum)
{
    int i, j, tmp, k;
    if(n == 0)
    {
        ans = sum;
        return;
    }
    if(sum == dd)  return;
    for(i = index; i<tot && path[i].times>n; i++);
    for(k = i; k < tot; k++)
    {
        if(sum+n/path[k].times >= ans)  return;
        if(check(path[k].start, path[k].interval))
        {
            tmp = path[k].interval;
            for(j = path[k].start; j < N; j += tmp)
            {
                ct[j]--;
                n--;
            }
            dfs(k, sum+1);
            if(ans == dd)  return;
            for(j = path[k].start; j < N; j += tmp)
            {
                ct[j]++;
                n++;
            }
        }
    }
}

int main()
{
    int i, x, j;
    scanf("%d", &n);
    memset(ct, 0, sizeof(ct));
    for(i = 0; i < n; i++)
    {
        scanf("%d", &x);
        ct[x]++;
    }
    tot = 0;
    for(i = 0; i <= 29; i++)
    {
        if(!ct[i])  continue;
        for(j = i+1; j <= 59-i; j++)
            if(check(i, j))
            {
                path[tot].start = i;
                path[tot].interval = j;
                path[tot].times = 1 + (59-i)/j;
                tot++;
            }
    }
    qsort(path, tot, sizeof(path[0]), cmp);
    ans = 17;
    dd = 0;
    while(dd != ans)
    {
        dd++;
        dfs(0, 0);
    }
    printf("%d\n", ans);
    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