結果

問題 No.1391 ±1 Abs Sum
ユーザー eSeF
提出日時 2020-11-08 13:04:15
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 137 ms / 2,000 ms
コード長 3,471 bytes
コンパイル時間 944 ms
コンパイル使用メモリ 107,904 KB
実行使用メモリ 43,776 KB
最終ジャッジ日時 2024-07-22 15:05:42
合計ジャッジ時間 5,341 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます
コンパイルメッセージ
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