微软2014实习生及秋令营技术类职位在线测试_题目3 : Reduce inversion count

来源:互联网 发布:淘宝出版物许可证造假 编辑:程序博客网 时间:2024/06/15 04:12

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,21,2,3,4,5
样例输出
10

作者提供的源代码,提交结果RE(运行错误!)大哭,想跟大家分享一下,希望哪位大虾能够帮我解决问题!

//source here

import java.util.Scanner;public class Main {static long total = 0;static int temp[] = new int[100];  //额外的辅助数组   static int count;       public static void main(String[] args) {    long n;       //注意声明为长整型    String arrayStr[];    int arrayInt[] = null;    int array2[][] = new int[100][];    int g = 0;    int i=0,j=0,k=0;    Boolean flag = false;        Scanner in = new Scanner(System.in);//不会检测输入越界的问题,但是注意整型上限,最好将变量定义为long长整型        do{        String line = in.nextLine();        if( line==null || "".equals(line.toString())){        flag = true;        }else{            arrayStr = line.toString().split(",");            arrayInt = new int[arrayStr.length];            for(i=0;i<arrayStr.length;i++){            arrayInt[i] = Integer.parseInt(arrayStr[i]);            }            array2[g++] = arrayInt;        }        }while(!flag);                        for(i=0;i<g;i++){        count = 0;            MergeSort(array2[i], 0, array2[i].length-1);            System.out.println(count);        }    }    static void Merge(int []array,int first,int med,int last)  {    int i=first,j=med+1;       int cur=0;       while (i<=med&&j<=last)       {           if (array[i]<array[j])           {               temp[cur++]=array[i++];           }           else           {               temp[cur++]=array[j++];               count+=med-i+1;  //核心代码,逆序数增加           }    }       while (i<=med)       {           temp[cur++]=array[i++];       }       while (j<=last)       {           temp[cur++]=array[j++];       }       for (int m=0;m<cur;m++)    {           array[first++]=temp[m++];       }}static void MergeSort(int []array,int first,int last)   {       if (first==last)       {           return ;       }       int med=first+(last-first)/2;       MergeSort(array,first,med);       MergeSort(array,med+1,last);       Merge(array,first,med,last);  } }

时间限制: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
}





0 0
原创粉丝点击