結果

問題 No.2821 A[i] ← 2A[j] - A[i]
ユーザー 👑 kakel-sankakel-san
提出日時 2024-07-26 22:52:19
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 165 ms / 1,500 ms
コード長 2,954 bytes
コンパイル時間 8,993 ms
コンパイル使用メモリ 168,056 KB
実行使用メモリ 188,776 KB
最終ジャッジ日時 2024-07-26 22:52:45
合計ジャッジ時間 14,391 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
30,720 KB
testcase_01 AC 133 ms
67,564 KB
testcase_02 AC 165 ms
98,004 KB
testcase_03 AC 155 ms
93,776 KB
testcase_04 AC 127 ms
70,132 KB
testcase_05 AC 121 ms
67,712 KB
testcase_06 AC 107 ms
55,456 KB
testcase_07 AC 102 ms
53,316 KB
testcase_08 AC 120 ms
66,176 KB
testcase_09 AC 80 ms
40,960 KB
testcase_10 AC 97 ms
44,032 KB
testcase_11 AC 86 ms
42,880 KB
testcase_12 AC 82 ms
42,496 KB
testcase_13 AC 87 ms
43,264 KB
testcase_14 AC 84 ms
43,008 KB
testcase_15 AC 91 ms
45,432 KB
testcase_16 AC 80 ms
38,912 KB
testcase_17 AC 86 ms
43,264 KB
testcase_18 AC 93 ms
48,976 KB
testcase_19 AC 67 ms
33,152 KB
testcase_20 AC 67 ms
32,896 KB
testcase_21 AC 63 ms
31,616 KB
testcase_22 AC 68 ms
32,128 KB
testcase_23 AC 66 ms
32,384 KB
testcase_24 AC 63 ms
31,464 KB
testcase_25 AC 62 ms
30,976 KB
testcase_26 AC 63 ms
31,616 KB
testcase_27 AC 62 ms
31,232 KB
testcase_28 AC 63 ms
31,616 KB
testcase_29 AC 63 ms
31,232 KB
testcase_30 AC 61 ms
31,360 KB
testcase_31 AC 63 ms
30,592 KB
testcase_32 AC 64 ms
31,212 KB
testcase_33 AC 62 ms
188,776 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (97 ms)。
MSBuild のバージョン 17.9.6+a4ecab324 (.NET)
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.0/publish/

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static int NN => int.Parse(ReadLine());
    static long[] NList => ReadLine().Split().Select(long.Parse).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Test()
    {
        var r = new Random();
        var count = 0;
        while (true)
        {
            var n = 4;
            var a = new long[n];
            for (var i = 0; i < n; ++i) a[i] = r.Next(10);
            // 愚直解
            var s1 = All(n, a);
            // 作成解
            var s2 = Gather(n, a);
            if (s1 != s2[^1])
            {
                WriteLine(n);
                // WriteLine(string.Join("\n", map.Select(m => string.Join(" ", m))));
                WriteLine(string.Join(" ", a));
                WriteLine(s1);
                WriteLine(s2[^1]);
                return;
            }
            ++count;
            if (count % 1000 == 0) WriteLine(count);
        }
    }
    static long All(int n, long[] _a)
    {
        var a = _a.ToArray();
        var d = a.Max() - a.Min();
        while (true)
        {
            var max = long.MinValue;
            var maxp = 0;
            var max2 = long.MinValue;
            var max2p = 0;
            for (var i = 0; i < n; ++i)
            {
                if (max < a[i])
                {
                    max2 = max;
                    max2p = maxp;
                    max = a[i];
                    maxp = i;
                }
                else if (max > a[i] && max2 < a[i])
                {
                    max2 = a[i];
                    max2p = i;
                }
            }
            var na = new long[n];
            for (var i = 0; i < n; ++i)
            {
                if (i == maxp || i == max2p) na[i] = a[i];
                else na[i] = a[i] + max - max2;
            }
            var nd = a.Max() - a.Min();
            if (d < nd) break;
            a = na;
            d = nd;
        }
        return d;
    }
    static void Solve()
    {
        var n = NN;
        var a = NList;
        WriteLine(string.Join("\n", Gather(n, a)));
    }
    static long[] Gather(int n, long[] a)
    {
        var ans = new long[n];
        var gcd = 0L;
        var set = new HashSet<long>{ a[0] };
        for (var i = 1; i < n; ++i)
        {
            if (!set.Contains(a[i]))
            {
                if (set.Count == 1) gcd = Math.Abs(set.First() - a[i]);
                else gcd = GCD(gcd, Math.Abs(set.First() - a[i]));
                set.Add(a[i]);
            }
            ans[i] = gcd;
        }
        return ans;
    }
    static long GCD(long a, long b)
    {
        if (a < b) return GCD(b, a);
        if (a % b == 0) return b;
        return GCD(b, a % b);
    }
}
0