【Codeforces 442B】Andrey and Problem

【链接】 我是链接,点我呀:)
【题意】


n个朋友
第i个朋友帮你的概率是pi
现在问你恰好有一个朋友帮你的概率最大是多少
前提是你可以选择只问其中的某些朋友不用全问.

【题解】


主要思路是逆向思维,转换成一个一个地加上去
然后看看概率的改变值在何时为正数,显然只有为正数的时候才能加
然后概率大的和概率小的,哪一个加上去会比较优一点,也比较一下.
但是因为加上去之后S也会变化,所以只知道概率大的加上去比较优还不够.
可以再看看下面那个反证法.
它指出为何一定是选择末尾最大的若干个(也即优先选择p[i]最大的)
(反证的思想就是,假设i<j,但是p[i]在答案中,p[j]不在(p[i]< p[j]),那么这个时候,如果我们把p[i]删掉,再加上p[j]的话,显然比原来相同数量的答案来的好)
(所以对于每一个不是选择最大的若干个概率的方案,我们总能让这些方案更优一点

【代码】

import java.io.*;
import java.util.*;

public class Main {
    
    
    static InputReader in;
    static PrintWriter out;
        
    public static void main(String[] args) throws IOException{
        //InputStream ins = new FileInputStream("E:\\rush.txt");
        InputStream ins = System.in;
        in = new InputReader(ins);
        out = new PrintWriter(System.out);
        //code start from here
        new Task().solve(in, out);
        out.close();
    }
    
    static int N = (int)100;
    static class Task{
        int n;
        double p[];
        double P=1,S,ans;
        
        public void solve(InputReader in,PrintWriter out) {
            p = new double[N+10];
            n = in.nextInt();
            for (int i = 1;i <= n;i++) p[i] = in.nextDouble();
            Arrays.sort(p, 1,1+n);
            if (Math.abs(p[n]-1)<(1e-9)) {
                out.println(1);
                return;
            }
            for (int i = n;i >= 1;i--) {
                if (S<1) {
                    P = P*(1-p[i]);
                    S = S + p[i]/(1-p[i]);
                    ans = P*S;
                }
            }
            out.println(ans);
        }
    }

    

    static class InputReader{
        public BufferedReader br;
        public StringTokenizer tokenizer;
        
        public InputReader(InputStream ins) {
            br = new BufferedReader(new InputStreamReader(ins));
            tokenizer = null;
        }
        
        public String next(){
            while (tokenizer==null || !tokenizer.hasMoreTokens()) {
                try {
                tokenizer = new StringTokenizer(br.readLine());
                }catch(IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        
        public int nextInt() {
            return Integer.parseInt(next());
        }
        
        public double nextDouble() {
            return Double.parseDouble(next());
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/AWCXV/p/10447741.html
今日推荐