Saturday, 25 January 2014

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++];
		}
	}	
	

}

Previous Post
Next Post

0 comments:

Advertisement