# # 队列

## # 练习题

### #LC862 - 和至少为K的最短子数组 (opens new window)

typedef long long ll;

class Solution {
public:
int shortestSubarray(vector<int>& A, int K) {
int n = A.size();
vector<ll> s(n + 1);
for (int i = 1; i <= n; ++i)
s[i] = s[i - 1] + A[i - 1];
deque<int> dq;
int ans = n + 1;
for (int i = 0; i <= n; ++i) {
while (!dq.empty() && s[dq.back()] >= s[i])
dq.pop_back();
while (!dq.empty() && s[dq.front()] + K <= s[i]) {
ans = min(ans, i - dq.front());
dq.pop_front();
}
dq.push_back(i);
}
return ans == n + 1 ? -1 : ans;
}
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

### #KS2019C2 - Circuit Board (opens new window)

#include <deque>
#include <iostream>
#include <stack>
#include <vector>

using namespace std;

int main() {
int t;
cin >> t;
for (int case_num = 1; case_num <= t; ++case_num) {
int r, c, k;
cin >> r >> c >> k;
cout << "Case #" << case_num << ": ";
vector<vector<int>> v(r + 1, vector<int>(c + 1));
for (int i = 1; i <= r; ++i)
for (int j = 1; j <= c; ++j)
cin >> v[i][j];
vector<vector<int>> f(r + 2, vector<int>(c + 1));
for (int i = 1; i <= r; ++i) {
deque<pair<int, int>> asc, desc;
int l = 1;
for (int j = 1; j <= c; ++j) {
while (!asc.empty() && asc.back().first >= v[i][j])
asc.pop_back();
while (!asc.empty() && v[i][j] - asc.front().first > k) {
l = max(l, asc.front().second + 1);
asc.pop_front();
}
asc.emplace_back(v[i][j], j);
while (!desc.empty() && desc.back().first <= v[i][j])
desc.pop_back();
while (!desc.empty() && desc.front().first - v[i][j] > k) {
l = max(l, desc.front().second + 1);
desc.pop_front();
}
desc.emplace_back(v[i][j], j);
f[i][j] = j - l + 1;
}
}
int ans = 0;
for (int j = 1; j <= c; ++j) {
stack<pair<int, int>> st;
for (int i = 1; i <= r + 1; ++i) {
int l = i;
while (!st.empty() && f[i][j] < st.top().first) {
ans = max(ans, st.top().first * (i - st.top().second));
l = st.top().second;
st.pop();
}
if (st.empty() || st.top().first < f[i][j])
st.emplace(f[i][j], l);
}
}
cout << ans << endl;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

### #BS - Bunnyhopping (opens new window)

#include "solution.hpp"
using namespace std;
const int INF = 0x3f3f3f3f;

class Solution {
public:
int solve(vector<int>& nums, int k) {
int n = nums.size();
vector<int> cost(n, INF);
cost[0] = nums[0];
deque<int> dq;
dq.push_back(0);
for (int i = 1; i < n; ++i) {
while (!dq.empty() && i - dq.front() > k)
dq.pop_front();
cost[i] = cost[dq.front()] + nums[i];
while (!dq.empty() && cost[dq.back()] >= cost[i])
dq.pop_back();
dq.push_back(i);
}
return cost[n - 1];
}
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

### #BS - Longest Equivalent Sublist After K Increments (opens new window)

#include "solution.hpp"
using namespace std;

class Solution {
public:
int solve(vector<int>& nums, int k) {
int ans = 0;
int n = nums.size();
int l = 0, sum = 0;
deque<int> q;
auto check = [&](int len, int s){
if (q.empty())
return true;
int need = nums[q.front()] * len - s;
return need <= k;
};
for (int r = 0; r < n; ++r) {
sum += nums[r];
while (!q.empty() && nums[q.back()] <= nums[r])
q.pop_back();
q.push_back(r);
while (!check(r - l + 1, sum)) {
sum -= nums[l];
l++;
while (!q.empty() && q.front() < l)
q.pop_front();
}
ans = max(ans, r - l + 1);
}
return ans;
}
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33