ural 1431. Diplomas
1431. Diplomas
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
It might be interesting for you to learn that there are students who take part in various contests instead of studying. Sometimes such students are even awarded diplomas for winning these contests. Another amusing fact is that some deans collect and hang on the walls color copies of student diplomas. And when there are too many diplomas, extra walls are put up in a dean's office. But before a new wall is put up, its size should be determined, and for that a scheme of arranging diplomas on the wall is needed. That is why a designer is usually hired, to make everything beautiful.
A hired designer reckons that all diplomas of the same kind (for example, for winning semifinals) must be in the same row, and each row may contain diplomas of at most two different kinds. Moreover, if a row contains diplomas of two kinds, then they must alternate, and the last diploma in the row must be of the same kind as the first one for the sake of symmetry.
In order to determine how many walls should be put up, the dean has to know the minimal number of rows needed to arrange all the diplomas (the rows can be unboundedly long). Of course, having such clever students, it's not a big problem. That is to say, it is a problem, but a problem for the students.
Input
The first line of the input contains the number of different kinds of diplomas N (1 ≤ N ≤ 18). The second line contains the numbers of diplomas of each kind separated with a space: N integers in the range from 1 to 30.
Output
You should output the minimal number of rows needed to arrange the diplomas in accordance with the designer's requirements.
Sample
input | output |
---|---|
6 8 15 13 8 14 8 |
5 |
Problem Author: Stanislav Vasilyev
Problem Source: The 7th USU Open Personal Contest - February 25, 2006
Problem Source: The 7th USU Open Personal Contest - February 25, 2006
Tags: none
Difficulty: 367
题意:问n样东西,每个东西都有一个值
1、每行最多放两样东西,可以放一样
2、如果两样东西放一行,则这两样东西的值必须差1
分析:n只有18,那岂不是怎么搞都行?
貌似可以贪心。。。
但我用了状压dp,显然的,每个位代表选了没有
1 /** 2 Create By yzx - stupidboy 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 #include <cmath> 8 #include <deque> 9 #include <vector> 10 #include <queue> 11 #include <iostream> 12 #include <algorithm> 13 #include <map> 14 #include <set> 15 #include <ctime> 16 #include <iomanip> 17 using namespace std; 18 typedef long long LL; 19 typedef double DB; 20 #define For(i, s, t) for(int i = (s); i <= (t); i++) 21 #define Ford(i, s, t) for(int i = (s); i >= (t); i--) 22 #define Rep(i, t) for(int i = (0); i < (t); i++) 23 #define Repn(i, t) for(int i = ((t)-1); i >= (0); i--) 24 #define rep(i, x, t) for(int i = (x); i < (t); i++) 25 #define MIT (2147483647) 26 #define INF (1000000001) 27 #define MLL (1000000000000000001LL) 28 #define sz(x) ((int) (x).size()) 29 #define clr(x, y) memset(x, y, sizeof(x)) 30 #define puf push_front 31 #define pub push_back 32 #define pof pop_front 33 #define pob pop_back 34 #define ft first 35 #define sd second 36 #define mk make_pair 37 inline void SetIO(string Name) 38 { 39 string Input = Name+".in", 40 Output = Name+".out"; 41 freopen(Input.c_str(), "r", stdin), 42 freopen(Output.c_str(), "w", stdout); 43 } 44 45 46 inline int Getint() 47 { 48 int Ret = 0; 49 char Ch = ' '; 50 bool Flag = 0; 51 while(!(Ch >= '0' && Ch <= '9')) 52 { 53 if(Ch == '-') Flag ^= 1; 54 Ch = getchar(); 55 } 56 while(Ch >= '0' && Ch <= '9') 57 { 58 Ret = Ret * 10 + Ch - '0'; 59 Ch = getchar(); 60 } 61 return Flag ? -Ret : Ret; 62 } 63 64 const int N = 19; 65 int n, Arr[N]; 66 int Dp[N][1 << N]; 67 68 inline void Input() 69 { 70 scanf("%d", &n); 71 For(i, 1, n) scanf("%d", &Arr[i]); 72 } 73 74 inline int Val(int x) 75 { 76 return 1 << (x - 1); 77 } 78 79 inline void Search(int x, int State, int Cnt) 80 { 81 if(Dp[x - 1][State] <= Cnt) return; 82 Dp[x - 1][State] = Cnt; 83 if(x > n) return ; 84 if(State & Val(x)) 85 { 86 Search(x + 1, State, Cnt); 87 return; 88 } 89 Search(x + 1, State | Val(x), Cnt + 1); 90 For(i, x + 1, n) 91 if(!(State & Val(i)) && abs(Arr[i] - Arr[x]) == 1) 92 Search(x + 1, State | Val(x) | Val(i), Cnt + 1); 93 } 94 95 inline void Solve() 96 { 97 clr(Dp, 63); 98 Search(1, 0, 0); 99 cout << Dp[n][(1 << n) - 1] << endl; 100 } 101 102 int main() 103 { 104 #ifndef ONLINE_JUDGE 105 SetIO("A"); 106 #endif 107 Input(); 108 Solve(); 109 return 0; 110 }