【HDU 6196】happy happy happy

传送门qwq

题目描述

Today, Bob plays with a child. There is a row of n numbers. One can takes a number from the left side or the right side in turns and gets the grade which equals to the number. Bob knows that the child always chooses the bigger number of the left side and right side. If the number from two sides is equal, child will always choose the left one.
The child takes first and the person who gets more grade wins. The child will be happy only when he wins the game. 
Bob wants to make the child happy, please help him calculate the minimal difference of their grades when he loses the game. 

题目翻译

解题思路

代码

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 const int INF=99999999;
 7 int a[100],pre[100],dp1[100][100],dp2[100][100],n,ans;
 8 int step=0;
 9 inline void ini(){
10     step=0;
11     memset(a,0,sizeof(a));
12     memset(pre,0,sizeof(pre));
13     for(register int i=0;i<=99;i++){
14         for(register int j=0;j<=99;j++){
15             dp1[i][j]=INF,dp2[i][j]=-INF;
16         }
17     }
18 }
19 inline int get_min(int l,int r){
20     if(dp1[l][r]!=INF){
21         return dp1[l][r];
22     }
23     if(l>r)return dp1[l][r]=0;
24     if(a[l]>=a[r]){
25         return dp1[l][r]=min(get_min(l+1,r-1)+a[r],get_min(l+2,r)+a[l+1]);
26     }
27     else return dp1[l][r]=min(get_min(l+1,r-1)+a[l],get_min(l,r-2)+a[r-1]);
28 }
29 inline int get_max(int l,int r){
30     if(dp2[l][r]!=-INF)return dp2[l][r];
31     if(l>r)return dp2[l][r]=0;
32     if(a[l]>=a[r]){
33         return dp2[l][r]=max(get_max(l+1,r-1)+a[r],get_max(l+2,r)+a[l+1]);
34     }
35     else return dp2[l][r]=max(get_max(l+1,r-1)+a[l],get_max(l,r-2)+a[r-1]);
36 }
37 inline void read(int &x){
38     x=0; register char ch=getchar();
39     while(ch<'0'||ch>'9')ch=getchar();
40     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
41 }
42 inline void dfs(int l,int r,int sub){
43     if(step>=80000000)return;
44     step++;
45     if(l>r){
46         ans=max(ans,sub);
47         return;
48     }
49     if(sub+dp1[l][r]-((pre[r]-pre[l-1])-dp1[l][r])>=0)return;
50     if(sub+dp2[l][r]-((pre[r]-pre[l-1])-dp2[l][r])<=ans){
51         ans=max(ans,sub+dp2[l][r]-((pre[r]-pre[l-1])-dp2[l][r]));
52         return;
53     }
54     if(sub+dp2[l][r]-((pre[r]-pre[l-1])-dp2[l][r])<=ans)return;
55 
56     if(a[l]>=a[r]){
57         dfs(l+1,r-1,sub+a[r]-a[l]);
58         dfs(l+2,r,sub+a[l+1]-a[l]);
59     }
60     else {
61         dfs(l+1,r-1,sub+a[l]-a[r]);
62         dfs(l,r-2,sub+a[r-1]-a[r]);
63     }
64 }
65 int main(){
66     while(~scanf("%d",&n)){
67         ini();
68         for(register int i=1;i<=n;i++){
69             read(a[i]);
70             pre[i]=pre[i-1]+a[i];
71         }
72         get_min(1,n),get_max(1,n);
73         ans=-INF;
74         dfs(1,n,0);
75         if(ans<0&&ans>=-9000000){
76             printf("%d\n",-ans);
77         }
78         else printf("The child will be unhappy...\n");
79     }
80 }

猜你喜欢

转载自www.cnblogs.com/Fang-Hao/p/9166127.html