微软2014年4月 实习生招聘机试题 3.Reduce inversion count

Time Limit: 10000ms
Case Time Limit: 1000ms
Memory Limit: 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]) 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.


Sample In

3,1,2
1,2,3,4,5

Sample Out

1
0

 

 

个人给出的一个解,找逆序用归并排序,找逆序最大减少数用暴力计算法:

#include <fstream>
#include <iostream>
#include <vector>
#include <string>
using namespace std;

#define USE_IFSTREAM 1

int gCount = 0; //统计逆序个数

template<class T,class Iterator>
int merge(Iterator begin, Iterator mid, Iterator end)
{
	Iterator iL = begin;
	Iterator iR = mid;

	int count = distance(begin, end);
	vector<T> v(count);
	vector<T>::iterator it = v.begin();

	while(iL != mid && iR != end)
	{
		if(*iL <= * iR)
		{
			*it++ = *iL++;
		}
		else
		{
			gCount += distance(iL, mid);
			*it++ = *iR++;
		}
	}

	if(iL == mid) copy(iR, end, it);
	if(iR == end) copy(iL, mid, it);

	copy(v.begin(), v.end(), begin);

	return 0;

}

template<class T,class Iterator>
int mergeSort(Iterator begin, Iterator end)
{
	int count, step;
	count = distance(begin, end);

	if(count <= 1)
	{
		return 0;
	}

	step = count / 2;

	mergeSort<T>(begin, begin + step);
	mergeSort<T>(begin + step, end);

	merge<T>(begin, begin + step, end);

	return 0;

}
/*寻找最大减少逆序数*/
template<class T,class Iterator>
int findMaxDecInv(Iterator begin, Iterator end)
{
	int sumTmp =0;
	int maxDecInv=0;
#if 0
	Iterator i=begin ;
	iterator j=begin ;
	iterator k=begin ;
#endif
	Iterator i,j,k;
	for(i= begin;i<end;i++){
		for(j=i+1;j<end;j++){
			if(*j<*i){
				sumTmp = 1;
				for(k=i+1;k<j;k++)
					if((*k>*j)&&(*k<*i))
						sumTmp +=2;
				if(sumTmp > maxDecInv)
					maxDecInv = sumTmp;
			}
		}
	}
	return maxDecInv;

}



int main(){

	string str;
	int length=0;
	int i=0;
	char ch =0;
	int tmp = 0;
	int totalNum=0;
	int maxDecInv =0;
	//int* ptr ; 
	vector<int> vec(100);//这里先开辟10000,还会自动扩展
	vector<int> vecBackUp(100);
	vector<int>::iterator ite = vec.begin();
	
#ifdef USE_IFSTREAM
	ifstream ifs=ifstream("testCase.txt");
#endif

	while(1){
		//str.clear();
		gCount = 0;
		vec.assign(100,0);
		ite=vec.begin();
		totalNum = 0;
		i = 0;
#ifndef USE_IFSTREAM
		getline(cin,str);
#else  
		if(ifs.eof())
			break;
		getline(ifs,str);

#endif
		length = str.length();
		//ptr = new int[length];
		while(i<length){
			tmp = 0; 
			while((i<length)&&((ch=str[i++]) != ',')){
				tmp = (tmp*10)+(ch-'0');
			}
			*ite++ = tmp;
			totalNum++;

		}
		vecBackUp = vec;

		mergeSort<int>(vec.begin(),ite);

		if(gCount==0){
			cout<<0<<endl;
		}
		else{
			int dis = distance(vec.begin(),ite);
			vector<int>::iterator begin= vecBackUp.begin();
			maxDecInv = findMaxDecInv<int>(begin,begin+dis);
			cout<<(gCount-maxDecInv)<<endl;
		}

		
	}
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值