标签:
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1203
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22401 Accepted Submission(s): 8959
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int N = 10005; 6 int a[N]; 7 double b[N]; 8 double dp[N]; 9 double max(double a,double b) 10 { 11 if(a<b) return b; 12 else return a; 13 } 14 int main() 15 { 16 int n,m; 17 while(~scanf("%d%d",&n,&m)) 18 { 19 if(n==0&&m==0) return 0; 20 memset(dp,0,sizeof(dp)); 21 for(int i = 1; i <= m; i++) 22 { 23 scanf("%d%lf",&a[i],&b[i]); 24 } 25 for(int i = 1; i <= m; i++) 26 { 27 for(int j = n; j >= a[i]; j--) 28 { 29 dp[j] = max(dp[j],1-(1-dp[j-a[i]])*(1-b[i])); 30 } 31 } 32 double ans = dp[n]*100; 33 printf("%.1lf%%\n",ans); 34 } 35 return 0; 36 }
第二种方法:
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int N = 10005; 6 int a[N]; 7 double b[N]; 8 double dp[N]; 9 double min(double a, double b) 10 { 11 if(a<b) return a; 12 else return b; 13 } 14 int main() 15 { 16 int n, m; 17 while(~scanf("%d%d",&n,&m)) 18 { 19 if(n==0&&m==0) return 0; 20 for(int i = 0; i < N; i++) 21 dp[i] = 1; 22 for(int i = 0; i < m; i++) 23 scanf("%d%lf",&a[i],&b[i]); 24 for(int i = 0; i < m; i++) 25 { 26 for(int j = n; j >= a[i]; j--) 27 { 28 dp[j] = min(dp[j],dp[j-a[i]]*(1-b[i])); 29 } 30 } 31 double ans = (1-dp[n])*100; 32 printf("%.1lf%%\n",ans); 33 } 34 return 0; 35 }
标签:
原文地址:http://www.cnblogs.com/shanyr/p/5263230.html