跳到正文

个人记录

2023.9.17模拟赛

q0y1约 6 分钟
原文
文章信息与版本

文章信息

发布
2023年9月17日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 6 分钟

版本

  1. v1当前

2023.9.17模拟赛

T377848 花坛 (bed)

思路

显然会有一种贪心思路,就是对于如果满足了 A、BA、B,那么区间越小就肯定白色越少,所以可以用一个指针来记录指针最小的时候可以满足 A、BA、B,然后枚举左端点,计算右端点即可,然后考试时算错了空间,MLEMLE。显然这种做法有点无脑,所以可以将指针这个东西动态的维护,这样就不用存储了,然后稍微用脑后就发现其实就是双指针。(烦死了!!!我只是想偷个懒,结果痛失100分!!!)

时空复杂度

时间复杂度:O(n)O(n)

空间复杂度:O(n)O(n)

code

C++
#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)

思路

这个题意就是说要能维护成一个 MM,然后就可以想到可以记录有多少个转折点,然后就没了,不过要注意就是如果两个相邻的值不能相同。

时空复杂度

时间复杂度:O(n)O(n)

空间复杂度:O(n)O(n)

code

C++
#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)

题意

首先考虑第一问,dpdp 即可,对于第二问,如果可以转移过来的话,就统计方案数即可,同样是 dpdp,所以可以设计状态为 dp[i][j]dp[i][j] 表示到格子 (i,j)(i,j) 时的最优值和方案数。

时空复杂度

时间复杂度:O(nm)O(nm)

空间复杂度:O(nm)O(nm)

code

C++
#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;
}

T377853 灯笼 (lantern)

原文