【IOI 1994】 The Buses

【题目链接】

           http://poj.org/problem?id=1167

【算法】

            深度优先搜索 + 迭代加深

【代码】

           

#include <algorithm>  
#include <bitset>  
#include <cctype>  
#include <cerrno>  
#include <clocale>  
#include <cmath>  
#include <complex>  
#include <cstdio>  
#include <cstdlib>  
#include <cstring>  
#include <ctime>  
#include <deque>  
#include <exception>  
#include <fstream>  
#include <functional>  
#include <limits>  
#include <list>  
#include <map>  
#include <iomanip>  
#include <ios>  
#include <iosfwd>  
#include <iostream>  
#include <istream>  
#include <ostream>  
#include <queue>  
#include <set>  
#include <sstream>  
#include <stdexcept>  
#include <streambuf>  
#include <string>  
#include <utility>  
#include <vector>  
#include <cwchar>  
#include <cwctype>  
#include <stack>  
#include <limits.h> 
using namespace std;
#define MAXN 1010

struct info
{
        int opt,fir;
} b[MAXN];

int i,n,g,mx;
int a[MAXN],h[MAXN];

inline bool check()
{
        for (i = 1; i <= g; i++)
        {
                if (b[i].opt == 1)
                        return false;
        }
        return true;
}
inline bool IDDFS(int dep,int m)
{
        int i,d,tmp,e;
        if (dep > n)
        {
                if (check())
                        return true;
                else return false;
        } 
        if (h[a[dep]] <= 0) return IDDFS(dep+1,m);
        for (i = 1; i <= m; i++)
        {
                if (b[i].opt == 1)
                {
                        d = a[dep] - b[i].fir;
                        if (d <= b[i].fir) continue;
                        b[i].opt = 2;
                        tmp = a[dep];
                        while (tmp <= mx && h[tmp] > 0)
                        {
                                h[tmp]--;
                                tmp += d;        
                        }    
                        if (tmp > mx) 
                        {
                                if (IDDFS(dep+1,m)) 
                                        return true;
                        }
                        b[i].opt = 1;
                        e = tmp - d;
                        tmp = a[dep];
                        while (tmp <= e)
                        {
                                h[tmp]++;
                                tmp += d;
                        }
                }
        }
        if (m >= g) return false;
        if (2 * a[dep] >= 59) return false;
        m++;
        h[a[dep]]--;
        b[m].opt = 1;
        b[m].fir = a[dep];
        if (IDDFS(dep+1,m)) 
                return true;
        h[a[dep]]++;
        return false;
}

int main() 
{
        
        scanf("%d",&n);
        for (i = 1; i <= n; i++) 
        {
                scanf("%d",&a[i]);
                if (a[i] > mx)
                        mx = a[i];
                h[a[i]]++;
        }
        for (i = 1; i <= 17; i++)
        {
                g = i;
                if (IDDFS(1,0))
                        break;        
        }
        printf("%d\n",g);
        
        return 0;
    
}

 

转载于:https://www.cnblogs.com/evenbao/p/9279439.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值