zoukankan      html  css  js  c++  java
  • 【微软2014实习生及秋令营技术类职位在线測试】题目3 : Reduce inversion count

    时间限制:10000ms
    单点时限:1000ms
    内存限制:256MB

    Description

    Find a pair in an integer array that swapping them would maximally decrease the inversion count of the array. If such a pair exists, return the new inversion count; otherwise returns the original inversion count.

    Definition of Inversion: Let (A[0], A[1] ... A[n], n <= 50) be a sequence of n numbers. If i < j and A[i] > A[j], then the pair (i, j) is called inversion of A.

    Example:
    Count(Inversion({3, 1, 2})) = Count({3, 1}, {3, 2}) = 2
    InversionCountOfSwap({3, 1, 2})=>
    {
     InversionCount({1, 3, 2}) = 1 <-- swapping 1 with 3, decreases inversion count by 1
     InversionCount({2, 1, 3}) = 1 <-- swapping 2 with 3, decreases inversion count by 1
     InversionCount({3, 2, 1}) = 3 <-- swapping 1 with 2 , increases inversion count by 1
    }


    Input

    Input consists of multiple cases, one case per line.Each case consists of a sequence of integers separated by comma.

    Output

    For each case, print exactly one line with the new inversion count or the original inversion count if it cannot be reduced.


    例子输入
    3,1,2
    1,2,3,4,5
    例子输出
    1
    0

    思路。此题採用的是暴力法,只是改进的一个地方是计算InversionCount的算法,採用的是合并排序,时间复杂度是O(nlogn)

    import java.util.Scanner;
    
    
    public class Main {
    
    	static int InversionCount ;
    	
    	public static void main(String[] args) 
    	{
    		int T,t;
    		Scanner jin = new Scanner(System.in);
    		while (jin.hasNext()) {
    			String str = jin.next();
    			String[] argstr = str.split(",");
    			int[] num = new int[argstr.length];
    			int[] tmp = new int[argstr.length];
    			for (int i = 0; i < num.length; i++) {
    				num[i] = Integer.parseInt(argstr[i]);
    				tmp[i] = Integer.parseInt(argstr[i]);
    			}
    			InversionCount = 0;
    			MergeSort(tmp, 0, tmp.length-1);
    			int ret = InversionCount;
    			for (int i = 0; i < num.length; i++)
    				tmp[i] = num[i];
    			for (int i = 0; i < num.length-1; i++) {
    				for (int j = i+1; j < num.length; j++) {
    					if (num[i] > num[j]) {
    						int tmpnum = num[i];
    						num[i] = num[j];
    						num[j] = tmpnum;
    						InversionCount = 0;
    						MergeSort(num, 0, num.length-1);
    						ret = Math.min(ret, InversionCount);
    						for (int k = 0; k < num.length; k++)
    							num[k] = tmp[k];
    					}
    				}
    			}
    			System.out.println(ret);
    		}
    	}
    	
    	public static void MergeSort(int[] array, int lhs, int rhs) {
    		if (lhs < rhs) {
    			int mid = lhs + ((rhs - lhs)>>1);
    			MergeSort(array, lhs, mid);
    			MergeSort(array, mid+1, rhs);
    			Merge(array, lhs, mid, rhs);
    		}
    	}
    	public static void Merge(int[] array, int lhs, int mid, int rhs) {
    		int[] tmp = new int[rhs-lhs+1];
    		int i = lhs, j = mid+1;
    		int k = 0;
    		while(i <= mid && j <= rhs)
    		{
    			if (array[i] > array[j]) {
    				InversionCount += mid-i+1;
    				tmp[k++] = array[j++];
    			}
    			else {
    				tmp[k++] = array[i++];
    			}
    		}
    		while(i <= mid)
    		{
    			tmp[k++] = array[i++];
    		}
    		while(j <= rhs)
    		{
    			tmp[k++] = array[j++];
    		}
    		for (i = 0; i < k; i++) {
    			array[i+lhs] = tmp[i];
    		}
    		tmp = null;
    	}
    	
    }
    
    

  • 相关阅读:
    《Scrum实战》第4次课【全职的Scrum Master】作业汇总
    回顾Scrum学习:《Scrum实战》第4次课【全职的Scrum Master】作业
    孙式无极桩站桩要领--林泰年
    [Android Tips] 29. 如何判断当前编译的是哪个 Flavor ?
    [Jenkins] 解决 Gradle 编译包含 SVG Drawable 出现异常
    [Android Tips] 28. 如何指定运行特定的 Android Instrumentation Test
    [Gradle] 给已存在的 task 添加依赖
    [Gradle] 针对不同的项目类型应用不同的 findbugs 配置
    [Android Tips] 27. 检查 APK 是否可调试
    [Gradle] 如何强制 Gradle 重新下载项目的依赖库
  • 原文地址:https://www.cnblogs.com/jzssuanfa/p/6970620.html
Copyright © 2011-2022 走看看