题目大意:
两个同样大小的数组,a,b。统计MAX(a[l,k]) < MIN(b[l,k])的总数,即是在某一段相同始末位置的子数组中,a的该子数组的最大值如果小于b的对应的子数组的最小值则计数1。
例:
输入:
3
1 2 3
3 3 3
输出
3
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct m_s findMax(vector<int> vec);
struct m_s findMin(vector<int> vec);
int cntT(vector<int> a, vector<int> b);
struct m_s {
int m = 0;
int index = 0;
};
int main() {
int n, temp, cnt = 0;
cin >> n;
vector<int> a, b;
char ch;
for (int i = 0; i < n; i++) {
cin >> temp;
a.push_back(temp);
}
cout << a.size() << endl;
for (int i = 0; i < n; i++) {
cin >> temp;
b.push_back(temp);
}
cnt = cntT(a, b);
cout << cnt << endl;
}
struct m_s findMax(vector<int> vec) {
m_s max{ INT_MIN,-1 };
for (int i = 0; i < vec.size(); i++) {
if (max.m < vec[i]) {
max.m = vec[i];
max.index = i;
}
}
return max;
}
struct m_s findMin(vector<int> vec) {
m_s min{ INT_MAX,-1 };
for (int i = 0; i < vec.size(); i++) {
if (min.m > vec[i]) {
min.m = vec[i];
min.index = i;
}
}
return min;
}
int cntT(vector<int> a, vector<int> b) {
if (a.empty() || b.empty()) { return 0; }
int cnt = 0;
int n = a.size();
vector<int> v11, v12;
for (int i = 0; i < n; i++) {
for (int j = i+1; j <= n; j++) {
v11.assign(a.begin() + i, a.begin() + j);
v12.assign(b.begin() + i, b.begin() + j);
m_s maxleft = findMax(v11);
m_s minright = findMin(v12);
//cout << "maxleft " << maxleft.m <<" " <<"minright " << minright.m << endl;
if (maxleft.m<minright.m) {
cnt++;
}
}
}
//cin.get();
//cin.get();
return cnt;
}