結果

問題 No.1391 ±1 Abs Sum
ユーザー eSeFeSeF
提出日時 2020-11-08 13:04:15
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 168 ms / 2,000 ms
コード長 3,471 bytes
コンパイル時間 3,605 ms
コンパイル使用メモリ 105,272 KB
実行使用メモリ 48,932 KB
最終ジャッジ日時 2023-09-29 21:10:15
合計ジャッジ時間 10,821 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
20,876 KB
testcase_01 AC 59 ms
20,728 KB
testcase_02 AC 60 ms
20,952 KB
testcase_03 AC 59 ms
20,880 KB
testcase_04 AC 61 ms
20,836 KB
testcase_05 AC 60 ms
20,964 KB
testcase_06 AC 59 ms
20,892 KB
testcase_07 AC 60 ms
20,980 KB
testcase_08 AC 68 ms
23,104 KB
testcase_09 AC 66 ms
18,888 KB
testcase_10 AC 63 ms
18,912 KB
testcase_11 AC 167 ms
46,956 KB
testcase_12 AC 138 ms
41,020 KB
testcase_13 AC 151 ms
41,064 KB
testcase_14 AC 123 ms
36,296 KB
testcase_15 AC 125 ms
34,424 KB
testcase_16 AC 140 ms
38,860 KB
testcase_17 AC 132 ms
37,852 KB
testcase_18 AC 150 ms
40,736 KB
testcase_19 AC 127 ms
37,872 KB
testcase_20 AC 164 ms
45,804 KB
testcase_21 AC 126 ms
39,480 KB
testcase_22 AC 126 ms
39,596 KB
testcase_23 AC 135 ms
40,780 KB
testcase_24 AC 129 ms
36,148 KB
testcase_25 AC 132 ms
38,460 KB
testcase_26 AC 135 ms
42,828 KB
testcase_27 AC 115 ms
37,240 KB
testcase_28 AC 118 ms
35,632 KB
testcase_29 AC 130 ms
38,008 KB
testcase_30 AC 121 ms
36,712 KB
testcase_31 AC 166 ms
48,932 KB
testcase_32 AC 116 ms
35,564 KB
testcase_33 AC 121 ms
38,472 KB
testcase_34 AC 148 ms
41,424 KB
testcase_35 AC 58 ms
20,960 KB
testcase_36 AC 168 ms
48,576 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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.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>();
            Debug.Assert(1 <= N && N <= 200000);
            Debug.Assert(0 <= K && K <= N);

            var A = Cin.ReadSplitLong();
            for (var i = 0; i < N; i++) Debug.Assert(Abs(A[i]) <= 1000000000);
            for (var i = 0; i < N - 1; i++) Debug.Assert(A[i] <= A[i + 1]);
            long ans = INF;


            int l = 0, r = K - 1;
            //[l, r] を +1 にする
            long now = 0;
            for (var i = 0; i < N; i++)
            {
                if (i <= r) now += Abs(A[0] - A[i]);
                else now -= Abs(A[0] - A[i]);
            }


            for (var i = 0; i < N; i++)
            {
                //x=A[i]
                while (r < i || (r + 1 < N && Abs(A[l] - A[i]) > Abs(A[r + 1] - A[i])))
                {
                    //得をする間、区間を動かす(区間が後ろに動くことはない)
                    now = (now - 2 * Abs(A[i] - A[l]) + 2 * Abs(A[i] - A[r + 1]));
                    r++;
                    l++;
                }
                ans = Min(ans, now);

                //xの値を入れ替える
                // [0, l-1]、[i+1, r]ではxの符号は +、それ以外では -
                now -= (i - l + 1 + N - 1 - r - (l + (r - i))) * A[i];
                if (i + 1 < N)
                {
                    //次のx
                    now += (i - l + 1 + N - r - (l + (r - i - 1))) * A[i + 1];
                    now += -2 * A[i + 1];
                }
            }
            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