Codeforces 442B Andrey and Problem(贪婪)

题目链接:Codeforces 442B Andrey and Problem

题目大意:Andrey有一个问题,想要朋友们为自己出一道题,如今他有n个朋友。每一个朋友想出题目的概率为pi,可是他能够同一时候向多个人寻求帮助。只是他仅仅能要一道题,也就是假设他向两个人寻求帮助,假设两个人都成功出题,也是不能够的。

解题思路:贪心,从概率最大的人開始考虑。假设询问他使得概率变大,则要询问。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const int N = 105;
const double eps = 1e-9;

int n, c, rec[N];
double s, p[N];

double solve (int x) {
    double ans = s * p[x];
    double tmp = s * (1-p[x]);

    for (int i = 0; i < c; i++)
        ans += tmp / (1-p[rec[i]]) * p[rec[i]];
    return ans;
}

int main () {
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
        scanf("%lf", &p[i]);

    sort (p, p + n);

    c = 0;
    double ans = p[n-1];
    s = 1 - p[n-1];
    rec[c++] = n-1;

    for (int i = n-2; i >= 0; i--) {
        double tmp = solve(i);

        if (tmp > ans) {
            ans = tmp;
            rec[c++] = i;
            s *= (1 - p[i]);
        }
    }

    printf("%.12lf\n", ans);
    return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。







本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4722936.html,如需转载请自行联系原作者


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值