首页 > 代码库 > 2013 Asia Regional Changchun C

2013 Asia Regional Changchun C

Little Tiger vs. Deep Monkey

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 960    Accepted Submission(s): 344


Problem Description
A crowd of little animals is visiting a mysterious laboratory – The Deep Lab of SYSU.

“Are you surprised by the STS (speech to speech) technology of Microsoft Research and the cat face recognition project of Google and academia? Are you curious about what technology is behind those fantastic demos?” asks the director of the Deep Lab. “Deep learning, deep learning!” Little Tiger raises his hand briskly. “Yes, clever boy, that’s deep learning (深度学习/深度神经网络)”, says the director. “However, they are only ‘a piece of cake’. I won’t tell you a top secret that our lab has invented a Deep Monkey (深猴) with more advanced technology. And that guy is as smart as human!”

“Nani ?!” Little Tiger doubts about that as he is the smartest kid in his kindergarten; even so, he is not as smart as human, “how can a monkey be smarter than me? I will challenge him.”

To verify their research achievement, the researchers of the Deep Lab are going to host an intelligence test for Little Tiger and Deep Monkey.

The test is composed of N binary choice questions. And different questions may have different scores according to their difficulties. One can get the corresponding score for a question if he chooses the correct answer; otherwise, he gets nothing. The overall score is counted as the sum of scores one gets from each question. The one with a larger overall score wins; tie happens when they get the same score.

Little Tiger assumes that Deep Monkey will choose the answer randomly as he doesn’t believe the monkey is smart. Now, Little Tiger is wondering “what score should I get at least so that I will not lose in the contest with probability of at least P? ”. As little tiger is a really smart guy, he can evaluate the answer quickly.

You, Deep Monkey, can you work it out? Show your power!?
 

 

Input
The first line of input contains a single integer T (1 ≤ T ≤ 10) indicating the number of test cases. Then T test cases follow.

Each test case is composed of two lines. The first line has two numbers N and P separated by a blank. N is an integer, satisfying 1 ≤ N ≤ 40. P is a floating number with at most 3 digits after the decimal point, and is in the range of [0, 1]. The second line has N numbers separated by blanks, which are the scores of each question. The score of each questions is an integer and in the range of [1, 1000]?
 

 

Output
For each test case, output only a single line with the answer.
 

 

Sample Input
13 0.51 2 3
 

 

Sample Output
3
 

 

Source
2013 Asia Regional Changchun

 

一开始想法是对的,但是实现起来没弄好。

用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-610 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 151 3 0.152 1 2 353 1054 40 0.555 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 156 1057 10 0.758 1 2 3 2 3 2 1 1 3 459 1360 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-610 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 167 3 0.168 1 2 369 1070 40 0.571 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 172 1073 10 0.774 1 2 3 2 3 2 1 1 3 475 1376 77 */

 

2013 Asia Regional Changchun C