标签:des style blog http color io os ar java
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 960 Accepted Submission(s): 344
一开始想法是对的,但是实现起来没弄好。
用long long存种类数竟然一直wa,改成概率就AC了,应该是精度的问题,毕竟2^40太大了。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<map> 6 #include<algorithm> 7 #include<vector> 8 #define M(a,b) memset(a,b,sizeof(a)) 9 #define eps 1e-6 10 using namespace std; 11 12 double p; 13 int n; 14 int num[45]; 15 double dp[45][40005]; 16 int cnt; 17 18 int main() 19 { 20 int t; 21 scanf("%d",&t); 22 while(t--) 23 { 24 scanf("%d%lf",&n,&p); 25 //long long a = pow(2,(long long)n); 26 for(int i = 0;i<n;i++) 27 { 28 scanf("%d",&num[i]); 29 } 30 M(dp,0); 31 dp[0][0] = 1; 32 for(int i = 0;i<n;i++) 33 { 34 for(int j = 0;j<1000*n;j++) 35 {dp[i+1][j] += dp[i][j]*0.5; 36 dp[i+1][j+num[i]]+= dp[i][j]*0.5;} 37 } 38 double res = 0; 39 int ans = 0; 40 for(int i = 1000*n;i>=0;i--) 41 { 42 res+=dp[n][i]; 43 if(res>1-p) {ans = i; break;} 44 } 45 printf("%d\n",ans); 46 } 47 return 0; 48 } 49 /* 50 1 51 3 0.1 52 1 2 3 53 10 54 40 0.5 55 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 56 10 57 10 0.7 58 1 2 3 2 3 2 1 1 3 4 59 13 60 61 */
把之前wa的代码也粘上吧:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<map> 6 #include<algorithm> 7 #include<vector> 8 #define M(a,b) memset(a,b,sizeof(a)) 9 #define eps 1e-6 10 using namespace std; 11 12 double p; 13 int n; 14 int num[45]; 15 int cnt; 16 double m[400005]; 17 double pls[400005]; 18 int which[400005]; 19 long long a; 20 21 int main() 22 { 23 int t; 24 scanf("%d",&t); 25 while(t--) 26 { 27 scanf("%d%lf",&n,&p); 28 a = pow(2,n); 29 for(int i = 0;i<n;i++) 30 { 31 scanf("%d",&num[i]); 32 } 33 M(m,0); 34 m[0] = 1.0/a; 35 for(int i = 0;i<n;i++) 36 { 37 int cnt = 0; 38 for(int j = 0;j<40000;j++) 39 { 40 if(m[j]>0) 41 { 42 pls[cnt] = m[j]; 43 which[cnt] = j+num[i]; 44 cnt++; 45 } 46 } 47 for(int i = 0;i<cnt;i++) 48 { 49 m[which[i]] += pls[i]; 50 } 51 } 52 double res = 0; 53 int ans = 0; 54 for(int i = 0;i<40000;i++) 55 { 56 res+=m[i]; 57 if(res>=p) {ans = i; break;} 58 } 59 //cout<<a<<endl; 60 //cout<<ct<<endl; 61 printf("%d\n",ans); 62 } 63 return 0; 64 } 65 /* 66 1 67 3 0.1 68 1 2 3 69 10 70 40 0.5 71 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 72 10 73 10 0.7 74 1 2 3 2 3 2 1 1 3 4 75 13 76 77 */
2013 Asia Regional Changchun C
标签:des style blog http color io os ar java
原文地址:http://www.cnblogs.com/haohaooo/p/4041684.html