搜寻最快的数据结构

时间:2019-04-01 18:10:38

标签: c# algorithm sorting data-structures

我有一个算法学校项目,在这里我必须跟踪学生组织中有多少个任务。每个任务都有一个ID和一个难度级别。我从用户那里得到用户输入,可以是“ R”(当他们请求任务时)或“ N id难度”,其中N表示我必须创建一个新任务,id是任务的ID,难度是任务的难处。

每次组织中的某人请求一项任务时,我都需要遍历我的数据结构并找到难度最高的任务,并返回此任务的ID(密钥)。目前,我正在这样解决它:

class Program
{
    static void Main(string[] args)
    {
        int commands = int.Parse(Console.ReadLine());
        var list = new List<KeyValuePair<int, int>>();

        for(int i = 0; i<commands; i++)
        {
            string[] input = Console.ReadLine().Split(" ");
            if (input[0].Equals("N"))
            {
                list.Add(new KeyValuePair<int, int>(int.Parse(input[1]), int.Parse(input[2])));
            }
            if (input[0].Equals("R"))
            {
                var max = list.Aggregate((l, r) => l.Value > r.Value ? l : r).Key;
                list.Remove(list.First(item => item.Key.Equals(max)));
                Console.WriteLine(max);
            }

        }
        Console.ReadLine();
    }
}

}

我已将其提交给CodeJudge,但是我得到的答复是已超过了时间限制,因此我认为这是因为我的数据结构不够快。 如果有人知道更快的数据结构,可以在其中保存键和值并更快地进行遍历,那将非常有帮助。

0 个答案:

没有答案
相关问题