题目
思路:滑动窗口
- 右指针
right
,每轮循环往前走一步。for(int right = 0; right < len_size; ++right){}
- 如果出现不同时符合两个条件(1.2.)的数组,那么左边的指针需要不断往右走,直到满足条件才可以
- 为什么滑动窗口是有效的?因为滑动窗口遍历了所有满足条件的子数组
代码:
```cppinclude
include
include
using namespace std;
int main() { int n, m, k; cin >> n >> m >> k;
// input array
vector<int> nums;
for (int i = 0; i < n; ++i) {
int input = 0;
cin >> input;
nums.push_back(input);
}
int cnt_nega = 0, max_comb = 0;
for (int right = 0, left = 0; right < n; ++right) {
int right_num = nums[right], left_num = nums[left];
if (right_num < 0) {cnt_nega++;}
while (left < right && right_num + left_num > k && cnt_nega > m) {
if (left_num < 0) {cnt_nega--;}
left++;
left_num = nums[left];
}
if (left != right) {max_comb = max(max_comb, left_num + right_num);}
}
cout << "max_comb is: " << max_comb << endl;
return 0;
}
```