Saturday, 25 January 2014

UVa Problem ID 11308 (Bankrupt Banker)

UVa Problem ID 11308 (Bankrupt Banker)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.TreeSet;

public class Main
{
	public static void main(String[] args) throws IOException 
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb=new StringBuffer("");
        int testcases=Integer.parseInt(br.readLine());
        if(testcases<1 || testcases>100)
        	System.exit(0);
        for (int t = 0; t < testcases; t++)
		{
        	
        	sb.append(br.readLine().toUpperCase()+"\n");
        	String s[]=br.readLine().split(" ");
        	int m=Integer.parseInt(s[0]);
        	int n=Integer.parseInt(s[1]);
        	int b=Integer.parseInt(s[2]);
        	
        	if(m<1 || m>100 || n<1 || n>100 || b<1 || b>1000000)
        		break;
        	
        	HashMap ingredientcost=new HashMap<>();
        	TreeSet recipes=new TreeSet<>(new Comparator()
			{
				@Override
				public int compare(Recipe r1,Recipe r2)
				{
					if(r1.cost>r2.cost)
						return 1;
					else if(r1.cost==r2.cost)
						return r1.name.compareTo(r2.name);
					else
						return -1;
				}
			});
        	for (int i = 0; i 100)
					break;
				int cost=0;
				for (int j = 0; j < k; j++)
				{
					String s2[]=br.readLine().split(" ");
					cost+=ingredientcost.get(s2[0])*Integer.parseInt(s2[1]);
				}
				//System.out.println("cost ="+cost);
				r1.cost=cost;
				recipes.add(r1);
			}
        	boolean inbudget=false;
        	Iterator i=recipes.iterator();
        	while (i.hasNext())
			{
				Recipe recipe = (Recipe) i.next();
				if(recipe.cost<=b)
				{
					inbudget=true;
					sb.append(recipe.name+"\n");
				}
			}
        	if(!inbudget)
        	{
        		sb.append("Too expensive!\n");
        	}
        	sb.append("\n");
		}
        System.out.print(sb);
    }
}
class Recipe
{
	String name;
	int cost;
	
	public Recipe(String s)
	{
		name=s;
	}
}

UVa Problem ID 11495 (Bubbles and Buckets)

UVa Problem ID 11495 (Bubbles and Buckets)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	static long swapcount=0;
	public static void main(String[] args) throws IOException 
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb=new StringBuffer("");
        while(true)
        {
        	swapcount=0;
        	String s[]=br.readLine().split(" ");
	        int n=Integer.parseInt(s[0]);
	        if(n<2 || n>100000)
	        	break;
	        int a[]=new int[n];
	
	        for (int i = 0; i < n; i++) {
	        	a[i]=Integer.parseInt(s[i+1]);
				if(a[i]<1 || a[i]>n)
					System.exit(0);
				
			}

	        merge_sort(a,0,a.length-1);

	        if(swapcount%2==0)
	        	sb.append("Carlos");
	        else
	        	sb.append("Marcelo");
	        sb.append("\n");
        }
        System.out.print(sb);
    }
	public static void merge_sort(int[] a,int start,int end) {
			if(startmiddle)
			{
				combinedarray[k]=a[j++];
			}
			else if(j>end)
			{
				combinedarray[k]=a[i++];
			}
			else
			{
				if(a[i]>a[j])
				{
					combinedarray[k]=a[j++];
					swapcount+=middle-i+1;
				}
				else
					combinedarray[k]=a[i++];
			}
			
		}
		int p=0;
		for (int j2 = start; j2 <=end; j2++)
		{
			a[j2]=combinedarray[p++];
		}
	}	
	

}

UVa Problem ID 10810 (Ultra Quicksort)

UVa Problem ID 10810 (Ultra Quicksort)
//Note :- use long , int is not enough

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	static long swapcount=0;
	public static void main(String[] args) throws IOException 
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb=new StringBuffer("");
        while(true)
        {
        	swapcount=0;
	        int n=Integer.parseInt(br.readLine());
	        if(n==0 || n>500000)
	        	break;
	        long a[]=new long[n];
	
	        for (int i = 0; i < n; i++) {
	        	a[i]=Long.parseLong(br.readLine());
				if(a[i]<0 || a[i]>999999999)
					break;
				
			}
//	        for (int i = 0; i < a.length; i++) {
//				System.out.print(a[i]+" ");
//			}
//	        System.out.println("");
	        merge_sort(a,0,a.length-1);
//	        for (int i = 0; i < a.length; i++) {
//				System.out.print(a[i]+" ");
//			}
//	        System.out.println("");
	        sb.append(swapcount+"\n");
        }
        System.out.print(sb);
    }
	public static void merge_sort(long[] a,int start,int end) {
			if(startmiddle)
			{
				combinedarray[k]=a[j++];
			}
			else if(j>end)
			{
				combinedarray[k]=a[i++];
			}
			else
			{
				if(a[i]>a[j])
				{
					combinedarray[k]=a[j++];
					swapcount+=middle-i+1;
				}
				else
					combinedarray[k]=a[i++];
			}
			
		}
		int p=0;
		for (int j2 = start; j2 <=end; j2++)
		{
			a[j2]=combinedarray[p++];
		}
	}	
	

}

UVa Problem ID 673 (Parentheses Balance)

UVa Problem ID 673 (Parentheses Balance)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

public class Main
{
	public static void main(String[] args) throws IOException
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String input;
		StringBuffer sb = new StringBuffer("");
		int testcases = Integer.parseInt(br.readLine());

		for (int i = 0; i < testcases; i++)
		{
			boolean iscorrect = true;
			input = br.readLine();
			Stack st = new Stack();
			for (int j = 0; j < input.length(); j++)
			{
				if (iscorrect)
				{
					switch (input.charAt(j))
					{
						case '(':
						{
							st.push('(');
							break;
						}
						case '[':
						{
							st.push('[');
							break;
						}
						case ']':
						{
							if(st.isEmpty())
							{
								iscorrect=false;
								break;
							}
							char c = st.pop();
							if (c != '[')
							{
								iscorrect = false;
							}
							break;
						}
						case ')':
						{
							if(st.isEmpty())
							{
								iscorrect=false;
								break;
							}
							char c = st.pop();
							if (c != '(')
							{
								iscorrect = false;
							}
							break;
						}
					}
				}
			}
			if(iscorrect && st.isEmpty())
				sb.append("Yes");
			else
				sb.append("No");
			sb.append("\n");
		}
		System.out.print(sb);
	}

}

UVa Problem ID 458 (The Decoder)

UVa Problem ID 458 (The Decoder)
//Note:-be careful about encoding

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Main {
	public static void main(String[] args) throws IOException 
    {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in,"ISO-8859-1"));        
		OutputStreamWriter cout = new OutputStreamWriter(System.out, "ISO-8859-1");

        StringBuffer sb=new StringBuffer("");
        String input;
        while ((input=br.readLine())!=null) {
        	for (int i = 0; i 

Advertisement