【微软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;
	}

}

【微软2014实习生及秋令营技术类职位在线测试】题目3 : Reduce inversion count,布布扣,bubuko.com

时间: 2024-10-25 03:14:28

【微软2014实习生及秋令营技术类职位在线测试】题目3 : Reduce inversion count的相关文章

【微软2014实习生及秋令营技术类职位在线測试】题目2 : K-th string

时间限制:10000ms 单点时限:1000ms 内存限制:256MB Description Consider a string set that each of them consists of {0, 1} only. All strings in the set have the same number of 0s and 1s. Write a program to find and output the K-th string according to the dictionary

微软2014实习生及校招秋令营技术类职位,在线编程题目及解答。

题目1 : String reorder 时间限制:10000ms 单点时限:1000ms 内存限制:256MB Description For this question, your program is required to process an input string containing only ASCII characters between '0' and '9', or between 'a' and 'z' (including '0', '9', 'a', 'z'). Y

微软2014实习生在线测试之K-th string

问题描述: Time Limit: 10000msCase Time Limit: 1000msMemory Limit: 256MB Description Consider a string set that each of them consists of {0, 1} only. All strings in the set have the same number of 0s and 1s. Write a program to find and output the K-th str

微软2014编程之美初赛第一场——题目2 : 树

[来源] 题目2 : 树 [分析] 依据输入情况建立起树的模型.树的表示是一个表明父亲节点的数组.核心算法有两个: 计算某一节点的深度.用循环实现,一直向上找父亲节点,直到找到根节点.计算循环的次数即为深度. 计算某一节点的全部子节点.用递归实现. 本题在实现上节点的命名从0至N-1,与题目描写叙述不同. [代码] #include <iostream> #include <vector> using namespace std; vector<int> childre

腾讯2014实习生笔试题--德梅齐里亚克砝码问题

问题 珠宝商甲需要去鉴定一批41克以下的宝石(可能是41克以下不包括41克的任意重量),他只能携带一个天平和四个砝码去称重,请问他会携带那些重量的砝码?-----2014腾讯暑期实习生附加题第一题 解答: 首先给出问题的答案,聪明的人看到答案的形式就能猜到其中的规律:1,1*2+1=3,(1+3)*2+1=9,(1+3+9)*2+1=27. 德梅齐里亚克砝码问题问题描述: 一位商人有一个40磅的砝码,由于跌落在地而碎成4块.后来,称得每块碎片的重量都是整磅数,而且可以用这4块来称从1至40磅之间

微软2014年12月安全补丁 提醒及时修复

2014年最后一批微软补丁来了,北京时间12月10日凌晨,微软准时发布12月安全公告https://technet.microsoft.com/library/security/ms14-dec  目前服务器安全狗也已第一时间推送安全更新,为避免漏洞被利用遭到攻击,请及时修复服务器安全狗提示给您的系统高危漏洞! 本次安全公告带来7枚补丁,共涉及3个"严重"级别和4个"重要"级别,修复 Windows 系统.IE 浏览器以及 Office 组件中的24处漏洞.这7枚补

微软2014 算法笔试题目1及答案

题目1 : Beautiful String 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 We say a string is beautiful if it has the equal amount of 3 or more continuous letters (in increasing order.) Here are some example of valid beautiful strings: "abc", "cde"

微软2014编程之美初赛第一场——题目3 : 活动中心

[来源] 题目3 : 活动中心 [分析] 本题採用的是三分法. 输入的一组点中找出左右边界.作为起始边界. while(右边界-左边界<精度){ 将左右边界构成的线段均匀分成3段,推断切割点的距离关系,抹去距离大的一段.更新左右边界. } 输出左(右)边界 [代码] #include <iostream> #include <vector> #include <cmath> #include <iomanip> using namespace std;

微软2014年技术岗位在线笔试题

DescriptionFor this question, your program is required to process an input string containing only ASCII characters between ‘0’ and ‘9’, or between ‘a’ and ‘z’ (including ‘0’, ‘9’, ‘a’, ‘z’). Your program should reorder and split all input string char