結果

問題 No.2382 Amidakuji M
ユーザー 👑 kakel-sankakel-san
提出日時 2023-07-14 23:42:54
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 168 ms / 2,000 ms
コード長 2,184 bytes
コンパイル時間 2,097 ms
コンパイル使用メモリ 70,016 KB
実行使用メモリ 42,856 KB
最終ジャッジ日時 2023-10-14 14:13:37
合計ジャッジ時間 4,753 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
21,800 KB
testcase_01 AC 65 ms
21,928 KB
testcase_02 AC 65 ms
21,828 KB
testcase_03 AC 106 ms
28,340 KB
testcase_04 AC 143 ms
38,036 KB
testcase_05 AC 82 ms
23,112 KB
testcase_06 AC 112 ms
30,800 KB
testcase_07 AC 96 ms
26,624 KB
testcase_08 AC 75 ms
22,468 KB
testcase_09 AC 134 ms
38,588 KB
testcase_10 AC 160 ms
39,884 KB
testcase_11 AC 100 ms
27,524 KB
testcase_12 AC 136 ms
38,824 KB
testcase_13 AC 64 ms
23,960 KB
testcase_14 AC 64 ms
21,980 KB
testcase_15 AC 65 ms
23,888 KB
testcase_16 AC 65 ms
23,936 KB
testcase_17 AC 64 ms
21,812 KB
testcase_18 AC 64 ms
21,868 KB
testcase_19 AC 165 ms
40,792 KB
testcase_20 AC 167 ms
42,856 KB
testcase_21 AC 168 ms
42,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;
class Program
{
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static long[] LList => ReadLine().Split().Select(long.Parse).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var c = LList;
        var (n, m) = ((int)c[0], c[1]);
        var p = NList;

        var ft = new FenwickTree(n + 2);
        var rev = 0L;
        for (var i = 0; i < n; ++i)
        {
            rev += ft.Sum(n + 1) - ft.Sum(p[i]);
            ft.Add(p[i], 1);
        }
        var d = 0L;
        if (rev != 0) d = (m + rev - 1) / m;

        if (m * d % 2 == rev % 2) WriteLine(m * d);
        else if (m % 2 == 1) WriteLine(m * (d + 1));
        else WriteLine(-1);
    }
    class FenwickTree
    {
        int size;
        long[] tree;
        public FenwickTree(int size)
        {
            this.size = size;
            tree = new long[size + 2];
        }
        public void Add(int index, int value)
        {
            ++index;
            for (var x = index; x <= size; x += (x & -x)) tree[x] += value;
        }
        /// <summary>先頭からindexまでの和(include index)</summary>
        public long Sum(int index)
        {
            ++index;
            var sum = 0L;
            for (var x = index; x > 0; x -= (x & -x)) sum += tree[x];
            return sum;
        }
        /// <summary>Sum(x) >= value となる最小のxを求める</summary>
        // 各要素は非負であること
        // sizeが2べきでない場合に正しく動く?
        public int LowerBound(long value)
        {
            if (value < 0) return -1;
            var x = 0;
            var b = 1;
            while (b * 2 <= size) b <<= 1;
            for (var k = b; k > 0; k >>= 1)
            {
                if (x + k <= size && tree[x + k] < value)
                {
                    value -= tree[x + k];
                    x += k;
                }
            }
            return x;
        }
    }
}
0