HDU_6693 Valentine's Day [Probability]

I, entitled

  Valentine's Day

Second, analysis

  Assume that the probability $ s_0 $ representatives unhappy, $ s_1 $ represents the probability of once happy.

  Then take a random item, then its probability is happy $ p _i $, after the addition may be derived girlfriend into a happy and unhappy probability $$ s_0 = s_0 (1-p_i) $$ $$ s_1 = s_1 + (s_0 - s_1) p $$

  It can be derived by the formula, if $ s_0 \ le s_1 $, the probability of a happy there will be no growth.

  Therefore, the probability can be sorted in descending order, and then continue to join a judgment on it.

Three, AC codes

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 #define ll long long
 5 #define Min(a,b) ((a)>(b)?(b):(a))
 6 #define Max(a,b) ((a)>(b)?(a):(b))
 7 #define P pair<int, int>
 8 
 9 int main()
10 {
11     int T;
12     scanf("%d", &T);
13     while(T--) {
14         int n;
15         double p;
16         vector<double> vec;
17         scanf("%d", &n);
18         for(int i = 0; i < n; i++) {
19             scanf("%lf", &p);
20             vec.push_back(p);
21         }
22         sort(vec.begin(), vec.end(), greater<double>());
23         double ans1 = 1.0, ans2 = 0;
24         for(int i = 0; i < vec.size(); i++) {
25             if(vec[i] == 1.0) {
26                 ans2 = 1.0;
27                 break;
28             }
29             else {
30                 double res1 = ans1 * (1.0 - vec[i]);
31                 double res2 = ans2 + vec[i]*(ans1 - ans2);
32                 ans1 = res1, ans2 = res2;
33                 if(ans1 <= ans2)
34                     break;
35             }
36         }
37         printf("%.12lf\n", ans2);
38         
39     }
40     return 0;
41 }

 

Guess you like

Origin www.cnblogs.com/dybala21/p/11403111.html