C♯の勉強

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

TopCoder SRM598: BinPackingEasy

public class BinPackingEasy {
    public int minBins(int[] item) {
        Array.Sort(item);
        int ans = 0;
        int n = item.Length;
        bool[] used = new bool[n];
        for (int i = n - 1; i >= 0; i--) {
            if (used[i]) continue;
            int target = -1;
            for (int j = i - 1; j >= 0; j--) {
                if (used[j]) continue;
                if (item[i] + item[j] <= 300) {
                    target = j;
                    break;
                }
            }
            used[i] = true;
            if (target != -1) used[target] = true;
            ans++;
        }
        return ans;
    }
}