結果

問題 No.699 ペアでチームを作ろう2
ユーザー AreTrashAreTrash
提出日時 2018-06-13 00:20:32
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 89 ms / 1,000 ms
コード長 3,891 bytes
コンパイル時間 2,263 ms
コンパイル使用メモリ 107,428 KB
実行使用メモリ 23,752 KB
最終ジャッジ日時 2023-09-13 03:43:59
合計ジャッジ時間 4,209 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 61 ms
19,580 KB
testcase_01 AC 62 ms
21,680 KB
testcase_02 AC 63 ms
23,660 KB
testcase_03 AC 61 ms
21,604 KB
testcase_04 AC 64 ms
21,608 KB
testcase_05 AC 62 ms
21,768 KB
testcase_06 AC 64 ms
21,704 KB
testcase_07 AC 89 ms
21,628 KB
testcase_08 AC 87 ms
19,648 KB
testcase_09 AC 89 ms
21,540 KB
testcase_10 AC 88 ms
23,712 KB
testcase_11 AC 88 ms
23,600 KB
testcase_12 AC 88 ms
23,752 KB
testcase_13 AC 87 ms
21,596 KB
testcase_14 AC 87 ms
21,704 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