SyntaxHighlighter

Saturday 24 December 2011

SRM 526.5

Div2 - 250 MagicStonesStore
public class MagicStonesStore {
    public String ableToDivide(int n) {
        return n > 1 ? "YES" : "NO";
    }
}
Div2 - 500, Div1 - 250 MagicCandy
public class MagicCandy {
    public int whichOne(int n) {
        int t = (int)Math.sqrt(n * 1.0);
        if (t * t == n) return n - t + 1;
        int tmp = (t + 1) * (t + 1) - (t + 1) + 1;
        if (tmp <= n) return tmp;
        return t * t + 1;
    }
}
Div2 - 1000 MagicNaming
public class MagicNaming {
    String S;
    
    boolean check(String s1, String s2) {
        String t1 = s1 + s2;
        String t2 = s2 + s1;
        return t1.compareTo(t2) <= 0;
    }
    Integer dp[][] = new Integer[100][100];
    int rec(int k, int prev) {
        if (k == S.length()) return 0;
        if (dp[k][prev] != null) return dp[k][prev];
        String s = S.substring(prev, k);
        int res = Integer.MIN_VALUE;    
        for (int i = k; i < S.length(); ++i) {
            String t = S.substring(k, i + 1);
            if (check(s, t)) {
                int x = rec(i + 1, k);
                if (x == Integer.MIN_VALUE) continue;
                res = Math.max(res, x + 1);
            }
        }
        return dp[k][prev] = res;
    }

    public int maxReindeers(String magicName) {
        this.S = magicName;
        return rec(0, 0);
     }
}
Div1 - 500 MagicBlizzard




Div1 - 1000 MagicMatchesGame

No comments:

Post a Comment