【Codeforces 442B】Andrey and Problem
【链接】 我是链接,点我呀:)
【题意】
【题解】
主要思路是逆向思维,转换成一个一个地加上去 然后看看概率的改变值在何时为正数,显然只有为正数的时候才能加 然后概率大的和概率小的,哪一个加上去会比较优一点,也比较一下. 但是因为加上去之后S也会变化,所以只知道概率大的加上去比较优还不够. 可以再看看下面那个反证法. 它指出为何一定是选择末尾最大的若干个(也即优先选择p[i]最大的) (反证的思想就是,假设i【代码】
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());
}
}
}