您的位置:首页 > 其它

URAL 1152

2013-08-23 10:31 176 查看
题目大意:给出n个元素大小数组视为首尾相接,表示有n个可以对人造成a[i]点伤害的来源,一个人手中具有一种特殊的枪,每次只能打掉相邻的三个伤害来源,每打一次后,就会惊动其它的伤害来源,使其它每个伤害来源都会对此人造成对应的伤害。求此人最少会受到多大的伤害?

Time Limit:2000MS     Memory
Limit:65536KB     64bit IO Format:%I64d
& %I64u


数据规模:3<=n<=20,0<a[i]<=100。

理论基础:无。

题目分析:枚举每一次开枪所对应的中间位置,找出最小值。结果==>超时。

dfs?结果==>TLE。

dfs+剪枝+前缀和求和?结果==>AC。

代码如下:

#include<iostream>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<ctime>
#include<vector>
using namespace std;
typedef double db;
#define DBG 0
#define maa (1<<31)
#define mii ((1<<31)-1)
#define ast(b) if(DBG && !(b)) { printf("%d!!|\n", __LINE__); while(1) getchar(); } //调试
#define dout DBG && cout << __LINE__ << ">>| "
#define pr(x) #x"=" << (x) << " | "
#define mk(x) DBG && cout << __LINE__ << "**| "#x << endl
#define pra(arr, a, b) if(DBG) {\
dout<<#arr"[] |" <<endl; \
for(int i=a,i_b=b;i<=i_b;i++) cout<<"["<<i<<"]="<<arr[i]<<" |"<<((i-(a)+1)%8?" ":"\n"); \
if((b-a+1)%8) puts("");\
}
template<class T> inline bool updateMin(T& a, T b) { return a>b? a=b, true: false; }
template<class T> inline bool updateMax(T& a, T b) { return a<b? a=b, true: false; }
typedef long long LL;
typedef long unsigned int LU;
typedef long long unsigned int LLU;
#define N 20
int dp[N+1],n,Sum,a[N+1],use[N+1],ans=mii;
void dfs(int dep,int pdt)
{
if(pdt==0)
{
ans=min(ans,dp[dep-1]);
return;
}
if(dp[dep-1]>=ans)return;
for(int i=0;i<n;i++)
{
if(!use[i])
{
int t=pdt,flag2=0,flag3=0;
t-=(!use[i]*a[i]+(!use[(i-1)%n])*a[(i-1)%n]+(!use[(i+1)%n])*a[(i+1)%n]);
use[i]=true;
if(!use[(i-1)%n])flag2=use[(i-1)%n]=true;
if(!use[(i+1)%n])flag3=use[(i+1)%n]=true;
dp[dep]=dp[dep-1]+t;
dfs(dep+1,t);
use[i]=false;
if(flag2)use[(i-1)%n]=false;
if(flag3)use[(i+1)%n]=false;
}
}
}
int main()
{
while(~scanf("%d",&n))
{
memset(dp,0,sizeof dp);
dp[0]=0;
Sum=0;
for(int i=0;i<n;i++)
{
scanf("%d",a+i);
Sum+=a[i];
}
dfs(1,Sum);
printf("%d\n",ans);
}
return 0;
}

其中,特别要注意回溯时对状态的恢复。
by:Jsun_moon http://blog.csdn.net/jsun_moon







                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  URAL 1152 False Mirrors