跳到正文

题解

如果你不会矩阵乘法

zyn0309约 11 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 11 分钟
题目
CF1609E

版本

  1. v2当前
  2. v1

题目传送门

笔者不太会矩阵乘法,于是使用分块配合图论建模解决了这道题。

思路

首先设 dpi,jdp_{i,j} 为当前处理到第 ii 位,和 abc 匹配了 jj 位的最小代价,转移显然,代码如下:

C++
inline void solve(){
	for(int i=1;i<=n;++i){
	  dp[i][0]=dp[i-1][0]+(s[i]=='a');
	  dp[i][1]=dp[i-1][1]+(s[i]=='b');
	  if(s[i]=='a')dp[i][1]=min(dp[i][1],dp[i-1][0]);
	  dp[i][2]=dp[i-1][2]+(s[i]=='c');
	  if(s[i]=='b')dp[i][2]=min(dp[i][2],dp[i-1][1]);
	}
}

然后我们把每一个位置看成 33 个点,把一个位置向另一个位置转移看成一条边,图像大概是下面这样。

其中红色的边有可能不存在。

首先由于每个点都是只能往右或者往右下走,这显然是个 DAG,这启示我们使用拓扑排序(其实由于边权只有 00 和 11,还可以 0-1 bfs)。

但是现在还是有一个问题,每次修改都跑一遍拓扑排序显然不现实,因此考虑分块。

维护 resi,j,kres_{i,j,k} 代表第 ii 个块,上一个块最右边第 jj 行的点,到这个块最右边第 kk 行的点的最短距离,这个每次修改可以 O(n)O(\sqrt n) 跑拓扑排序暴力维护。

最后查询直接把所有块的 resres 扫一遍转移就行了。

时间复杂度 O(n+qn)O(n+q \sqrt n),空间复杂度 O(n)O(n)。

Code

C++
#include<bits/stdc++.h>
namespace fast_IO{
	#define IOSIZE 100000
		char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
	#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
	#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
	#define isdigit(ch) (ch>47&&ch<58)
	#define isspace(ch) (ch<33)
		template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
		template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
		template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
		inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
		inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
		inline void print(char x) { putchar(x); }
		inline void print(char *x) { while (*x) putchar(*x++); }
		inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
		inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
		inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
		inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
		inline void print(bool b) { putchar(b+48); }
		template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
		template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
		struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
		template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
		template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
	#define cout io
	#define cin io
}using namespace fast_IO;
using namespace std;
const int N=1e5+10,inf=2e9;
#define pii  pair<int,int>
#define mk make_pair
#define pb emplace_back
int n,q,bl[N],L[N],R[N],len,id[N][5],cnt,res[350][5][5],dis[N],d[N];
string s;
vector<pii>e[N];
inline void bfs(int s){
	for(int i=1;i<=cnt;++i)dis[i]=inf;
	queue<int>q;
	dis[s]=0;
	for(int i=1;i<=cnt;++i)
	for(pii p:e[i]){
	  int v=p.first;
	  ++d[v];
	}
	for(int i=1;i<=cnt;++i)
	  if(!d[i])
		q.push(i);
	while(!q.empty()){
	  int u=q.front();
	  q.pop();
	  for(pii p:e[u]){
		int v=p.first,c=p.second;
		dis[v]=min(dis[v],dis[u]+c);
		--d[v];
		if(!d[v])q.push(v);
	  }
	}
}
inline void build(int pos){
	cnt=0;
	for(int i=L[pos]-1;i<=R[pos];++i){
	  id[i][0]=++cnt;
	  id[i][1]=++cnt;
	  id[i][2]=++cnt;
	}
	for(int i=1;i<=cnt;++i)e[i].clear();
	for(int i=L[pos];i<=R[pos];++i){
	  e[id[i-1][0]].pb(id[i][0],(s[i]=='a'));
	  e[id[i-1][1]].pb(id[i][1],(s[i]=='b'));
	  if(s[i]=='a')e[id[i-1][0]].pb(id[i][1],0);
	  e[id[i-1][2]].pb(id[i][2],(s[i]=='c'));
	  if(s[i]=='b')e[id[i-1][1]].pb(id[i][2],0);
	}
	bfs(id[L[pos]-1][0]);
	res[pos][0][0]=dis[id[R[pos]][0]];
	res[pos][0][1]=dis[id[R[pos]][1]];
	res[pos][0][2]=dis[id[R[pos]][2]];
	bfs(id[L[pos]-1][1]);
	res[pos][1][1]=dis[id[R[pos]][1]];
	res[pos][1][2]=dis[id[R[pos]][2]];
	bfs(id[L[pos]-1][2]);
	res[pos][2][2]=dis[id[R[pos]][2]];
}
inline int getans(){
	int lasta=0,lastb=0,lastc=0;
	for(int pos=1;pos<=bl[n];++pos){
	  int nowa,nowb,nowc;
	  nowa=lasta+res[pos][0][0];
	  nowb=min(lasta+res[pos][0][1],lastb+res[pos][1][1]);
	  nowc=min({lasta+res[pos][0][2],lastb+res[pos][1][2],lastc+res[pos][2][2]});
	  lasta=nowa,lastb=nowb,lastc=nowc;
	}
	return min({lasta,lastb,lastc});
}
signed main(){
	cin>>n>>q>>s;
	len=sqrt(n);
	for(int i=1;i<=n;++i)bl[i]=(i-1)/len+1;
	for(int i=1;i<=bl[n];++i)L[i]=(i-1)*len+1,R[i]=i*len;
	R[bl[n]]=n;
	s=' '+s;
	int x;
	char c;
	for(int i=1;i<=bl[n];++i)build(i);
	while(q--){
	  cin>>x>>c;
	  s[x]=c;
	  build(bl[x]);
	  cout<<getans()<<"\n";
	}
	return 0;
}
原文