https://leetcode.com/problems/minimum-swaps-to-make-sequences-increasing/description/
dp[i][0]表示 在第i个位置 没有换 从0...i最小交换次数
dp[i][1]表示 在第i个位置,交换了 从0...i最小交换次数
class Solution {
public:
int minSwap(vector<int>& aa, vector<int>& bb) {
int dp[aa.size()+1][2];
vector<int> a, b;
a.push_back(-1);
b.push_back(-1);
for (int i = 0; i < aa.size(); i++) {
a.push_back( aa[i] );
b.push_back( bb[i] );
}
// A.push_front( -1 );
// B.push_front( -1 );
dp[0][0] = 0;
dp[0][1] = INT_MAX/3;
for (int i = 1; i < a.size(); i++) {
dp[i][0] = dp[i][1] = INT_MAX/3;
if (a[i] > a[i-1] && b[i] > b[i-1]) {
dp[i][0] = min( dp[i][0], dp[i-1][0] );
}
if (a[i] > b[i-1] && b[i] > a[i-1]) {
dp[i][0] = min( dp[i][0], dp[i-1][1] );
}
if (b[i] > a[i-1] && a[i] > b[i-1] ) {
dp[i][1] = min( dp[i][1], dp[i-1][0] + 1 );
}
if (b[i] > b[i-1] && a[i] > a[i-1]) {
dp[i][1] = min(dp[i][1], dp[i-1][1]+ 1) ;
}
// cout << "i:" << i << " [0]:" << dp[i][0] << " [1]:" << dp[i][1] << endl;
}
return min(dp[a.size()-1][0], dp[a.size()-1][1]);
}
};