Reduce inversion count
来源:互联网 发布:剑三周边淘宝商城 编辑:程序博客网 时间:2024/05/17 21:44
- 样例输入
3,1,21,2,3,4,5
- 样例输出
10
- 我的java代码。。。求大神不吝赐教。
- package com.summer.intern;
import java.util.Scanner;
public class Main {
public static int getInversionCount(int data[])
{
int count=0;
for(int i=0;i<data.length;i++){
for(int j=i+1;j<data.length;j++)
{
if(data[i]>data[j]) count++;
}
}
return count;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (input.hasNextLine()) {
String line = input.nextLine();
String num[] = line.split(",");
int data[]=null;
data=new int [num.length];
for(int i=0;i<num.length;i++)
{
data[i]=Integer.parseInt(num[i]);
}
int orial_inversion_count=getInversionCount(data);
int temp_data[]=data.clone();
for(int i=0;i<data.length;i++){
for(int j=i+1;j<data.length;j++){
int temp=temp_data[i];
temp_data[i]=temp_data[j];
temp_data[j]=temp;
int now_inversion_count=getInversionCount(temp_data);
if(orial_inversion_count>now_inversion_count)
{
orial_inversion_count=now_inversion_count;
}
temp_data=data.clone();
}
}
System.out.println(orial_inversion_count);
}
}
}
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.
- Reduce inversion count
- 微软2014实习 Reduce inversion count 【next_permutation】
- count inversion
- 微软2014实习生及秋令营技术类职位在线测试——Reduce inversion count
- 微软2014实习生及秋令营技术类职位在线测试: Reduce inversion count
- 【微软2014实习生及秋令营技术类职位在线测试】题目3 : Reduce inversion count
- 微软2014年4月 实习生招聘机试题 3.Reduce inversion count
- [codility]Array-inversion-count
- decreases inversion count
- 微软2014实习生及秋令营技术类职位在线测试_题目3 : Reduce inversion count
- Word Count (Map Reduce)
- Word Count (Map Reduce)
- Merge Sort and count inversion
- Inversion count implemented with merge sort
- Swift 函数Count,Filter,Map,Reduce
- Spark算子[07]:reduce,reduceByKey,count,countByKey
- 从word count分析Map Reduce / Map Reduce的word count讲解
- 【算法与数据结构】一道检测inversion count的初级算法
- eclipse xml文件中按alt+/没有提示信息
- JAVA中的不可变类
- 第八周项目一——运算符重载(运用友元函数)
- Android网络请求库android-async-http使用
- 快速学习正则表达式
- Reduce inversion count
- 呵呵
- 手把手学习——ant脚本打包APK
- 1029 遍历问题
- android 之 .9图制作
- Android设备HeartBleed漏洞影响分析
- 重新创建activity
- STM32输入上拉下拉 寄存器怎么设置…
- Android:hierarchy viewer unable to debug device 问题