結果

問題 No.699 ペアでチームを作ろう2
ユーザー AreTrashAreTrash
提出日時 2018-06-13 00:20:32
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 64 ms / 1,000 ms
コード長 3,891 bytes
コンパイル時間 938 ms
コンパイル使用メモリ 115,136 KB
実行使用メモリ 29,212 KB
最終ジャッジ日時 2024-06-30 13:58:53
合計ジャッジ時間 2,355 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
25,124 KB
testcase_01 AC 32 ms
25,264 KB
testcase_02 AC 32 ms
25,088 KB
testcase_03 AC 31 ms
25,244 KB
testcase_04 AC 35 ms
24,956 KB
testcase_05 AC 31 ms
24,992 KB
testcase_06 AC 32 ms
25,088 KB
testcase_07 AC 64 ms
29,212 KB
testcase_08 AC 64 ms
25,388 KB
testcase_09 AC 63 ms
27,172 KB
testcase_10 AC 64 ms
27,048 KB
testcase_11 AC 63 ms
25,340 KB
testcase_12 AC 63 ms
25,084 KB
testcase_13 AC 63 ms
25,084 KB
testcase_14 AC 62 ms
25,004 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 No698
{
    public class Program
    {
        int N;
        int[] A;
        int[] B;

        void Solve(StreamScanner ss, StreamWriter sw)
        {
            //---------------------------------
            N = ss.Next(int.Parse);
            A = ss.Next(int.Parse, N / 2);
            B = ss.Next(int.Parse, N / 2);
            sw.WriteLine(Dfs(N / 2));
            //---------------------------------
        }

        int Dfs(int c)
        {
            if (c-- == 0) return Permute();

            var ret = 0;
            ret = Math.Max(ret, Dfs(c));
            MathX.Swap(ref A[c], ref B[c]);
            ret = Math.Max(ret, Dfs(c));
            MathX.Swap(ref A[c], ref B[c]);
            return ret;
        }

        int Permute()
        {
            var a = A.OrderBy(x => x).ToArray();
            var ans = 0;
            do
            {
                var power = 0;
                for (var i = 0; i < N / 2; i++) power ^= a[i] + B[i];
                ans = Math.Max(ans, power);
            }
            while (MathX.NextPermutation(a));
            return ans;
        }

        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 static partial class MathX
    {
        public static void Swap<T>(ref T left, ref T right)
        {
            var tmp = left;
            left = right;
            right = tmp;
        }

        //From: https://stackoverflow.com/questions/11208446/generating-permutations-of-a-set-most-efficiently
        public static bool NextPermutation(int[] numList)
        {
            var largestIndex = -1;
            for (var i = numList.Length - 2; i >= 0; i--)
            {
                if (numList[i] < numList[i + 1])
                {
                    largestIndex = i;
                    break;
                }
            }

            if (largestIndex < 0) return false;

            var largestIndex2 = -1;
            for (var i = numList.Length - 1; i >= 0; i--)
            {
                if (numList[largestIndex] < numList[i])
                {
                    largestIndex2 = i;
                    break;
                }
            }

            Swap(ref numList[largestIndex], ref numList[largestIndex2]);
            for (int i = largestIndex + 1, j = numList.Length - 1; i < j; i++, j--)
            {
                Swap(ref numList[i], ref numList[j]);
            }

            return true;
        }
    }

    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