码迷,mamicode.com
首页 > 其他好文 > 详细

第4章 分治策略 monge阵列

时间:2015-07-31 21:42:39      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:

#include<iostream>
#include<vector>
using namespace std;
void findOddMin(vector<vector<int>> &a, int m, int n, vector<int> &r)
{
	int len = r.size();
	vector<int> tmp;
	for (int i = 0; i < m; ++i){
		int b = 0, e = n - 1;
		if (i % 2 == 0){
			if (i / 2>0) b = r[i / 2 - 1];
			if (i / 2 < len) e = r[i / 2];
			int k = b;
			for (int j = b + 1; j <= e; ++j)
				if (a[i][j] < a[i][k]) k = j;
			tmp.push_back(k);
		}
		else tmp.push_back(r[i / 2]);
	}
	r = tmp;
}
void findMin(vector<vector<int>> &a, int m, int n, vector<int> &r)
{
	if (m == 1){
		int k = 0;
		for (int i = 1; i < n; ++i)
			if (a[0][i] < a[0][k]) k = i;
		r.push_back(k);
		return;
	}
	vector<vector<int>> b;
	for (int i = 0; i < m; ++i)
		if (i % 2 == 1) b.push_back(a[i]);
	findMin(b, m / 2, n, r);
	findOddMin(a, m, n, r);
}

int main()
{
	//vector<vector<int>> a = { { 10, 13, 23 }, { 17, 16, 23 }, { 45, 32, 23 } };
	vector<vector<int>> a = { { 37, 23, 22, 32 }, { 21, 6, 5, 10 }, { 53, 34, 30, 31 }, { 32, 13, 9, 6 }, { 43, 21, 15, 8 } };
	vector<int> r;
	findMin(a, 5,4, r);
	for (auto i : r)
		cout << i << ‘ ‘;
	cout << endl;
}

  

第4章 分治策略 monge阵列

标签:

原文地址:http://www.cnblogs.com/jokoz/p/4693042.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!