Ural 1152 False Mirrors

1152. False Mirrors

Time limit: 2.0 second
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 

参照了博客URAL 1152.False Mirrors(状态压缩dp+dfs) 

优化了一下,在dfs 的时候加了一个参数dam,用以保存伤害值,不用每次统计

#include 
   
   
    
    
#include 
    
    
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         #include 
        
          #include 
         
           #include 
          
            #include 
           
             #include 
            
              #include 
             
               #include 
              
                #include 
               
                 #include 
                
                  #include 
                 
                   #include 
                  
                    #include 
                   
                     #include 
                    
                      #define pi acos(-1.0) #define maxn (1048576 + 50) #define mod 1000000009 #define inf 0x7fffffff #define Lowbit(x) (x & (-x)) using namespace std; typedef long long int LLI; int a[maxn],n; int dp[maxn]; int dfs(int sum,int state) { if(dp[state] != inf) return dp[state]; for(int i = 1; i <= n; i ++) { if(state & (1 << (i - 1))) { int l,r; int temp = state - (1 << (i - 1)); int dam = sum - a[i]; l = (i - 2 + n) % n + 1; r = i % n + 1; if(temp & (1 << (l - 1))) { temp -= (1 << (l - 1)); dam -= a[l]; } if(temp & (1 << (r - 1))) { temp -= (1 << (r - 1)); dam -= a[r]; } dp[state] = min(dp[state],dfs(dam,temp) + dam); } } return dp[state]; } int main() { // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); int sum = 0; scanf("%d",&n); for(int i = 1; i <= n; i ++) { scanf("%d",&a[i]); sum += a[i]; } fill(dp,dp + maxn,inf); dp[0] = 0; printf("%d\n",dfs(sum,(1 << n) - 1)); return 0; } 
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
       
      
      
     
     
   
   

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值