結果

問題 No.1265 Balloon Survival
ユーザー terry_u16terry_u16
提出日時 2020-10-23 22:14:49
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 250 ms / 2,000 ms
コード長 10,189 bytes
コンパイル時間 1,489 ms
コンパイル使用メモリ 119,044 KB
実行使用メモリ 43,776 KB
最終ジャッジ日時 2024-07-21 10:49:31
合計ジャッジ時間 6,468 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
19,840 KB
testcase_01 AC 30 ms
20,224 KB
testcase_02 AC 29 ms
19,968 KB
testcase_03 AC 32 ms
19,968 KB
testcase_04 AC 32 ms
19,712 KB
testcase_05 AC 34 ms
19,968 KB
testcase_06 AC 33 ms
19,584 KB
testcase_07 AC 32 ms
20,224 KB
testcase_08 AC 33 ms
19,968 KB
testcase_09 AC 32 ms
20,224 KB
testcase_10 AC 33 ms
20,096 KB
testcase_11 AC 33 ms
19,968 KB
testcase_12 AC 32 ms
20,352 KB
testcase_13 AC 34 ms
19,840 KB
testcase_14 AC 56 ms
22,528 KB
testcase_15 AC 50 ms
21,888 KB
testcase_16 AC 41 ms
20,608 KB
testcase_17 AC 146 ms
35,840 KB
testcase_18 AC 43 ms
20,608 KB
testcase_19 AC 40 ms
20,864 KB
testcase_20 AC 56 ms
22,272 KB
testcase_21 AC 83 ms
25,472 KB
testcase_22 AC 66 ms
23,680 KB
testcase_23 AC 67 ms
24,704 KB
testcase_24 AC 244 ms
43,392 KB
testcase_25 AC 246 ms
43,520 KB
testcase_26 AC 246 ms
43,776 KB
testcase_27 AC 243 ms
43,648 KB
testcase_28 AC 250 ms
43,264 KB
testcase_29 AC 247 ms
43,264 KB
testcase_30 AC 243 ms
43,392 KB
testcase_31 AC 243 ms
43,648 KB
testcase_32 AC 243 ms
43,648 KB
testcase_33 AC 240 ms
43,520 KB
testcase_34 AC 33 ms
20,096 KB
testcase_35 AC 32 ms
19,968 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.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using YukicoderContest271.Extensions;
using YukicoderContest271.Questions;
using System.Diagnostics.CodeAnalysis;

namespace YukicoderContest271.Questions
{
    public class QuestionB : AtCoderQuestionBase
    {
        public override IEnumerable<object> Solve(TextReader inputStream)
        {
            var n = inputStream.ReadInt();
            var balloons = new Coordinate[n];

            for (int i = 0; i < balloons.Length; i++)
            {
                var (x, y) = inputStream.ReadValue<int, int>();
                balloons[i] = new Coordinate(x, y);
            }

            var collisions = new List<Collision>();

            for (int i = 0; i < balloons.Length; i++)
            {
                for (int j = i + 1; j < balloons.Length; j++)
                {
                    collisions.Add(new Collision(i, j, balloons[i].GetSquaredDistanceTo(balloons[j])));
                }
            }

            collisions.Sort();
            var removed = new HashSet<int>();
            var count = 0;

            foreach (var collision in collisions.ToArray())
            {
                if (collision.Index1 == 0)
                {
                    if (removed.Add(collision.Index2))
                    {
                        count++;
                    }
                }
                else
                {
                    if (!removed.Contains(collision.Index1) && !removed.Contains(collision.Index2))
                    {
                        removed.Add(collision.Index1);
                        removed.Add(collision.Index2);
                    }
                }
            }

            yield return count;
        }

        [StructLayout(LayoutKind.Auto)]
        readonly struct Collision : IComparable<Collision>
        {
            public readonly int Index1;
            public readonly int Index2;
            public readonly long Distance;

            public Collision(int index1, int index2, long distance)
            {
                Index1 = index1;
                Index2 = index2;
                Distance = distance;
            }

            public int CompareTo([AllowNull] Collision other) => Distance.CompareTo(other.Distance);

            public void Deconstruct(out int index1, out int index2) => (index1, index2) = (Index1, Index2);
            public override string ToString() => $"{nameof(Index1)}: {Index1}, {nameof(Index2)}: {Index2}";
        }

        [StructLayout(LayoutKind.Auto)]
        readonly struct Coordinate
        {
            public readonly int X;
            public readonly int Y;

            public Coordinate(int x, int y)
            {
                X = x;
                Y = y;
            }

            public long GetSquaredDistanceTo(Coordinate other)
            {
                long dx = X - other.X;
                long dy = Y - other.Y;
                return dx * dx + dy * dy;
            }

            public void Deconstruct(out int x, out int y) => (x, y) = (X, Y);
            public override string ToString() => $"{nameof(X)}: {X}, {nameof(Y)}: {Y}";
        }
    }
}

namespace YukicoderContest271
{
    class Program
    {
        static void Main(string[] args)
        {
            IAtCoderQuestion question = new QuestionB();
            var answers = question.Solve(Console.In);

            var writer = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(writer);
            foreach (var answer in answers)
            {
                Console.WriteLine(answer);
            }
            Console.Out.Flush();
        }
    }
}

#region Base Class

namespace YukicoderContest271.Questions
{

    public interface IAtCoderQuestion
    {
        IEnumerable<object> Solve(string input);
        IEnumerable<object> Solve(TextReader inputStream);
    }

    public abstract class AtCoderQuestionBase : IAtCoderQuestion
    {
        public IEnumerable<object> Solve(string input)
        {
            var stream = new MemoryStream(Encoding.Unicode.GetBytes(input));
            var reader = new StreamReader(stream, Encoding.Unicode);

            return Solve(reader);
        }

        public abstract IEnumerable<object> Solve(TextReader inputStream);
    }
}

#endregion

#region Extensions

namespace YukicoderContest271.Extensions
{
    public static class StringExtensions
    {
        public static string Join<T>(this IEnumerable<T> source) => string.Concat(source);
        public static string Join<T>(this IEnumerable<T> source, char separator) => string.Join(separator, source);
        public static string Join<T>(this IEnumerable<T> source, string separator) => string.Join(separator, source);
    }

    public static class TextReaderExtensions
    {
        public static int ReadInt(this TextReader reader) => int.Parse(ReadString(reader));
        public static long ReadLong(this TextReader reader) => long.Parse(ReadString(reader));
        public static double ReadDouble(this TextReader reader) => double.Parse(ReadString(reader));
        public static string ReadString(this TextReader reader) => reader.ReadLine();

        public static int[] ReadIntArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(int.Parse).ToArray();
        public static long[] ReadLongArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(long.Parse).ToArray();
        public static double[] ReadDoubleArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(double.Parse).ToArray();
        public static string[] ReadStringArray(this TextReader reader, char separator = ' ') => reader.ReadLine().Split(separator);

        // Supports primitive type only.
        public static T1 ReadValue<T1>(this TextReader reader) => (T1)Convert.ChangeType(reader.ReadLine(), typeof(T1));

        public static (T1, T2) ReadValue<T1, T2>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            return (v1, v2);
        }

        public static (T1, T2, T3) ReadValue<T1, T2, T3>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            return (v1, v2, v3);
        }

        public static (T1, T2, T3, T4) ReadValue<T1, T2, T3, T4>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
            return (v1, v2, v3, v4);
        }

        public static (T1, T2, T3, T4, T5) ReadValue<T1, T2, T3, T4, T5>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
            var v5 = (T5)Convert.ChangeType(inputs[4], typeof(T5));
            return (v1, v2, v3, v4, v5);
        }

        public static (T1, T2, T3, T4, T5, T6) ReadValue<T1, T2, T3, T4, T5, T6>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
            var v5 = (T5)Convert.ChangeType(inputs[4], typeof(T5));
            var v6 = (T6)Convert.ChangeType(inputs[5], typeof(T6));
            return (v1, v2, v3, v4, v5, v6);
        }

        public static (T1, T2, T3, T4, T5, T6, T7) ReadValue<T1, T2, T3, T4, T5, T6, T7>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
            var v5 = (T5)Convert.ChangeType(inputs[4], typeof(T5));
            var v6 = (T6)Convert.ChangeType(inputs[5], typeof(T6));
            var v7 = (T7)Convert.ChangeType(inputs[6], typeof(T7));
            return (v1, v2, v3, v4, v5, v6, v7);
        }

        public static (T1, T2, T3, T4, T5, T6, T7, T8) ReadValue<T1, T2, T3, T4, T5, T6, T7, T8>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
            var v5 = (T5)Convert.ChangeType(inputs[4], typeof(T5));
            var v6 = (T6)Convert.ChangeType(inputs[5], typeof(T6));
            var v7 = (T7)Convert.ChangeType(inputs[6], typeof(T7));
            var v8 = (T8)Convert.ChangeType(inputs[7], typeof(T8));
            return (v1, v2, v3, v4, v5, v6, v7, v8);
        }
    }
}

#endregion
0