結果

問題 No.1391 ±1 Abs Sum
ユーザー eSeFeSeF
提出日時 2020-11-08 13:14:04
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 3,207 bytes
コンパイル時間 2,502 ms
コンパイル使用メモリ 105,580 KB
実行使用メモリ 22,984 KB
最終ジャッジ日時 2023-09-29 21:10:45
合計ジャッジ時間 7,503 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
22,984 KB
testcase_01 AC 57 ms
22,960 KB
testcase_02 AC 55 ms
20,852 KB
testcase_03 AC 57 ms
20,900 KB
testcase_04 AC 56 ms
20,960 KB
testcase_05 AC 54 ms
18,824 KB
testcase_06 AC 56 ms
20,892 KB
testcase_07 AC 56 ms
18,984 KB
testcase_08 AC 72 ms
21,124 KB
testcase_09 AC 68 ms
19,048 KB
testcase_10 AC 61 ms
22,860 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using static System.Math;
namespace Solution
{
    class AC
    {
        const long INF = long.MaxValue / 2;
        static void Main(string[] args)
        {
            var (N, K) = Cin.Input<int, int>();

            var A = Cin.ReadSplitLong();
            long ans = INF;
            var lst = new List<long>();
            for (var i = 0; i < Min(N, 200); i++)
            {
                long s = 0;
                lst.Clear();
                for (var j = 0; j < N; j++) lst.Add(Abs(A[i] - A[j]));
                lst.Sort();
                for (var j = 0; j < N; j++) s += (j < K ? lst[j] : -lst[j]);
                ans = Min(ans, s);
            }
            for(var i = N - 1; i >= Max(0, N - 1 - 200); i--)
            {
                long s = 0;
                lst.Clear();
                for (var j = 0; j < N; j++) lst.Add(Abs(A[i] - A[j]));
                lst.Sort();
                for (var j = 0; j < N; j++) s += (j < K ? lst[j] : -lst[j]);
                ans = Min(ans, s);
            }
            for(var i = Max(0, N / 2 - 100); i < Min(N, N / 2 + 100); i++)
            {
                long s = 0;
                lst.Clear();
                for (var j = 0; j < N; j++) lst.Add(Abs(A[i] - A[j]));
                lst.Sort();
                for (var j = 0; j < N; j++) s += (j < K ? lst[j] : -lst[j]);
                ans = Min(ans, s);
            }
            Console.WriteLine(ans);            
        }
    }
}
static class Cin
{
    public static string Str() => Console.ReadLine();
    public static int Int() => int.Parse(Cin.Str());
    public static long Long() => long.Parse(Cin.Str());
    public static double Double() => double.Parse(Cin.Str());

    private static T Conv<T>(string value) => (T)Convert.ChangeType(value, typeof(T));
    public static (T, U) ReadTuple<T, U>(string[] input) => (Conv<T>(input[0]), Conv<U>(input[1]));
    public static (T, U, V) ReadTuple<T, U, V>(string[] input) => (Conv<T>(input[0]), Conv<U>(input[1]), Conv<V>(input[2]));
    public static (T, U, V, W) ReadTuple<T, U, V, W>(string[] input) => (Conv<T>(input[0]), Conv<U>(input[1]), Conv<V>(input[2]), Conv<W>(input[3]));
    public static (T, U, V, W, X) ReadTuple<T, U, V, W, X>(string[] input) => (Conv<T>(input[0]), Conv<U>(input[1]), Conv<V>(input[2]), Conv<W>(input[3]), Conv<X>(input[4]));

    public static (T, U) Input<T, U>() => ReadTuple<T, U>(ReadSplit());
    public static (T, U, V) Input<T, U, V>() => ReadTuple<T, U, V>(ReadSplit());
    public static (T, U, V, W) Input<T, U, V, W>() => ReadTuple<T, U, V, W>(ReadSplit());
    public static (T, U, V, W, X) Input<T, U, V, W, X>() => ReadTuple<T, U, V, W, X>(ReadSplit());

    public static string[] ReadSplit() => Console.ReadLine().Split();
    public static string[] ReadSplit(char separator) => Console.ReadLine().Split(separator);
    public static int[] ReadSplitInt() => Array.ConvertAll(ReadSplit(), int.Parse);
    public static long[] ReadSplitLong() => Array.ConvertAll(ReadSplit(), long.Parse);
    public static double[] ReadSplit_Double() => Array.ConvertAll(ReadSplit(), double.Parse);
}
0