結果

問題 No.43 野球の試合
ユーザー sekiya9311sekiya9311
提出日時 2017-10-09 01:47:08
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 61 ms / 5,000 ms
コード長 5,331 bytes
コンパイル時間 883 ms
コンパイル使用メモリ 120,040 KB
実行使用メモリ 31,660 KB
最終ジャッジ日時 2024-04-28 13:39:24
合計ジャッジ時間 1,723 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
23,476 KB
testcase_01 AC 28 ms
25,328 KB
testcase_02 AC 28 ms
25,264 KB
testcase_03 AC 30 ms
25,524 KB
testcase_04 AC 30 ms
27,664 KB
testcase_05 AC 30 ms
27,504 KB
testcase_06 AC 30 ms
25,456 KB
testcase_07 AC 61 ms
31,660 KB
testcase_08 AC 29 ms
25,500 KB
testcase_09 AC 29 ms
25,324 KB
testcase_10 AC 28 ms
27,552 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.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProgrammingContest
{
    class MainClass
    {
        Scanner sc;
        static void Main(string[] args)
        {
            new MainClass().Solve();
        }

        void Solve()
        {
#if DEBUG
            string backPath = "..";
            char dirSep = System.IO.Path.DirectorySeparatorChar;
            string inFilePath = backPath + dirSep + backPath + dirSep + "in.txt";
            sc = new Scanner(new System.IO.StreamReader(inFilePath));
#else
            sc = new Scanner();
#endif

            int n = sc.NextInt;
            char[][] s = new char[n][].Select(chars => sc.Next.ToCharArray()).ToArray();
            //for (int i = 1; i < n; i++)
            //{
            //    if (s[0][i] == '-')
            //    {
            //        s[0][i] = 'o';
            //        s[i][0] = 'x';
            //    }
            //}

            List<Tuple<int, int>> list = new List<Tuple<int, int>>();
            for (int i = 0; i < n; i++)
            {
                for (int j = i + 1; j < n; j++)
                {
                    if (s[i][j] == '-')
                    {
                        list.Add(Tuple.Create(i, j));
                    }
                }
            }
            
            if (list.Count == 0)
            {
                Write(Calc(n, s));
                return;
            }
            int ans = 111;
            for (int firstWin = 0; firstWin < (1 << list.Count); firstWin++)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    if ((firstWin & (1 << i)) != 0)
                    {
                        s[list[i].Item1][list[i].Item2] = 'o';
                        s[list[i].Item2][list[i].Item1] = 'x';
                    }
                    else
                    {
                        s[list[i].Item1][list[i].Item2] = 'x';
                        s[list[i].Item2][list[i].Item1] = 'o';
                    }
                }
                ans = Math.Min(ans, Calc(n, s));
            }
            Write(ans);    
        }

        int Calc(int n, char[][] s)
        {
            int[] winCnt = new int[n];
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (s[i][j] == 'o')
                    {
                        winCnt[i]++;
                    }
                }
            }
            State[] st = new State[n];
            for (int i = 0; i < n; i++)
            {
                st[i] = new State();
                st[i].win = winCnt[i];
                st[i].idx = i;
            }
            Array.Sort(st);
            Array.Reverse(st);
            int rank = 1;
            int preWinCnt = -1;
            for (int i = 0; i < n; i++)
            {
                if (st[i].idx == 0)
                {
                    return rank;
                }
                if (preWinCnt != st[i].win)
                {
                    preWinCnt = st[i].win;
                    rank++;
                }
            }
            throw new Exception();
        }

        struct State : IComparable<State>
        {
            public int win, idx;
            public int CompareTo(State other)
            {
                int ret = Math.Sign(this.win - other.win);
                if (ret == 0)
                {
                    ret = Math.Sign(other.idx - this.idx);
                }
                return ret;
            }
        }

        void Write(object val)
        {
            Console.WriteLine(val.ToString());
        }
        void Write(string format, params object[] vals)
        {
            Console.WriteLine(format, vals);
        }
    }
    class Scanner
    {
        Queue<String> buffer;
        char[] sep;
        System.IO.TextReader reader;
        public Scanner(System.IO.TextReader reader = null)
        {
            this.buffer = new Queue<string>();
            this.sep = new char[] { ' ' };
            this.reader = (reader ?? Console.In);
        }
        private void CheckBuffer()
        {
            if (this.buffer.Count == 0 && this.reader.Peek() >= 0)
            {
                string[] sreArray = this.reader.ReadLine().Split(this.sep);
                foreach (String elStr in sreArray)
                {
                    this.buffer.Enqueue(elStr);
                }
            }
        }

        public String Next
        {
            get
            {
                this.CheckBuffer();
                return this.buffer.Dequeue();
            }
        }

        public int NextInt
        {
            get
            {
                return int.Parse(this.Next);
            }
        }
        public double NextDouble
        {
            get
            {
                return double.Parse(this.Next);
            }
        }

        public long NextLong
        {
            get
            {
                return long.Parse(this.Next);
            }
        }
        public bool IsEmpty
        {
            get
            {
                this.CheckBuffer();
                return this.buffer.Count == 0;
            }
        }
    }
}
0