1152. False Mirrors
Time limit: 2.0 second
Memory limit: 64 MB
Memory limit: 64 MB
Background
We wandered in the labyrinth for twenty minutes before finally entering the large hall. The walls were covered by mirrors here as well. Under the ceiling hung small balconies where monsters stood. I had never seen this kind before. They had big bulging eyes, long hands firmly holding riffles and scaly, human-like bodies. The guards fired at me from the balconies, I shot back using my BFG-9000. The shot shattered three mirrors filling the room with silvery smoke. Bullets drummed against my body-armor knocking me down to the floor. Falling down I let go a shot, and got up as fast as I fell down by rotating on my back, like I did in my youth while break dancing, all this while shooting three more times. Three mirrors, three mirrors, three mirrors…
Sergey Lukjanenko, “The Labyrinth of Reflections”
Problem
BFG-9000 destroys three adjacent balconies per one shoot. (
N-th balcony is adjacent to the first one). After the shoot the survival monsters inflict damage to Leonid (main hero of the novel) — one unit per monster. Further follows new shoot and so on until all monsters will perish. It is required to define the minimum amount of damage, which can take Leonid.
Input
The first line contains integer
N, аmount of balconies, on which monsters have taken a circular defense. 3 ≤
N ≤ 20. The second line contains
N integers, amount of monsters on each balcony (not less than 1 and no more than 100 on each).
Output
Output minimum amount of damage.
Sample
input | output |
---|---|
7 3 4 2 2 1 4 1 | 9 |
Problem Author: Eugene Bryzgalov
Problem Source: Ural Collegiate Programming Contest, April 2001, Perm, English Round
Problem Source: Ural Collegiate Programming Contest, April 2001, Perm, English Round
Difficulty: 209
题意:一个环n个点,每个点一个权值,每回合可以破坏连续三个点,然后受到剩下点的伤害,问破坏所有点,最少要受到多少伤害
分析:
1、搜索,显然先搜较大的组合,并且只打中间没有被破坏的地方
2、状态压缩dp,才20个点,表示那些点被破坏了的最少伤害,我直接用了方法二
1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cmath> 5 #include <deque> 6 #include <vector> 7 #include <queue> 8 #include <iostream> 9 #include <algorithm> 10 #include <map> 11 #include <set> 12 #include <ctime> 13 using namespace std; 14 typedef long long LL; 15 typedef double DB; 16 #define For(i, s, t) for(int i = (s); i <= (t); i++) 17 #define Ford(i, s, t) for(int i = (s); i >= (t); i--) 18 #define Rep(i, t) for(int i = (0); i < (t); i++) 19 #define Repn(i, t) for(int i = ((t)-1); i >= (0); i--) 20 #define rep(i, x, t) for(int i = (x); i < (t); i++) 21 #define MIT (2147483647) 22 #define INF (1000000001) 23 #define MLL (1000000000000000001LL) 24 #define sz(x) ((int) (x).size()) 25 #define clr(x, y) memset(x, y, sizeof(x)) 26 #define puf push_front 27 #define pub push_back 28 #define pof pop_front 29 #define pob pop_back 30 #define ft first 31 #define sd second 32 #define mk make_pair 33 inline void SetIO(string Name) { 34 string Input = Name+".in", 35 Output = Name+".out"; 36 freopen(Input.c_str(), "r", stdin), 37 freopen(Output.c_str(), "w", stdout); 38 } 39 40 inline int Getint() { 41 int Ret = 0; 42 char Ch = ' '; 43 while(!(Ch >= '0' && Ch <= '9')) Ch = getchar(); 44 while(Ch >= '0' && Ch <= '9') { 45 Ret = Ret*10+Ch-'0'; 46 Ch = getchar(); 47 } 48 return Ret; 49 } 50 51 const int N = 21; 52 int n, Arr[N]; 53 int Dp[1<<N]; 54 bool Visit[1<<N]; 55 56 inline void Input() { 57 scanf("%d", &n); 58 Rep(i, n) scanf("%d", &Arr[i]); 59 } 60 61 inline int Calc(int State) { 62 static int Cal[1<<N]; 63 if(Cal[State]) return Cal[State]; 64 int Ret = 0; 65 Rep(i, n) 66 if((1<<i)&State) Ret += Arr[i]; 67 return Cal[State] = Ret; 68 } 69 70 inline int Search(int State) { 71 if(Visit[State]) return Dp[State]; 72 Visit[State] = 1; 73 if(!State) return Dp[State] = 0; 74 int Ret = INF; 75 Rep(i, n) 76 if((1<<i)&State) { 77 int _State = State, _j; 78 For(j, i-1, i+1) { 79 _j = (j+n)%n; 80 if(_State&(1<<_j)) _State ^= (1<<_j); 81 } 82 Ret = min(Ret, Search(_State)+Calc(_State)); 83 } 84 return Dp[State] = Ret; 85 } 86 87 inline void Solve() { 88 int Ans = Search((1<<n)-1); 89 printf("%d\n", Ans); 90 } 91 92 int main() { 93 #ifndef ONLINE_JUDGE 94 SetIO("C"); 95 #endif 96 Input(); 97 Solve(); 98 return 0; 99 }