Stanford - Algorithms: Design and Analysis, Part 1 - Week 1 Assignment: number of inversions

题目要求如下:

Download the text file here. (Right click and save link as)

This file contains all of the 100,000 integers between 1 and 100,000 (inclusive) in some order, with no integer repeated.

Your task is to compute the number of inversions in the file given, where the  ith  row of the file indicates the  ith  entry of an array.
Because of the large size of this array, you should implement the fast divide-and-conquer algorithm covered in the video lectures. The numeric answer for the given input file should be typed in the space below.
So if your answer is 1198233847, then just type 1198233847 in the space provided without any space / commas / any other punctuation marks. You can make up to 5 attempts, and we'll use the best one for grading.
(We do not require you to submit your code, so feel free to use any programming language you want --- just type the final numeric answer in the following space.)

[TIP: before submitting, first test the correctness of your program on some small test files or your own devising. Then post your best test cases to the discussion forums to help your fellow students!]

介绍下number of inversion:

[1,3,5,2,4,6]一共有三组inversion: [3,2],[5,2],[5,4]

本题最直观的算法肯定是完全遍历,这样时间复杂度会是O(N^2),如果用本节课将的divide and conquer方法,会降低时间复杂度:O(NlogN)

具体思路就是[1,3,5,2,4,6]里面有多少inversion其实就等于[1,3,5]的inversion + [2,4,6]的inversion + 跨越[1,3,5]和[2,4,6]的inversion,然后再通过recursive不断的细化。。

具体的算法实现就是在merge sort的基础上加入了计算inversion的功能:

当[1,3,5]和[2,4,6]merge的时候,过程是1 < 2,则原数组[1,,,,]

之后3>2,则把2放到原数组[1,2,,,]这样的话就把2放到了3,5前面,这就是两次inversion,

之后3<4,原数组[1,2,3,,]

之后5>4,把4放到原数组[1,2,3,4,,]这样4放到了5前面,又一次inversion,

之后两步没有inversion

详细代码如下:

# include <iostream>
# include <fstream>
# include <vector>

using namespace std;

/* prototype */
long sort(vector<int>&, int, int);
long merge(vector<int>&, int, int, int);

int main(int argc, char** argv) {

	/* copy the file to inversion vector */
	vector<int> inversion;
	ifstream infile;
	infile.open("test5.txt", ios::in);
	int temp;
	long cnt = 0;
	while (infile >> temp) {
		inversion.push_back(temp);
	}
	infile.close();

	cnt = sort(inversion, 0, inversion.size()-1);
	cout << "number of inversions: " << cnt << endl;

	return 0;
}

long sort(vector<int>& vec, int low, int high) {
	vector<int> aux(vec);
	if (high <= low)
		return 0;
	int mid = low + (high - low) / 2;
	long x = sort(vec, low, mid);
	long y = sort(vec, mid+1, high);
	long z = merge(vec, low, mid, high);
	return (x + y + z);
}

long merge(vector<int>& vec, int low, int mid, int high) {
	long cnt = 0;
	int i = low;
	int j = mid + 1;
	vector<int> aux(vec);
	for (int k = low; k <= high; ++k) {
		if (i > mid)
			vec[k] = aux[j++];
		else if (j > high)
			vec[k] = aux[i++];
		else if (aux[i] < aux[j])
			vec[k] = aux[i++];
		else {
			vec[k] = aux[j++];
			cnt += (mid - i + 1);
		}
	}
	return cnt;
} 

有一个问题就是,答案虽然对,但是我看论坛里别人讨论的运行速度要比我写的快很多,不知道为什么,我的mergesort应该是对的。如果各位发现我里面哪出了问题,希望不吝赐教,谢谢!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值