結果

問題 No.1251 絶対に間違ってはいけない最小化問題
ユーザー shojin_proshojin_pro
提出日時 2020-10-09 23:31:58
言語 Java21
(openjdk 21)
結果
AC  
実行時間 864 ms / 2,000 ms
コード長 4,068 bytes
コンパイル時間 4,287 ms
コンパイル使用メモリ 86,788 KB
実行使用メモリ 75,704 KB
最終ジャッジ日時 2023-09-27 19:54:18
合計ジャッジ時間 32,544 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
51,028 KB
testcase_01 AC 66 ms
50,944 KB
testcase_02 AC 70 ms
51,272 KB
testcase_03 AC 65 ms
50,836 KB
testcase_04 AC 66 ms
51,712 KB
testcase_05 AC 69 ms
52,748 KB
testcase_06 AC 78 ms
52,928 KB
testcase_07 AC 95 ms
52,392 KB
testcase_08 AC 84 ms
52,576 KB
testcase_09 AC 94 ms
52,500 KB
testcase_10 AC 82 ms
52,664 KB
testcase_11 AC 92 ms
52,624 KB
testcase_12 AC 93 ms
52,824 KB
testcase_13 AC 82 ms
52,596 KB
testcase_14 AC 67 ms
51,256 KB
testcase_15 AC 72 ms
51,000 KB
testcase_16 AC 86 ms
53,048 KB
testcase_17 AC 73 ms
51,972 KB
testcase_18 AC 864 ms
73,932 KB
testcase_19 AC 728 ms
75,704 KB
testcase_20 AC 421 ms
64,984 KB
testcase_21 AC 210 ms
57,792 KB
testcase_22 AC 484 ms
66,496 KB
testcase_23 AC 189 ms
57,064 KB
testcase_24 AC 554 ms
70,812 KB
testcase_25 AC 456 ms
64,132 KB
testcase_26 AC 293 ms
59,028 KB
testcase_27 AC 182 ms
57,716 KB
testcase_28 AC 715 ms
71,920 KB
testcase_29 AC 692 ms
71,876 KB
testcase_30 AC 700 ms
70,952 KB
testcase_31 AC 786 ms
70,468 KB
testcase_32 AC 766 ms
71,900 KB
testcase_33 AC 721 ms
72,256 KB
testcase_34 AC 691 ms
72,148 KB
testcase_35 AC 696 ms
70,912 KB
testcase_36 AC 751 ms
72,124 KB
testcase_37 AC 717 ms
71,828 KB
testcase_38 AC 719 ms
72,012 KB
testcase_39 AC 711 ms
72,456 KB
testcase_40 AC 735 ms
72,060 KB
testcase_41 AC 708 ms
73,792 KB
testcase_42 AC 716 ms
71,844 KB
testcase_43 AC 66 ms
51,048 KB
testcase_44 AC 65 ms
51,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;
 
public class Main {
    public static void main(String[] args) throws Exception {
        FastScanner sc = new FastScanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
        StringBuilder sb = new StringBuilder();
        int n = sc.nextInt();
        Point[] arr = new Point[n];
        for(int i = 0; i < n; i++){
            arr[i] = new Point(sc.nextLong());
        }
        long total = 0;
        for(int i = 0; i < n; i++){
            arr[i].b = sc.nextLong();
            total += arr[i].b;
        }
        Arrays.sort(arr);
        long[] a = new long[n];
        long[] b = new long[n];
        long now = 0;
        int left = -1;
        for(int i = 0; i < n; i++){
            a[i] = arr[i].a;
            b[i] = arr[i].b;
            now += b[i];
            if(left == -1 && total <= now*2){
                left = i;
            }
        }
        long nowA = a[0];
        long nowB = b[0];
        if(n == 1){
            pw.println(nowA + " " + 0);
            pw.flush();
            return;
        }
        long a1 = a[left];
        long ans = 0;
        for(int i = 0; i < n; i++){
            ans += Math.abs(a1-a[i])*b[i];
        }
        if(left < n-1){
            long a2 = a[left+1];
            long ans2 = 0;
            for(int i = 0; i < n; i++){
                ans2 += Math.abs(a2-a[i])*b[i];
            }
            if(ans > ans2){
                ans = ans2;
                a1 = a2;
            }
        }
        pw.println(a1 + " " + ans);
        pw.flush();
    }
    
    static class GeekInteger {
        public static void save_sort(int[] array) {
            shuffle(array);
            Arrays.sort(array);
        }
 
        public static int[] shuffle(int[] array) {
            int n = array.length;
            Random random = new Random();
            for (int i = 0, j; i < n; i++) {
                j = i + random.nextInt(n - i);
                int randomElement = array[j];
                array[j] = array[i];
                array[i] = randomElement;
            }
            return array;
        }
    }
}

class Point implements Comparable<Point>{
    long a,b;
    public Point(long a){
        this.a = a;
    }
    
    public int compareTo(Point p){
        if(this.a < p.a){
            return -1;
        }else if(this.a > p.a){
            return 1;
        }else{
            return 0;
        }
    }
}

class FastScanner {
    private BufferedReader reader = null;
    private StringTokenizer tokenizer = null;
    public FastScanner(InputStream in) {
        reader = new BufferedReader(new InputStreamReader(in));
        tokenizer = null;
    }

    public String next() {
        if (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public String nextLine() {
        if (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                return reader.readLine();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken("\n");
    }

    public long nextLong() {
        return Long.parseLong(next());
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    public double nextDouble() {
         return Double.parseDouble(next());
    }
    
    public String[] nextArray(int n) {
        String[] a = new String[n];
        for (int i = 0; i < n; i++)
            a[i] = next();
        return a;
    }

    public int[] nextIntArray(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = nextInt();
        return a;
    }

    public long[] nextLongArray(int n) {
        long[] a = new long[n];
        for (int i = 0; i < n; i++)
            a[i] = nextLong();
        return a;
    } 
}
0