2019牛客暑期多校训练营(第一场) C. Euclidean Distance

终于AC了。。。先睡觉。。。。。。

/*
 * Copyright (c) 2019 Ng Kimbing, HNU, All rights reserved. May not be used, modified, or copied without permission.
 * @Author: Ng Kimbing, HNU.
 * @LastModified:2019-07-19 T 22:54:43.695 +08:00
 */

package NowCoder;

import java.util.Arrays;

import static ACMProblems.ACMIO.*;

public class ProblemC {
    static class Fraction {
        long molecule;
        long denominator;

        Fraction(long molecule, long denominator) {
            this.molecule = molecule;
            this.denominator = denominator;
        }

        void add(int num) {
            this.molecule += num * this.denominator;
        }

        @Override
        public String toString() {
            if (this.denominator == 1)
                return String.valueOf(this.molecule);
            else return molecule + "/" + denominator;
        }

        void approximate() {
            long gcd = gcd(molecule, denominator);
            this.molecule /= gcd;
            this.denominator /= gcd;
        }

        static long gcd(long a, long b) {
            return b > 0 ? gcd(b, a % b) : a;
        }

        void divide(int m) {
            this.denominator *= m;
        }
    }

    private static int[] a = new int[10005];
    private static long[] sum = new long[10005];
    private static int n, m;

    private static int findPos() {
        for (int i = 0; i < n - 1; ++i)
            if (a[i + 1] * (i + 1) < sum[i])
                return i;
        return n - 1;
    }

    public static void main(String[] args) {
        while (true) {
            try {
                n = nextInt();
                m = nextInt();
                for (int i = 0; i < n; ++i)
                    a[i] = nextInt();
                Arrays.sort(a, 0, n);
                for (int i = 0, j = n - 1; i < j; ++i, --j) {
                    int temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
                sum[0] = a[0] - m;
                for (int i = 1; i < n; ++i)
                    sum[i] = sum[i - 1] + a[i];
                int pos = findPos();
//                System.out.println("pos = " + pos);
                Fraction ans = new Fraction(sum[pos] * sum[pos], pos + 1);
//                System.out.println("org: " + ans);
                for (int i = pos + 1; i < n; ++i)
                    ans.add(a[i] * a[i]);
//                System.out.println("before divided " + ans);
                ans.divide(m * m);
                ans.approximate();
                out.println(ans);
//                out.flush();

                Arrays.fill(sum, 0, n, 0);
            } catch (Exception e) {
                out.flush();
                return;
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值