週末副業記

土日は副業エンジニアのブログです。副業に関することを投稿します。

Dictionary<long,long>をList<Tuple<long,long>>に変えるとACになった件【競技プロ】【C#】


Atcoder Begin Contest 121のC問題をときました。
atcoder.jp

最初のコードがDictionary型で書いたもの、次のコードがTuple型で書いた物。
ACになったのはList>型。忘れないようにメモ。

Dictionary

using System;
using System.Linq;
using System.Collections.Generic;
 
 
namespace _121c
{
    class Program
    {
        static void Main(string[] args)
        {
            ulong[] read = Array.ConvertAll(Console.ReadLine().Split(),ulong.Parse);
            <span style="color: #ff5252">Dictionary<ulong,ulong> AB = new Dictionary<ulong, ulong>();</span>
            ulong[] readLong = new ulong[2];
            for(ulong i = 0; i < read[0]; i++){
                readLong = Array.ConvertAll(Console.ReadLine().Split(),ulong.Parse);
                <span style="color: #ff5252">AB.Add(readLong[0],readLong[1]);</span>
            }
            var sortrdAB = AB.OrderBy(x=>x.Key);
            ulong count = 0;
            ulong sum = 0;
            foreach(var tekito in sortrdAB){
                if(count+tekito.Value<=read[1]){
                    sum += tekito.Key * tekito.Value;
                    count += tekito.Value;
                }else{
                    sum += tekito.Key * (read[1]-count);
                    break;
                }
            }
            Console.WriteLine(sum);
        }
    }
}

List>

using System;
using System.Linq;
using System.Collections.Generic;


namespace _121c
{
    class Program
    {
        static void Main(string[] args)
        {
            long[] read = Array.ConvertAll(Console.ReadLine().Split(),long.Parse);
            <span style="color: #ff5252">List<Tuple<long,long>> AB = new List<Tuple<long, long>>();</span>
            long[] readLong = new long[2];
            for(long i = 0; i < read[0]; i++){
                readLong = Array.ConvertAll(Console.ReadLine().Split(),long.Parse);
                <span style="color: #ff5252">AB.Add(new Tuple<long,long>(readLong[0],readLong[1]));</span>
            }
            var sortrdAB = AB.OrderBy(x=>x.Item1);
            long count = 0;
            long sum = 0;
            foreach(var tekito in sortrdAB){
                if(count+tekito.Item2<=read[1]){
                    sum += tekito.Item1 * tekito.Item2;
                    count += tekito.Item2;
                }else{
                    sum += tekito.Item1 * (read[1]-count);
                    break;
                }
            }
            Console.WriteLine(sum);
        }
    }
}

Listだとスタックオーバーフローにならなかった。DictionaryだとRE判定だった物がListだとACに。

infoaisaka.hatenablog.com