跳到正文

个人记录

题解

whyxiexieni约 19 分钟
原文
文章信息与版本

文章信息

收录
2026年4月28日
更新
2026年5月11日
篇幅
约 19 分钟

版本

  1. v2当前
  2. v1

atcoder abc249 - f 题解

题意

一开始有一个整数 xx,有 NN 次操作,第 ii 次操作有两个整数 tit_i 和 yiy_i,如果 ti=1t_i = 1,将 xx 赋值为 yiy_i,否则将 xx 加上 yiy_i。可以跳过不超过 KK 次操作,问 xx 的最大值是多少。

思路

当 ti=1t_i = 1 时,将 xx 赋值为 yiy_i,代表着在此次操作之前的操作不需要管,只需要管这次操作以及后面的操作。于是我们从后往前遍历,假设此次 ti=1t_i = 1 的操作是最后一次,并将以后的赋值操作全部跳过。不跳过的操作22的旧集合一定属于新集合,因为如果多了此次操作为操作22,要么放入跳过的集合中,要么放入不跳过的集合中,导致不跳过的集合中的元素数量不会减少,加入的新元素不会影响旧元素。于是我们用一个优先队列维护需要跳过的操作22,如果此时需要跳过的操作次数大于 KK,则将优先队列中的最大的元素放入不跳过的集合中,在每次遇到操作11是统计答案。

标程

C++
#include<bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 2e5 + 5;
const ll INF = 1e18;

int n, k, t[N], y[N];
ll sum, ans = -INF;
priority_queue<int> q;

int main(){
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n >> k;
  for(int i = 1; i <= n; i++){
    cin >> t[i] >> y[i];
  }
  for(int i = n; k >= 0 && i >= 0; i--){
    if(t[i] == 2){
      if(y[i] >= 0){
        sum += y[i];
      } else{
        q.push(y[i]);
      }
    } else{
      while(q.size() > k){
        sum += q.top();
        q.pop();
      }
      k--;
      ans = max(ans, sum + y[i]);
    }
  }
  cout << ans;
  return 0;
}

atcoder abc248 - f题解

题意

给定整数 NN 和质数 PP,共有 NN 列点的图。 1≤i<N1 \le i < N,对于每个 ii,找到移除 ii 条边后图依旧连通的数量,对答案取模 PP。

思路

使用 dpdp 解决此题。

如果此时图是不连通的,在以后也可能连通。

要使此次图为连通的情况下:

如果之前图是不连通的,只有一种方法才能使得图联通,如果之前图联通,删11条边的方法有三种,删00条边的方法有一种。

要使此次图不联通的情况下:

会有一种删一条边还是不连通的情况,或有两种在之前联通的情况下删两条边变成不连通的情况。

状态:dpi,j,0/1dp_{i, j, 0/1}表示枚举到第 ii 列时,删掉了 jj 条边,图是否联通

转移:

i=1i = 1时,dp1,1,0=1,dp1,0,1=1dp_{1, 1, 0} = 1, dp_{1, 0, 1} = 1。

dpi,j,1+=dpi−1,j,1+dpi−1,j−1,0+3∗dpi−1,j−1,1dp_{i, j, 1} += dp_{i - 1, j, 1} + dp_{i - 1, j - 1, 0} + 3 * dp_{i - 1, j - 1, 1}

dpi,j,0+=dpi−1,j−1,0+2∗dpi−1,j−2,1dp_{i, j, 0} += dp_{i - 1, j - 1, 0} + 2 * dp_{i - 1, j - 2, 1}

标程

C++
#include<bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 3e3 + 5;

int n, p;
ll dp[N][N][2];

int main(){
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n >> p;
  dp[1][1][0] = dp[1][0][1] = 1;
  for(int i = 2; i <= n; i++){
    for(int j = 0; j < n; j++){
      dp[i][j][1] = (dp[i - 1][j][0] + dp[i - 1][j][1]) % p;
      if(j >= 1){
        dp[i][j][0] = dp[i - 1][j - 1][0];
        dp[i][j][1] = (dp[i][j][1] + 3 * dp[i - 1][j - 1][1]) % p;
      }
      if(j >= 2){
        dp[i][j][0] = (dp[i][j][0] + 2 * dp[i - 1][j - 2][1]) % p;
      }
    }
  }
  for(int i = 1; i < n; i++){
    cout << dp[n][i][1] << ' ';
  }
  return 0;
}

atcoder abc245 - f题解

题意

有 NN 个顶点 MM 条边的简单有向图,第 ii 条边连接顶点 UiU_i 和顶点 ViV_i。问高桥能从多少个顶点出发实现无限次移动。

思路

因为 NN 是有限的,所以要实现无限次移动需要找到一个环。一个不能到达环的路径必定有一个出度为 00 的点。于是我们建一个反图,让出度为 00 变为入度为 00,在对其使用拓扑排序,拓扑排序时遍历到的长度即为不能走到环的点的数量,所以剩下的点都可以走到环上,表示可以从这个点出发实现无限次移动,这些剩下的点的个数即为答案。

标程

C++
#include<bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5;

int n, m, ind[N], c, q[N];
vector<int> g[N];

void topo(){
  for(int i = 1; i <= n; i++){
    if(!ind[i]){
      q[++c] = i;
    }
  }
  for(int i = 1; i <= c;){
    int u = q[i];
    i++;
    for(int v : g[u]){
      ind[v]--;
      if(!ind[v]){
        q[++c] = v;
      }
    }
  }
}

int main(){
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n >> m;
  for(int i = 1, u, v; i <= m; i++){
    cin >> u >> v;
    g[v].push_back(u);
    ind[u]++;
  }
  topo();
  cout << n - c;
  return 0;
}

atcoder abc246 - e题解

题意

有一个 N×NN \times N 的棋盘若 Si,j=S_{i, j} = .,表示位置 (i,j)(i, j)为空,若 Si,j=S_{i, j} = #,表示 (i,j)(i, j) 上有兵。有一枚象在位置 (Ax,Ay)(A_x, A_y)。象可以斜向移动,问象到 (Bx,By)(B_x, B_y) 需要的最小步数。无法到达则输出 -1。

思路

我们需要使用一个 01BFS01BFS,双端队列维护位置和方向,每次移动一格,可以走到它的左下角、右下角、左上角、右上角,如果这一次走到方向与前一次方向相同,则不计入步数中,否则步数加一。状态设计为 (x,y,dir)(x, y, dir), 表示位置 (x,y)(x, y)上,与方向 dirdir,dir=0/1/2/3dir = 0/1/2/3分别表示走左下角、右下角、左上角、右上角。

标程

C++
#include<bits/stdc++.h>

using namespace std;

const int N = 15e2 + 5, dx[] = {1, 1, -1, -1}, dy[] = {-1, 1, -1, 1};

struct Node{
  int x, y, d, dis;
};

int n, ax, ay, bx, by, vis[N][N][4];
char a[N][N];
deque<Node> q;

void bfs(){
  for(int i = 0; i < 4; i++){
    q.push_front({ax, ay, i, 1});
  }
  while(q.size()){
    Node now = q.front();
    q.pop_front();
    int x = now.x, y = now.y, d = now.d, dis = now.dis;
    if(x == bx && y == by){
      cout << dis;
      return ;
    }
    vis[x][y][d] = 1;
    for(int i = 0; i < 4; i++){
      int nx = x + dx[i], ny = y + dy[i];
      if(nx >= 1 && ny >= 1 && nx <= n && ny <= n && !vis[nx][ny][i] && a[nx][ny] == '.'){
        if(i == d){
          q.push_front({nx, ny, i, dis});
        } else{
          q.push_back({nx, ny, i, dis + 1});
        }
      }
    }
  }
  cout << -1;
}

int main(){
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n >> ax >> ay >> bx >> by;
  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= n; j++){
      cin >> a[i][j];
    }
  }
  bfs();
  return 0;
}

atcoder abc245 - e题解

题意

有 NN 块巧克力和 MM 个盒子,第 ii 块巧克力宽 AiA_i,长 BiB_i,第 ii 个盒子宽 CiC_i,长 DiD_i。要把所有巧克力放进盒子里,如果要把第 ii 个巧克力放入第jj个盒子里,需满足 Cj≥AiC_j \ge A_i 并且 Dj≥BiD_j \ge B_i , 如果能把所有巧克力放入盒子中就输出 Yes,否则输出 No。

思路

当看到Cj≥AiC_j \ge A_i 和 Dj≥BiD_j \ge B_i这两个不等式时,就能想到这是一个二维偏序,我们先按第一维排序,这样就只需要关心第二维,只需在选了的盒子中找到一个长 ≥\ge 这块巧克力的长的盒子,我们只需要使用找到第一个满足要求的盒子,这个盒子即为最小盒子,在使用双指针求解时需要一个 set 维护当前可用盒子的长的集合,但长可能有重复,所以使用multiset。

标程

C++
#include<bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5;

struct Node{
  int x, y;
}a[N], b[N];

int n, m, ans;
multiset<int> s;

bool cmp(const Node &i, const Node &j){
  return i.x > j.x;
}

int main(){
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n >> m;
  for(int i = 1; i <= n; i++){
    cin >> a[i].x;
  }
  for(int i = 1; i <= n; i++){
    cin >> a[i].y;
  }
  for(int i = 1; i <= m; i++){
    cin >> b[i].x;
  }
  for(int i = 1; i <= m; i++){
    cin >> b[i].y;
  }
  sort(a + 1, a + n + 1, cmp);
  sort(b + 1, b + m + 1, cmp);
  for(int i = 1, j = 1; i <= n; i++){
    for(; j <= m && b[j].x >= a[i].x; j++){
      s.insert(b[j].y);
    }
    auto it = s.lower_bound(a[i].y);
    if(it == s.end()){
      cout << "No";
      return 0;
    }
    s.erase(it);
  }
  cout << "Yes";
  return 0;
}

atcoder abc245 - d 题解

题意

已知

A(x)=a0+a1x+a2x2+...+anxnA(x) = a_0 + a_1x + a_2x^2 + ... + a_nx^n

C(x)=c0+c1x+c2x2+...+cn+mxn+mC(x) = c_0 + c_1x + c_2x^2 + ... + c_{n + m}x^{n + m}

C(x)=A(x)×B(x)C(x) = A(x) \times B(x)

B(x)=b0+b1x+b2x2+...+bmxmB(x) = b_0 + b_1x + b_2x^2 + ... + b_mx^m

求B(x)B(x)

思路

首先已知 C(x)=A(x)×B(x)C(x) = A(x) \times B(x),可以变成 c(k)=∑j=0kai×bk−i       (0≤k≤n+m)c(k) = \sum\limits_{j=0}^{k} a_i \times b_{k - i} \ \ \ \ \ \ \ (0 \le k \le n + m)。

我们知道 cn+m=an×bmc_{n + m} = a_n \times b_m,所以 bm=cn+manb_m = c_n + m \over a_n。

求出 bmb_m 后它对 CC 的贡献减去 C(x)−bm×xm×A(x)C(x) - b_m \times x^m \times A(x) 照此步骤可以求出所有 bib_i。

时间复杂度:O(n×m)O(n \times m)

标程

C++
#include<bits/stdc++.h>

using namespace std;

const int N = 2e2 + 5;

int n, m, a[N], b[N], c[N];

int main(){
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n >> m;
  for(int i = 0; i <= n; i++){
    cin >> a[i];
  }
  for(int i = 0; i <= n + m; i++){
    cin >> c[i];
  }
  for(int i = n + m; i >= n; i--){
    b[i - n] = c[i] / a[n];
    for(int j = i; j >= i - n; j--){
      c[j] -= b[i - n] * a[n - i + j];
    }
  }
  for(int i = 0; i <= m; i++){
    cout << b[i] << ' ';
  }
  return 0;
}

atcoder abc243 - d

题目大意

一开始有一个无限大的完全二叉树,有一个长度为 NN 的字符串,高桥位于顶点 XX。对于 SS 中的每一个字符,如果为 UU ,则移动到当前点的父亲节点,为 LL 则移动到当前点的左儿子节点,为 RR 则移动到当前点的右儿子节点,问最后所在的节点编号。

思路

如果直接对 XX 进行做操作,题目只保证了答案不超过101810^{18},过程中可能会超过 101810 ^ {18},所以不能直接操作。

通过观察题目可以发现,如果在 LL 或 RR 之后出现了 UU,则可以抵消掉,位置不会发生变化。所以我们可以通过模拟一个栈来解决问题。如果在遇到 UU 且栈为空的情况,则直接将 XX 移动到它的父节点上。对于栈中剩下的元素,最后在根据剩下元素移动 XX 的位置。

标程

C++
#include<bits/stdc++.h>

using namespace std;

using ll = long long;

int n;
ll x;
string s;
vector<char> v;

int main(){
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n >> x >> s;
  s = ' ' + s;
  for(int i = 1; i <= n; i++){
    if(s[i] == 'L' || s[i] == 'R'){
      v.push_back(s[i]);
    } else{
      if(v.size()){
        v.pop_back();
      } else{
        x /= 2;
      }
    }
  }
  for(int c : v){
    x = x * 2 + (c == 'R');
  }
  cout << x;
  return 0;
}

atcoder abc242 - e题解

题目大意

有 TT 组数据,对于每组数据,给定一个整数 NN 和 一个字符串 SS ,问有多少个长度为 NN 的字符串是一个回文串,字典序小于 SS。结果对 998244353998244353 ,取模。

思路

我们知道回文串的第 ii 个字符等于它的倒数第 ii 个字符,所以我们只需要取前半段。

那么我们要先算出字典序小于 SS 的前一半的字符串个数。最后判断如果构造的回文串的前半段等于 SS 的情况下,这个回文串是否字典序小于 SS。

标程

C++
#include<bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5, MOD = 998244353;

int n, ans;
string s;

void solve(){
  cin >> n >> s;
  ans = 0;
  for(int i = 0; i < (n + 1) / 2; i++){
    ans = (1ll * ans * 26 % MOD + s[i] - 'A') % MOD;
  }
  ans++;
  for(int i = (n + 1) / 2; i < n; i++){
    if(s[n - i - 1] != s[i]){
      ans -= s[n - i - 1] > s[i];
      break;
    }
  }
  cout << ans % MOD << '\n';

}

int main(){
  ios::sync_with_stdio(false), cin.tie(0);
  int T;
  cin >> T;
  while(T--){
    solve();
  }
  return 0;
}

atcoder abc242 - d题解

题目大意

给定由 A,B,C 组成的字符串串 SS,每次操作中A能变成BC,B能变成CA,C能变成AB。共QQ次询问,每次询问经过 tt 次操作后,第 kk 个字符是什么。

思路

我们先考虑使用暴力。tt 有 101810^{18},这么大,无法直接求出字符串SS,不可能实现。

题中的操作会将字符串长度乘上22,所以S(t)S^{(t)}的第kk个字符是由S(t−1)S^{(t - 1)}的第⌊k/2⌋\lfloor k / 2 \rfloor个字符变化过来。

得出这一结论后,我们先用0,1,20, 1, 2代替A, B, C,可以得出,每次操作后,每个字符会变成(这个字符+1)mod  3(这个字符 + 1) \mod 3和(这个字符+2)mod  3(这个字符 + 2) \mod 3

通过以上结论,我们考虑使用递归分治解决这道题,每次t−1t - 1,⌊k/2⌋\lfloor k / 2 \rfloor,直到t=0t = 0或k=0k = 0

时间复杂度:O(log⁡k)O(\log k)

标程

C++
#include<bits/stdc++.h>

using namespace std;
using ll = long long;

string s;
int q, n;
ll t, k;

char dfs(ll t, ll k){
  if(!t){
    return s[k];
  }
  if(!k){
    return (s[0] - 'A' + t) % 3 + 'A';
  }
  return (dfs(t - 1, k / 2) - 'A' + k % 2 + 1) % 3 + 'A';
}

int main(){
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> s >> q;
  while(q--){
    cin >> t >> k;
    k--;
    cout << dfs(t, k) << '\n';
  }
  return 0;
}
原文