AcWing 1235. 付账问题(贪婪)

Problem

这个题就是一群人吃饭,AA制某些人钱不够,如何付钱使得标准差最小?
贪心问题,不够的全拿,然后依次计算平均值。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;

class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter pw = new PrintWriter(System.out);
    static int N = 500010, n;
    static double sum, ave, cntave;
    static int a[] = new int[N];

    public static void main(String args[]) throws IOException {
        String s[] = br.readLine().split(" ");
        n = Integer.parseInt(s[0]);
        sum = Double.parseDouble(s[1]);
        s = br.readLine().split(" ");
        for (int i = 0; i < n; i++) a[i] = Integer.parseInt(s[i]);

        Arrays.sort(a, 0, n);

        ave = sum / n;
        double res = 0;
        for (int i = 0; i < n; i++) {
            double cnt = sum / (n - i);
            if (a[i] < cnt) cnt = a[i];
            res += (cnt - ave) * (cnt - ave);
            sum -= cnt;
        }
        pw.print(String.format("%.4f", Math.sqrt(res / n)));
        pw.flush();
        pw.close();
        br.close();
    }
}
发布了167 篇原创文章 · 获赞 3 · 访问量 3396

猜你喜欢

转载自blog.csdn.net/qq_43515011/article/details/104733186