Saturday, January 14, 2012

SRM529

Level 1 Level 2 Level 3
Div 1 Level 1 Level 2 Level 3
Div 2 Level 1 Level 2 Level 3

Tutorials:

SRM529


Division One - Level Three:

Solution

Source Code:


Division One - Level Two:

Solution

Source Code:


Division One - Level One:

Solution

Source Code:


Sivision Two - Level Three:

Solution

Source Code:


Division Two - Level Two:

Solution

Source Code:

/**
 * The main tasks are:
 * 1st, translate Roman number to Arabic numbers;
 *      this function is implemented by toNumerials(String a);
 * 2nd, sort the Objects by specific constrains.
 *      this function is implemented by the OOP design and rewrite function compareTo();
 */

/**
 * @author antonio081014
 * @since Jan 14, 2012, 9:16:24 AM
 */
import java.util.*;
import java.util.regex.*;
import java.text.*;
import java.math.*;
import java.awt.geom.*;

public class KingSort {

    public List<Kings> list;

    public String[] getSortedList(String[] kings) {
        list = new ArrayList<Kings>();
        for (int i = 0; i < kings.length; i++) {
            String[] king = kings[i].split(" ");
            System.out.println(toNumerials(king[1]));
            list
                    .add(new Kings(kings[i], king[0], king[1],
                            toNumerials(king[1])));
        }
        Collections.sort(list);
        String[] ret = new String[kings.length];
        for (int i = 0; i < list.size(); i++) {
            ret[i] = list.get(i).fullName;
        }
        return ret;
    }

    public int toNumerials(String a) {
        int sum = toNumbers(a.charAt(0));
        char last = a.charAt(0);
        for (int i = 1; i < a.length(); i++) {
            if (toNumbers(a.charAt(i)) > toNumbers(last)) {
                sum += toNumbers(a.charAt(i));
                sum -= 2 * toNumbers(last);
            }
            else {
                sum += toNumbers(a.charAt(i));
            }
            last = a.charAt(i);
        }
        return sum;
    }

    public int toNumbers(char a) {
        if (a == 'L')
            return 50;
        if (a == 'X')
            return 10;
        if (a == 'V')
            return 5;
        if (a == 'I')
            return 1;
        return Integer.MAX_VALUE;
    }

    // <%:testing-code%>
}

class Kings implements Comparable<Kings> {
    public String fullName;
    public String name;
    public String order;
    public int    number;

    public Kings(String a, String b, String c, int d) {
        this.fullName = a;
        this.name = b;
        this.order = c;
        this.number = d;
    }

    @Override
    public int compareTo(Kings o) {
        if (this.name.compareToIgnoreCase(o.name) < 0
                || (this.name.compareToIgnoreCase(o.name) == 0 && this.number < o.number))
            return -1;
        if (this.name.compareToIgnoreCase(o.name) == 0
                && this.number == o.number)
            return 0;
        return 1;
    }
}
// Powered by [KawigiEdit] 2.0!


Division Two - Level One:

Solution

Source Code:

/**
 * SRM529, Div2 - Level1.
 *
 */

/**
 * @author antonio081014
 * @since Jan 14, 2012, 9:10:53 AM
 */
import java.util.*;
import java.util.regex.*;
import java.text.*;
import java.math.*;
import java.awt.geom.*;

public class PairingPawns {
    public int savedPawnCount(int[] start) {
        for (int i = start.length - 1; i > 0; i--) {
            start[i - 1] += start[i] / 2;
        }
        return start[0];
    }

    // <%:testing-code%>
}
// Powered by [KawigiEdit] 2.0!

No comments :