TopCoder SRM 151 DIV 1 Medium 練習
問題
数列が与えられる。これをマージソートした時に比較の回数は何回になりますか。
解
テラやるだけwwww
using System; using System.Text; using System.Text.RegularExpressions; using System.Collections; using System.Collections.Generic; public class MergeSort { int res = 0; List<int> mergeSort(List<int> a) { if (a.Count <= 1) return a; List<int> llb = new List<int>(), llc = new List<int>(); for (int i = 0; i < a.Count / 2; ++i) llb.Add(a[i]); for (int i = a.Count / 2; i < a.Count; ++i) llc.Add(a[i]); List<int> lb = mergeSort(llb); List<int> lc = mergeSort(llc); return merge(lb, lc); } List<int> merge(List<int> b, List<int> c) { List<int> n = new List<int>(); while (b.Count != 0 && c.Count != 0) { res++; if (b[0] < c[0]) { n.Add(b[0]); b.RemoveAt(0); } else if (c[0] < b[0]) { n.Add(c[0]); c.RemoveAt(0); } else { n.Add(b[0]); n.Add(c[0]); b.RemoveAt(0); c.RemoveAt(0); } } while (b.Count != 0) { n.Add(b[0]); b.RemoveAt(0); } while (c.Count != 0) { n.Add(c[0]); c.RemoveAt(0); } return n; } public int howManyComparisons(int[] numbers) { mergeSort(new List<int>(numbers)); return res; } }
それにしてもソースの汚さが半端じゃない死にたい。