/* fi表示第i行的最左最小元素的列小标,则有f0<f1<f2<...<fn-1
取数组的偶数行,组成新的子数组,递归求解最左最小元素的列下表,利用偶数项限定奇数项的范围,再求奇数项
*/
#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;
}