题目1:1089. 烽火传递
分析:
分析简单。主要注意初始化问题:将dp[0]放入一开始的队列中。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
const ll mod = 1e9 + 7;
const int maxn = 1e4 + 10;
const int inf = 0x3f3f3f3f;
int n, m;
int v[maxn], q[maxn];
int dp[maxn];
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i++) cin >> v[i];
int l = 0, r = 0;
for(int i = 1; i <= n; i++)
{
while(l <= r && q[l] < i - m) l++;
dp[i] = v[i] + dp[q[l]];
while(l <= r && dp[i] <= dp[q[r]]) r--;
q[++r] = dp[i];
}
int res = inf;
for(int i = n - m + 1; i <= n; i++)
res = min(res, dp[i]);
cout << res << endl;
}
题目2:1087. 修剪草坪
分析:
注意初始化的问题:队列一开始不为空,含有下标0,用来后续的转移,不可省略。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
const ll mod = 1e9 + 7;
const int maxn = 1e6 + 10;
const int inf = 0x3f3f3f3f;
ll sum[maxn], dp[maxn];
int q[maxn];
int n, k;
ll calc(int x)
{
return !x ? 0 : dp[x - 1] - sum[x];
}
int main()
{
cin >> n >> k;
for(int i = 1; i <= n; i++)
cin >> sum[i], sum[i] += sum[i - 1];
int head = 1, tail = 1;
for(int i = 1; i <= n; i++)
{
while(head <= tail && q[head] < i - k) head++;
dp[i] = max(dp[i - 1], sum[i] + calc(q[head]));
while(head <= tail && calc(i) >= calc(q[tail])) tail--;
q[++tail] = i;
}
cout << dp[n] << endl;
}
题目3:1090. 绿色通道
分析:
这个题意是在不超过t时间内间隔的最小值。考虑二分答案+dp。
对于当前极限间隔,我们通过dp求出最小的花费时间,如果小于t则返回true,否则返回false
- 复习二分答案代码,见注释。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
const ll mod = 1e9 + 7;
const int maxn = 5e4 + 10;
const int inf = 0x3f3f3f3f;
int n, t;
int v[maxn], q[maxn];
int dp[maxn];
bool judge(int limit)
{
int l = 0, r = 0;
q[0] = 0;
for(int i = 1; i <= n; i++)
{
while(l <= r && q[l] < i - limit - 1) l++;
dp[i] = dp[q[l]] + v[i];
while(l <= r && dp[i] <= dp[q[r]]) r--;
q[++r] = i;
}
for(int i = n - limit; i <= n; i++)
if(dp[i] <= t)
return true;
return false;
}
int main()
{
cin >> n >> t;
for(int i = 1; i <= n; i++) cin >> v[i];
int l = 0, r = n;
while(l <= r) // <=
{
int mid = l + r >> 1;
if(judge(mid)) r = mid - 1; // - 1
else l = mid + 1;// + 1
}
cout << l << endl;//求最小值返回l,求最大值返回r
}
题目4:1091. 理想的正方形
分析:
这个题之前牛客做过,应该可以用二维滑动窗口求。这里y总用的单调队列。
先按行求出[i - k +1, i]的min,max值,存起来。然后再对刚求出的两个矩阵按列使用单调队列求最值。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
const ll mod = 1e9 + 7;
const int maxn = 1e3 + 10;
const int inf = 0x3f3f3f3f;
int n, m, k;
int g[maxn][maxn], lmin[maxn][maxn], lmax[maxn][maxn];
int cmin[maxn], cmax[maxn], buf[maxn];
void getmin(int a[], int b[], int num)
{
int q[maxn];
int l = 1, r = 0;
for(int i = 1; i <= num; i++)
{
while(l <= r && q[l] <= i - k) l++;
while(l <= r && a[i] <= a[q[r]]) r--;
q[++r] = i;
b[i] = a[q[l]];
}
}
void getmax(int a[], int b[], int num)
{
int q[maxn];
int l = 1, r = 0;
for(int i = 1; i <= num; i++)
{
while(l <= r && q[l] <= i - k) l++;
while(l <= r && a[i] >= a[q[r]]) r--;
q[++r] = i;
b[i] = a[q[l]];
}
}
int main()
{
cin >> n >> m >> k;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
scanf("%d", &g[i][j]);
for(int i = 1; i <= n; i++)
{
getmin(g[i], lmin[i], m);
getmax(g[i], lmax[i], m);
}
int minn = inf;
for(int j = k; j <= m; j++)
{
for(int i = 1; i <= n; i++)
buf[i] = lmin[i][j];
getmin(buf, cmin, n);
for(int i = 1; i <= n; i++)
buf[i] = lmax[i][j];
getmax(buf, cmax, n);
for(int i = k; i <= n; i++)
minn = min(minn, cmax[i] - cmin[i]);
}
cout << minn << endl;
}