Thursday, 20 February 2014

UVA Problem ID 10107 (What is the Median?)

UVA Problem ID 10107 (What is the Median?)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.TreeSet;

public class Main
{
	public static void main(String[] args) throws IOException
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String input;
		StringBuilder sb = new StringBuilder("");
		TreeSet ts=new TreeSet();
		int ar[]=new int[10000];
		int k=0;
		while((input=br.readLine())!=null)
		{
			input=input.trim();
			int n=Integer.parseInt(input);
			ar[k++]=n;
			Arrays.sort(ar,0,k);
				
			if((k)%2!=0)
			{
				sb.append(ar[k/2]);
			}
			else
			{
				sb.append((ar[k/2-1]+ar[k/2])/2);

			}
			sb.append("\n");
		}
		System.out.print(sb);
	}
}

Saturday, 25 January 2014

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 11462 (Age Sort)

UVa Problem ID 11462 (Age Sort)
//Note :-another approach use counting sort

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

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

Thursday, 23 January 2014

UVa Problem ID 612 (DNA Sorting)

UVa Problem ID 612 (DNA Sorting)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

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 M=Integer.parseInt(br.readLine());
        boolean blank = false;
       
        for (int i = 0; i 50 || m<=0 || m>100)
				break;
			
			String in[]=new String[m];
			int value[]=new int[m];
			
			
			for (int j = 0; j input.charAt(j2))
						{
							total+=1;
						}
					}
				}
				
				in[j]=input;
				value[j]=total;
			}
			
			for(int j=1;j0 && value[j2-1]>value[j2])
	            {
	                int temp=value[j2-1];
	                value[j2-1]=value[j2];
	                value[j2]=temp;
	                String s1=in[j2-1];
					in[j2-1]=in[j2];
					in[j2]=s1;
	                j2--;
	            }
	            
	        }
			
			if ( blank ) sb.append("\n");
	        blank = true;
			for (int j = 0; j < m; j++) {
//				if(in[j]==null)
//					in[j]="";
				sb.append(in[j]+"\n");
				
			}
			
		}
        System.out.print(sb);
    }
	
}

UVa Problem ID 299 (Train Swapping)

UVa Problem ID 299 (Train Swapping)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

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());
        for (int i = 0; i 50)
        		System.exit(0);
        	int a[]=new int[L];
        	String s[]=br.readLine().split(" ");
        	for(int j=0;ja[j2])
					{
						int temp=a[j2];
						a[j2]=a[j];
						a[j]=temp;
						swapcount++;
					}
				}
			}
        	sb.append("Optimal train swapping takes "+swapcount+" swaps.\n");
        }
        System.out.print(sb);
    }
}

Advertisement