結果

問題 No.1120 Strange Teacher
ユーザー bluemegane
提出日時 2021-05-01 16:23:19
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 105 ms / 1,000 ms
コード長 1,250 bytes
コンパイル時間 1,940 ms
コンパイル使用メモリ 106,112 KB
実行使用メモリ 37,376 KB
最終ジャッジ日時 2024-07-20 00:28:51
合計ジャッジ時間 4,235 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System.Linq;
using static System.Math;
using System;

public class Hello
{
    static void Main()
    {
        var n = int.Parse(Console.ReadLine().Trim());
        string[] line = Console.ReadLine().Trim().Split(' ');
        var a = Array.ConvertAll(line, long.Parse);
        line = Console.ReadLine().Trim().Split(' ');
        var b = Array.ConvertAll(line, long.Parse);
        if (n == 2) getAns2(n, a, b);
        else getAns3(n, a, b);
    }
    static void getAns2(int n, long[] a, long[] b)
    {
        if (a.Sum() != b.Sum()) { Console.WriteLine(-1); return; }
        Console.WriteLine(Abs(a[0] - b[0]));
    }
    static void getAns3(int n, long[] a, long[] b)
    {
        var d = a.Sum() - b.Sum();
        if (d % (n - 2L) != 0) { Console.WriteLine(-1); return; }
        var t = d / (n - 2L);
        for (int i = 0; i < n; i++) a[i] -= t;
        var count = 0L;
        for (int i = 0; i < n; i++)
        {
            var w = b[i] - a[i];
            if (w < 0) { Console.WriteLine(-1); return; }
            if (w > 0)
            {
                if (w % 2L != 0) { Console.WriteLine(-1); return; }
                else count += w / 2L;
            }
        }
        if (count == t) Console.WriteLine(t);
    }
}
0