C♯の勉強

C♯4.0 で TopCoder の過去問を解きます。

2013-10-14から1日間の記事一覧

TopCoder SRM578: WolfInZooDivTwo

public class WolfInZooDivTwo { struct Interval { public int left, right; } int N; Interval[] intervals; int?[,] memo; int dfs(int pos = 0, int last = -1) { if (pos == N) return 1; if (!memo[pos, last + 1].HasValue) { int val = dfs(pos + 1,…

TopCoder SRM578: GooseInZooDivTwo

public class GooseInZooDivTwo { public int count(string[] field, int dist) { int w = field[0].Length; int h = field.Length; int mod = 1000000007; var points = from i in Enumerable.Range(0, h) from j in Enumerable.Range(0, w) where field[i]…

TopCoder SRM578: DeerInZooDivTwo

public class DeerInZooDivTwo { public int[] getminmax(int N, int K) { return new[]{ Math.Max(0, N-K), N - (K+1) / 2 }; } }

TopCoder SRM579: TravellingPurchasingMan

「現在の位置」「買い物をした店のbit表現」 = 最小の時間 で動的計画法を行う。今回は PriorityQueue の実装も兼ねて、動的計画法ではなくダイクストラで解いた。 public class PriorityQueue { private T[] array = new T[100]; private int size = 0; pri…

TopCoder SRM579: MarblePositioning

public class MarblePositioning { static public void Swap(ref T a, ref T b) { T t = a; a = b; b = t; } static public bool NextPermutation(T[] array) where T : IComparable { int n = array.Length; for (int i = n - 2; i >= 0; i--) { if (array[…

TopCoder SRM579: UndoHistory

各行ごとに buffer を使う場合と、undo を使う場合を試して、手数が少ない方を採用する。 public class UndoHistory { int CommonPrefixLength(string a, string b) { return a.Zip(b, (c1, c2) => c1 == c2).TakeWhile(v => v).Count(); } public int minPr…

TopCoder SRM579: PrimalUnlicensedCreatures

弱い順に倒していけば良い。 public class PrimalUnlicensedCreatures { public int maxWins(int initialLevel, int[] grezPower) { Array.Sort(grezPower); int ans = 0; foreach (var power in grezPower) { if (initialLevel > power) { initialLevel += …