C#一定超时

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace SwissRound
{
    class Player : IComparable<Player>
    {
        public int Score { get; set; }
        public int Rank { get; set; }
        public int Number { get; set; }

        public int CompareTo(Player other)
        {
            if (Score > other.Score)
                return -1;
            else if (Score < other.Score)
                return 1;
            else
            {
                if (Number < other.Number)
                    return -1;
                else
                    return 1;
            }
        }
    }

    class Program
    {
        static int N;
        static int Total;

        static bool Compete(Player a, Player b)
        {
            if (a.Rank > b.Rank)
            {
                a.Score++;
                return true;
            }
            else
            {
                b.Score++;
                return false;
            }
        }

        static void Merge(Queue<Player> players, Queue<Player> winners, Queue<Player> losers)
        {
            for (int i = 0; i < Total; i++)
            {
                if(losers.Count==0)
                {
                    players.Enqueue(winners.Dequeue());
                }
                else if(winners.Count==0)
                {
                    players.Enqueue(losers.Dequeue());
                }
                else if(winners.Peek().CompareTo(losers.Peek())==-1)
                {
                    players.Enqueue(winners.Dequeue());
                }
                else
                {
                    players.Enqueue(losers.Dequeue());
                }
            }
        }

        static void Main(string[] args)
        {
            //FileStream file = new FileStream("input.in", FileMode.Open);
            //StreamReader input = new StreamReader(file);

            List<int> param = Console.ReadLine().Split(' ').ToList()
                .ConvertAll((x) => int.Parse(x));
            N = param[0];
            Total = N * 2;
            int R = param[1], Q = param[2];
            param = null;

            List<int> initialScores = Console.ReadLine().Split(' ').ToList()
                .ConvertAll((x) => int.Parse(x));
            List<int> ranks = Console.ReadLine().Split(' ').ToList()
                .ConvertAll((x) => int.Parse(x));

            List<Player> players = new List<Player>(2 * N);
            for (int i = 0; i < Total; i++)
            {
                Player player = new Player();
                player.Score = initialScores[i];
                player.Rank = ranks[i];
                player.Number = i + 1;
                players.Add(player);
            }
            players.Sort();

            Queue<Player> winners = new Queue<Player>(N);
            Queue<Player> losers = new Queue<Player>(N);
            Queue<Player> playerQueue = new Queue<Player>(2 * N);
            Player a, b;

            foreach(Player p in players)
            {
                playerQueue.Enqueue(p);
            }

            for (int i = 0; i < R; i++)
            {
                
                for (int j = 0; j < N; j++)
                {
                    a = playerQueue.Dequeue();
                    b = playerQueue.Dequeue();
                    if (Compete(a, b) == true)
                    {
                        winners.Enqueue(a);
                        losers.Enqueue(b);
                    }
                    else
                    {
                        winners.Enqueue(b);
                        losers.Enqueue(a);
                    }
                }
                Merge(playerQueue, winners, losers);
            }

            Console.WriteLine(playerQueue.ToList()[Q - 1].Number);

            //Console.ReadLine();
        }
    }
}

不知道有没有办法优化。

0 条评论

目前还没有评论...

信息

ID
1771
难度
7
分类
模拟 | 数据结构 | 队列 点击显示
标签
递交数
3535
已通过
703
通过率
20%
被复制
19
上传者