标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5024 | Accepted: 2108 |
Description
Input
Output
Sample Input
4 t -7 t 4 x 2 x 5
Sample Output
33 1 2
Source
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<algorithm> 6 using namespace std; 7 8 const int inf=1000000007; 9 const int maxn=20007; 10 11 bool op[maxn]; 12 int maxv[maxn],minv[maxn]; 13 int value[maxn]; 14 bool vis_max[maxn],vis_min[maxn]; 15 int n,ans; 16 int dfs_min(int i,int j); 17 int dfs_max(int i,int j); 18 19 int dfs_min(int i,int j) 20 { 21 int v=100*i+j; 22 if(vis_min[v]) return minv[v]; 23 vis_min[v]=1; 24 if(j-i<=1){ 25 if(j==i) return minv[v]=value[i-1]; 26 else if(!op[i]) return minv[v]=value[i]+value[i-1]; 27 else return minv[v]=value[i]*value[i-1]; 28 } 29 minv[v]=inf; 30 for(int k=i;k<j;k++) 31 { 32 int l=dfs_max(i,k); 33 int r=dfs_max(k+1,j); 34 int ll=dfs_min(i,k); 35 int rr=dfs_min(k+1,j); 36 if(!op[k]) minv[v]=min(minv[v],ll+rr); 37 else minv[v]=min(minv[v],min(l*r,min(ll*rr,min(l*rr,r*ll)))); 38 } 39 return minv[v]; 40 } 41 int dfs_max(int i,int j) 42 { 43 int v=100*i+j; 44 if(vis_max[v]) return maxv[v]; 45 vis_max[v]=1; 46 if(j-i<=1){ 47 if(j==i) return maxv[v]=value[i-1]; 48 else if(!op[i]) return maxv[v]=value[i]+value[i-1]; 49 else return maxv[v]=value[i]*value[i-1]; 50 } 51 maxv[v]=-inf; 52 for(int k=i;k<j;k++) 53 { 54 int l=dfs_max(i,k); 55 int r=dfs_max(k+1,j); 56 int ll=dfs_min(i,k); 57 int rr=dfs_min(k+1,j); 58 if(!op[k]) maxv[v]=max(maxv[v],l+r); 59 else maxv[v]=max(maxv[v],max(l*r,max(ll*rr,max(l*rr,r*ll)))); 60 } 61 return maxv[v]; 62 } 63 int main() 64 { 65 // freopen("in.txt","r",stdin); 66 char c; 67 while(~scanf("%d%*c",&n)){ 68 memset(op,0,sizeof(op)); 69 memset(value,0,sizeof(value)); 70 memset(maxv,0,sizeof(maxv)); 71 memset(minv,0,sizeof(minv)); 72 memset(vis_max,0,sizeof(vis_max)); 73 memset(vis_min,0,sizeof(vis_min)); 74 for(int i=0;i<n;i++) 75 { 76 scanf("%c %d%*c",&c,&value[i]); 77 value[i+n]=value[i]; 78 op[i]=op[i+n]=(c==‘x‘); 79 } 80 ans=-inf; 81 for(int i=0;i<n;i++) 82 { 83 ans=max(ans,dfs_max(i+1,i+n)); 84 } 85 printf("%d\n",ans); 86 bool first=1; 87 for(int i=0;i<n;i++) 88 { 89 if(dfs_max(i+1,i+n)==ans) 90 { 91 if(first){ 92 first=0; 93 printf("%d",i+1); 94 } 95 else printf(" %d",i+1); 96 } 97 } 98 printf("\n"); 99 } 100 return 0; 101 }
标签:
原文地址:http://www.cnblogs.com/codeyuan/p/4273625.html