微软2014实习生及秋令营技术类职位在线测试_题目3 : Reduce inversion count
来源:互联网 发布:淘宝出版物许可证造假 编辑:程序博客网 时间:2024/06/15 04:12
- 样例输入
3,1,21,2,3,4,5
- 样例输出
10
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.
作者提供的源代码,提交结果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
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
}
- 【微软2014实习生及秋令营技术类职位在线测试】题目3 : Reduce inversion count
- 微软2014实习生及秋令营技术类职位在线测试_题目3 : Reduce inversion count
- 微软2014实习生及秋令营技术类职位在线测试——Reduce inversion count
- 微软2014实习生及秋令营技术类职位在线测试: Reduce inversion count
- 微软2014实习生及秋令营技术类职位在线测试题目1+题目2
- 微软2014实习生及秋令营技术类职位在线测试_题目4 : Most Frequent Logs
- 微软2014实习生及秋令营技术类职位在线测试-题目1 : String reorder
- 微软2014实习生及秋令营技术类职位在线测试-题目2 : K-th string
- 微软2014实习生及秋令营技术类职位在线测试-题目1 : String reorder
- 微软2014实习生及秋令营技术类职位在线测试 题目1 : String reorder
- 微软2014实习生及秋令营技术类职位在线测试(题目1 : String reorder)
- 【微软2014实习生及秋令营技术类职位在线测试】题目1 : String reorder
- 【微软2014实习生及秋令营技术类职位在线测试】题目2 : K-th string
- 【微软2014实习生及秋令营技术类职位在线测试】题目1 : String reorder
- 微软2014实习生及秋令营技术类职位在线测试第3题
- 微软2014实习生及校招秋令营技术类职位,在线编程题目及解答。
- 微软2014实习生及秋令营技术类职位在线测试 原创解答 题目1 : String reorder
- 微软2014实习生及校招秋令营技术类职位,在线编程题目
- 项目一:kinect的人脸识别(水)
- 《C++ 并发编程》- 第1章 你好,C++的并发世界
- B树、B-树、B+树、B*树
- poj 1318 Word Amalgamation
- Activity生命周期
- 微软2014实习生及秋令营技术类职位在线测试_题目3 : Reduce inversion count
- 异或的性质及运用
- 面试经典(12)--射击打靶问题
- Asp.Net数据导出Excel报表
- 时间复杂度
- cocos2d-x回调中需注意陷阱
- a=10,b=15,在不用第三方变题的前提下,把a,b的值互换?
- M个1和N个0的排列
- Linux中内存buffer和cache的区别