【题目链接】
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 ",g); return 0; }