Saturday, November 2, 2013

SRM596


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


Tutorials:


Division One - Level Three:

Solution

Source Code:


Division One - Level Two:

Solution

Source Code:


Division One - Level One:

Solution

Source Code:


Division Two - Level Three:

Solution

Source Code:


Division Two - Level Two:

Solution

Using recursive function to try all of the possible way.

Source Code:

import java.util.*;
import java.util.regex.*;
import java.text.*;
import java.math.*;
import java.awt.geom.*;

public class ColorfulRoad
{
	private static final char[] nextC = {'R', 'G', 'B'};
	public int getMin(String road)
	{
		int min = minCost(road.substring(1), 1);
		return min == Integer.MAX_VALUE ? -1 : min;
	}

	private int minCost(String road, int next){
		if(road.length() ==0) return 0;
		long min = Integer.MAX_VALUE;
		for(int i=0; i<road.length(); i++){
			if(road.charAt(i) == nextC[next]){
				//System.out.println(road);
				int tmp = minCost(road.substring(i+1), (next+1)%3);
				if (tmp == Integer.MAX_VALUE) continue;
				min = Math.min(min, (i+1)*(i+1)+ tmp);
			}
		}
		return (int)min;
	}
}
//Powered by [KawigiEdit] 2.0!



Division Two - Level One:

Solution

Source Code:

import java.util.*;
import java.util.regex.*;
import java.text.*;
import java.math.*;
import java.awt.geom.*;

public class FoxAndSightseeing
{
	public int getMin(int[] position)
	{
		int min = Integer.MAX_VALUE;
		for(int i=1; i<position.length-1; i++){
			int last = 0;
			int sum = 0;
			for(int j=0; j<position.length; j++){
				if(i!=j){
					sum += Math.abs(position[j] - position[last]);
					last = j;
				}
			}
			min = Math.min(min, sum);
		}
		return min;
	}
	
	
}