Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 821 Accepted Submission(s): 231
ios
一道簡單的二維揹包問題,當時因爲有一段時間沒有碰這種題,在作這道題的時候也沒有向這個方向想....web
並只覺得很裝B的用着組合數字裏的,frrerrs圖像能夠AC。。。可是最後wa的一塌糊塗....ide
謝安貼一下錯誤的代碼吧....以此爲戒.優化
代碼:url
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<algorithm> 5 #include<vector> 6 #include<math.h> 7 #include<iostream> 8 #include<functional> 9 using namespace std; 10 int main() 11 { 12 int n,i; 13 double ans,tol; 14 //freopen("test.in","r",stdin); 15 //freopen("test.out","w",stdout); 16 while(scanf("%d",&n)!=EOF) 17 { 18 vector<int>aa(n+1); 19 for(i=0;i<n;i++) 20 cin>>aa[i]; 21 sort(aa.begin(),aa.end(),greater<int>()); 22 for(i=3;i<n;i++) 23 { 24 if(aa[0]>aa[1]) 25 { 26 if(aa[1]>aa[2]) 27 aa[2]+=aa[i]; 28 else 29 aa[1]+=aa[i]; 30 } 31 else 32 { 33 if(aa[0]>aa[2]) 34 aa[2]+=aa[i]; 35 else 36 aa[0]+=aa[i]; 37 } 38 } 39 tol=(aa[0]+aa[1]+aa[2])/2.0; 40 ans=(100.0*sqrt(tol*(tol-aa[0])*(tol-aa[1])*(tol-aa[2]))); //o£?×1?ê? 41 if(ans>1.0e-4) 42 { 43 printf("%d %d %d\n",aa[0],aa[1],aa[2]); 44 printf("%.lf\n",floor(ans)); 45 46 } 47 else 48 printf("-1\n"); 49 } 50 return 0; 51 }
固然上面的代碼,通常的狀況是能夠過的,可是有bug.....
好比下面的數據就過不了...spa
1 /* 2 10 3 1 3 6 9 0 8 5 7 4 2 4 7 6 5 4 3 2 1 0 5 1 0 6 2 1 0 7 3 2 1 0 8 4 3 2 1 0 9 5 4 3 2 1 0 10 6 5 4 3 2 1 0 11 7 6 5 4 3 2 1 0 12 8 7 6 5 4 3 2 1 0 13 9 8 7 6 5 4 3 2 1 0 14 10 9 8 7 6 5 4 3 2 1 0 15 */ 16 17 //answer 18 /* 19 9742 20 2121 21 -1 22 -1 23 -1 24 -1 25 447 26 1082 27 2121 28 3741 29 6235 30 9742 31 */
用二維0/1揹包來求解爲:
固然是通過優化後的...code
1 //二維0/1揹包 2 //擴大棧空間 3 #pragma comment(linker, "/STACK:102400000,102400000") 4 #include<stdio.h> 5 #include<math.h> 6 #include<string.h> 7 #include<stdlib.h> 8 int val[41],aa[41]; 9 bool dp[801][801]; 10 int main() 11 { 12 int n,i,j,k,ans,len,temp; 13 double cc; 14 freopen("test.in","r",stdin); 15 freopen("test.out","w",stdout); 16 while(scanf("%d",&n)!=EOF) 17 { 18 memset(dp,0,sizeof(dp)); 19 memset(aa,0,sizeof(aa)); 20 for(i=1;i<=n;i++) 21 { 22 scanf("%d",&val[i]); 23 aa[i]+=aa[i-1]+val[i]; 24 } 25 cc=aa[n]/2.0; 26 len=cc; 27 temp=0; 28 dp[0][0]=true; 29 for(i=1;i<=n;i++) 30 { 31 if(len<=aa[i]) 32 temp=len; 33 else temp=aa[i]; 34 for(j=temp;j>=0;j--) 35 { 36 for(k=temp;k>=0;k--) 37 { 38 if((j-val[i]>=0&&dp[j-val[i]][k])||(k-val[i]>=0&&dp[j][k-val[i]])) 39 dp[j][k]=true; 40 } 41 } 42 } 43 ans=0; 44 for(i=1;i<=len;i++) 45 { 46 for(j=1;j<=len;j++) 47 { 48 if(dp[i][j]) 49 { 50 if(ans<100*sqrt(cc*(cc-i)*(cc-j)*(i+j-cc))) 51 ans=100*sqrt(cc*(cc-i)*(cc-j)*(i+j-cc)); 52 } 53 } 54 } 55 if(ans==0) printf("-1\n"); 56 else printf("%d\n",ans); 57 } 58 return 0; 59 }