結果

問題 No.694 square1001 and Permutation 3
ユーザー AreTrashAreTrash
提出日時 2018-06-09 00:26:51
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,862 ms / 3,000 ms
コード長 3,391 bytes
コンパイル時間 3,551 ms
コンパイル使用メモリ 112,704 KB
実行使用メモリ 74,448 KB
最終ジャッジ日時 2023-09-13 01:01:50
合計ジャッジ時間 14,145 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
21,940 KB
testcase_01 AC 80 ms
24,036 KB
testcase_02 AC 79 ms
21,860 KB
testcase_03 AC 81 ms
21,864 KB
testcase_04 AC 82 ms
23,912 KB
testcase_05 AC 83 ms
22,044 KB
testcase_06 AC 84 ms
23,976 KB
testcase_07 AC 808 ms
35,436 KB
testcase_08 AC 1,122 ms
48,432 KB
testcase_09 AC 1,862 ms
73,692 KB
testcase_10 AC 696 ms
37,604 KB
testcase_11 AC 1,821 ms
74,448 KB
testcase_12 AC 1,814 ms
68,804 KB
testcase_13 AC 79 ms
21,988 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.IO;
using System.Collections.Generic;
using System.Linq;

namespace No694_1
{
    public class Program
    {
        void Solve(StreamScanner ss, StreamWriter sw)
        {
            //---------------------------------
            var N = ss.Next(int.Parse);
            var A = Compress(ss.Next(long.Parse, N));

            var cs = new Dictionary<int, int>();
            foreach (var tup in A.OrderBy(x => x).Select((a, i) => (a, i)))
            {
                var (a, i) = (tup.Item1, tup.Item2);
                if (cs.ContainsKey(a)) cs[a]--;
                else cs.Add(a, N - 2 * i - 1);
            }

            var bit = new BinaryIndexedTree(N);
            var cross = 0L;
            foreach (var tup in A.Select((a, i) => (a, i)))
            {
                var (a, i) = (tup.Item1, tup.Item2);
                cross += i - bit.Sum(a);
                bit.Add(a, 1);
            }

            sw.WriteLine(cross);
            foreach (var a in A.Take(N - 1)) sw.WriteLine(cross += cs[a]);
            //---------------------------------
        }

        IEnumerable<int> Compress(IEnumerable<long> source)
        {
            var comp = source
                .Distinct()
                .OrderBy(x => x)
                .Select((k, v) => new {k, v})
                .ToDictionary(kvp => kvp.k, kvp => kvp.v);

            return source.Select(x => comp[x] + 1);
        }

        static void Main()
        {
            var ss = new StreamScanner(new StreamReader(Console.OpenStandardInput()));
            var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            new Program().Solve(ss, sw);
            sw.Flush();
        }

        static readonly Func<string, string> String = s => s;
    }

    public class BinaryIndexedTree
    {
        readonly int _size;
        readonly long[] _bit;

        public BinaryIndexedTree(int size)
        {
            _size = size;
            _bit = new long[_size + 1];
        }

        public void Add(int x, int w)
        {
            for (var i = x; i <= _size; i += i & -i) _bit[i] += w;
        }

        public long Sum(int x)
        {
            var ret = 0L;
            for (var i = x; i >= 1; i -= i & -i) ret += _bit[i];
            return ret;
        }
    }

    public class StreamScanner
    {
        static readonly char[] Sep = { ' ' };
        readonly Queue<string> buffer = new Queue<string>();
        readonly TextReader textReader;

        public StreamScanner(TextReader textReader)
        {
            this.textReader = textReader;
        }

        public T Next<T>(Func<string, T> parser)
        {
            if (buffer.Count != 0) return parser(buffer.Dequeue());
            var nextStrings = textReader.ReadLine().Split(Sep, StringSplitOptions.RemoveEmptyEntries);
            foreach (var nextString in nextStrings) buffer.Enqueue(nextString);
            return Next(parser);
        }

        public T[] Next<T>(Func<string, T> parser, int x)
        {
            var ret = new T[x];
            for (var i = 0; i < x; ++i) ret[i] = Next(parser);
            return ret;
        }

        public T[][] Next<T>(Func<string, T> parser, int x, int y)
        {
            var ret = new T[y][];
            for (var i = 0; i < y; ++i) ret[i] = Next(parser, x);
            return ret;
        }
    }
}
0