2023.9.17模拟赛
T377848 花坛 (bed)
思路
显然会有一种贪心思路,就是对于如果满足了 ,那么区间越小就肯定白色越少,所以可以用一个指针来记录指针最小的时候可以满足 ,然后枚举左端点,计算右端点即可,然后考试时算错了空间,。显然这种做法有点无脑,所以可以将指针这个东西动态的维护,这样就不用存储了,然后稍微用脑后就发现其实就是双指针。(烦死了!!!我只是想偷个懒,结果痛失100分!!!)
时空复杂度
时间复杂度:
空间复杂度:
code
#include <iostream>
#include <algorithm>
using namespace std;
const int MaxN = 1e7 + 10;
short a[MaxN];
int n, x, y, ans = 1e9;
int cnt[3];
int main() {
ios::sync_with_stdio(0), cin.tie(0);
//freopen("bed.in", "r", stdin);
//freopen("bed.out", "w", stdout);
cin >> n >> x >> y;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1, j = 1; i <= n && j <= n; i++) {
for (j; j <= n && (cnt[1] < x || cnt[2] < y); j++) {
cnt[a[j]]++;
}
if (j <= n) {
ans = min(ans, cnt[0]);
}
cnt[a[i]]--;
}
cout << ans << endl;
return 0;
}T377851 小花 (flower)
思路
这个题意就是说要能维护成一个 ,然后就可以想到可以记录有多少个转折点,然后就没了,不过要注意就是如果两个相邻的值不能相同。
时空复杂度
时间复杂度:
空间复杂度:
code
#include <iostream>
using namespace std;
const int MaxN = 3e5 + 10;
int a[MaxN], n, t;
int C(int x, int y) {
return (x < y ? 1 : x > y ? 2 : 3);
}
void Work() {
int s = 1, op = 1, cnt = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
if (C(a[i - 1], a[i]) == 3 || (C(a[i - 1], a[i]) != op && s >= i - 1)) {
cout << "NIE" << '\n';
return;
} else if (C(a[i - 1], a[i]) != op) {
op = C(a[i - 1], a[i]), cnt++, s = i;
}
}
if (cnt == 3) cout << "TAK" << '\n';
else cout << "NIE" << '\n';
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
for (cin >> t >> n; t; t--) {
Work();
}
return 0;
}T377852 下棋 (chess)
题意
首先考虑第一问, 即可,对于第二问,如果可以转移过来的话,就统计方案数即可,同样是 ,所以可以设计状态为 表示到格子 时的最优值和方案数。
时空复杂度
时间复杂度:
空间复杂度:
code
#include <iostream>
using namespace std;
using ll = long long;
const int MaxN = 2010, mod = 1000000007;
int a[MaxN][MaxN], n, m;
ll dp[MaxN][MaxN], cnt[MaxN][MaxN];
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m, cnt[1][1] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j], dp[i][j] = -1e9;
}
}
dp[1][1] = a[1][1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (dp[i + 1][j] <= dp[i][j] + a[i + 1][j]) {
cnt[i + 1][j] = (cnt[i + 1][j] * (dp[i + 1][j] == dp[i][j] + a[i + 1][j]) + cnt[i][j]) % mod, dp[i + 1][j] = dp[i][j] + a[i + 1][j];
}
if (dp[i][j + 1] <= dp[i][j] + a[i][j + 1]) {
cnt[i][j + 1] = (cnt[i][j + 1] * (dp[i][j + 1] == dp[i][j] + a[i][j + 1]) + cnt[i][j]) % mod, dp[i][j + 1] = dp[i][j] + a[i][j + 1];
}
}
}
cout << dp[n][m] << endl << cnt[n][m] << endl;
return 0;
}