集训 4 & 模拟 5
集训 4 & 模拟 5
有点唐简单,所以一起写了(其实是因为之前懒得写)
集训 4:
T1 模拟,赛时不删调试保龄了。
T2 显然贪心
T3 发现显然要两两互质,有因为父比子小,所以方案数就是将 \(\varphi\) 乘起来(甚至都不需要线性筛)
T4 meet in middle 板子。
模拟 5
-
T1 特殊字符串
显然有 \(n^2\) 的 \(dp\),发现字符集为 \(26\) 可以对结尾字符分别存储 \(\max\) 优化到 \(n*26\)
CODE
#include<bits/stdc++.h> #include<sys/mman.h> #include<fcntl.h> using namespace std; using llt=long long; using llf=long double; using ull=unsigned long long; #define Ct const #define Il __always_inline #define For(i,a,b,c) for(int i=(a);i<=(b);i+=(c)) #define For_(i,a,b,c) for(int i=(a);i>=(b);i-=(c)) #define For_it(i,a,b) for(auto i=(a);i!=(b);++i) namespace IO{ #ifdef ONLINE_JUDGE int Fin=open("shiki.in",0); FILE* Fout=fopen("shiki.out","w"); #elif defined(UN_FAST) FILE *Fin=freopen("in_out/in.in","r",stdin),*Fout=freopen("in_out/out.out","w",stdout); #else int Fin=open("in_out/in.in",0); FILE* Fout=fopen("in_out/out.out","w"); #endif // file #ifdef UN_FAST char cc; #define G (cc=getchar()) #define C cc #else const char *I=(char*)mmap(0,1<<28,1,2,Fin,0)-1; #define G (*++I) #define C (*I) #endif // fast (mmap) #define P(x) putc_unlocked(x,Fout) template<class T> Il void read(T &x){x=0;bool f=0; while(f|=G==45,C<48); while(x=x*10+(C&15),G>47); f?x=-x:0;} template<class T> void write(T x){if(x<0)P('-'),x=-x; if(x/10)write(x/10); P('0'+x%10);} Il void read(char &c){while(G<33); c=C;} void read(char *s){int p=-1; while(G<33); while(s[++p]=C,G>32); s[++p]='\0';} Il void read(string &s){s.clear(); while(G<33); while(s.push_back(C),G>32);} template<class T=int> Il T read(){T a; read(a); return a;} template<class T,class... Argc> Il void read(T &x,Argc&... argc){read(x),read(argc...);} Il void write(const char &c){P(c);} Il void write(const char *s){for(const char *c=s;*c!='\0';++c) P(*c);} Il void write(const string &s){for(const char& c:s) P(c);} template<class T,class... Argc> Il void write(const T &x,const Argc&... argc){write(x),write(argc...);} template<class T> Il void Write(const T &x){write(x),P(' ');} Il void Write(const char &c){P(c); if(c>32)P(' ');} template<class T,class... Argc> Il void Write(const T &x,const Argc&... argc){Write(x),Write(argc...);} #undef G #undef C #undef P #define endl '\n' }using IO::read; using IO::write; using IO::Write; constexpr int N=1e5+3; llt ma[30],n,m,wt[30][30],ans; string s; int main(){ read(n,s,m); For(i,1,m,1){ char a,b; int k; read(a,b,k); wt[a-'a'][b-'a']+=k; } memset(ma,-0x3f,sizeof(ma)); ma[s[0]-'a']=0; For(i,2,n,1){ int nw=s[i-1]-'a'; For(j,0,25,1) ans=max(ans,ma[j]+wt[j][nw]); ma[nw]=max(ma[nw],ans); } write(ans); } // For(i,1,n,1) For(j,1,i-1,1) dp[i]=max(dp[i],dp[j]+wt[s[j-1]-'a'][s[i-1]-'a']);
-
T2 宝可梦
发现从一个点到自己的路径是一个环,并且每条询问路径都是环上一段,直接预处理环即可(不太好写,赛时想假了好几次)。
CODE
#include<bits/stdc++.h> #include<sys/mman.h> #include<fcntl.h> using namespace std; using llt=long long; using llf=long double; using ull=unsigned long long; #define Ct const #define Il __always_inline #define For(i,a,b,c) for(int i=(a);i<=(b);i+=(c)) #define For_(i,a,b,c) for(int i=(a);i>=(b);i-=(c)) #define For_it(i,a,b) for(auto i=(a);i!=(b);++i) namespace IO{ #ifdef ONLINE_JUDGE int Fin=open("pokemon.in",0); FILE* Fout=fopen("pokemon.out","w"); #elif defined(UN_FAST) FILE *Fin=freopen("in_out/in.in","r",stdin),*Fout=freopen("in_out/out.out","w",stdout); #else int Fin=open("in_out/in.in",0); FILE* Fout=fopen("in_out/out.out","w"); #endif // file #ifdef UN_FAST char cc; #define G (cc=getchar()) #define C cc #else const char *I=(char*)mmap(0,1<<28,1,2,Fin,0)-1; #define G (*++I) #define C (*I) #endif // fast (mmap) #define P(x) putc_unlocked(x,Fout) template<class T> Il void read(T &x){x=0;bool f=0; while(f|=G==45,C<48); while(x=x*10+(C&15),G>47); f?x=-x:0;} template<class T> void write(T x){if(x<0)P('-'),x=-x; if(x/10)write(x/10); P('0'+x%10);} Il void read(char &c){while(G<33); c=C;} void read(char *s){int p=-1; while(G<33); while(s[++p]=C,G>32); s[++p]='\0';} Il void read(string &s){s.clear(); while(G<33); while(s.push_back(C),G>32);} template<class T=int> Il T read(){T a; read(a); return a;} template<class T,class... Argc> Il void read(T &x,Argc&... argc){read(x),read(argc...);} Il void write(const char &c){P(c);} Il void write(const char *s){for(const char *c=s;*c!='\0';++c) P(*c);} Il void write(const string &s){for(const char& c:s) P(c);} template<class T,class... Argc> Il void write(const T &x,const Argc&... argc){write(x),write(argc...);} template<class T> Il void Write(const T &x){write(x),P(' ');} Il void Write(const char &c){P(c); if(c>32)P(' ');} template<class T,class... Argc> Il void Write(const T &x,const Argc&... argc){Write(x),Write(argc...);} #undef G #undef C #undef P #define endl '\n' }using IO::read; using IO::write; using IO::Write; constexpr int N=1e5+3,NM=5e5+3,mx[]={0,0,-1,0,1},my[]={0,1,0,-1,0},tlt[]={0,2,3,4,1},trt[]={0,4,1,2,3}; vector<bool> c[N]; vector<int> pos[5][N]; int n,m,q,cnt; void Dfs(Ct int &x,Ct int &y,Ct int &fw){ if(pos[fw][x][y]) return ; pos[fw][x][y]=++cnt; int nf=trt[fw]; For(i,1,4,1){ int nx=x+mx[nf],ny=y+my[nf]; if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&c[nx][ny]) Dfs(nx,ny,nf); nf=tlt[nf]; } } Il void Init(){ int x,y; read(n,m); For(i,1,n,1){ c[i].resize(m+3); For(k,1,4,1) pos[k][i].resize(m+3); For(j,1,m,1) if(read<char>()=='.') c[i][j]=1,x=i,y=j; } Dfs(x,y,2); } Il int Get(Ct int &xa,Ct int &ya,Ct int &fa,Ct int &xb,Ct int &yb,Ct int &fb){ if(!pos[fa][xa][ya]||!pos[fb][xb][yb]) return 0x3f3f3f3f; if(pos[fa][xa][ya]<=pos[fb][xb][yb]) return pos[fb][xb][yb]-pos[fa][xa][ya]; return cnt+pos[fb][xb][yb]-pos[fa][xa][ya]-1; } int main(){ Init(); read(q); For(i,1,q,1){ int xa,ya,xb,yb,fa; char c; read(xa,ya,xb,yb,c); if(c=='R') fa=1; else if(c=='U') fa=2; else if(c=='L') fa=3; else fa=4; if(xa==xb&&ya==yb){write(0,'\n'); continue;} xa+=mx[fa],ya+=my[fa]; int ans=0x3f3f3f3f; For(i,1,4,1) ans=min(ans,Get(xa,ya,fa,xb,yb,i)+1); write(ans,'\n'); } }
-
T3 矩阵
如果有相邻且相同的,就是 \(-1\)
其他情况对公比分治,在公比 \(>200\) 时最多长度为 \(2\),可以直接整。
公比 \(\le 200\) 时 \(O(n^2*\text{公比})\) 暴力 \(dp\) 即可。
也可以直接最长路。
ps:
谁 tm 造的数据,每个 sub 里塞一个 ans=1 ,给我卡没了。
CODE
#include<bits/stdc++.h> #include<sys/mman.h> #include<fcntl.h> using namespace std; using llt=long long; using llf=long double; using ull=unsigned long long; #define Ct const #define Il __always_inline #define For(i,a,b,c) for(int i=(a);i<=(b);i+=(c)) #define For_(i,a,b,c) for(int i=(a);i>=(b);i-=(c)) #define For_it(i,a,b) for(auto i=(a);i!=(b);++i) namespace IO{ #ifdef ONLINE_JUDGE int Fin=open("matrix.in",0); FILE* Fout=fopen("matrix.out","w"); #elif defined(UN_FAST) FILE *Fin=freopen("in_out/in.in","r",stdin),*Fout=freopen("in_out/out.out","w",stdout); #else int Fin=open("in_out/in.in",0); FILE* Fout=fopen("in_out/out.out","w"); #endif // file #ifdef UN_FAST char cc; #define G (cc=getchar()) #define C cc #else const char *I=(char*)mmap(0,1<<28,1,2,Fin,0)-1; #define G (*++I) #define C (*I) #endif // fast (mmap) #define P(x) putc_unlocked(x,Fout) template<class T> Il void read(T &x){x=0;bool f=0; while(f|=G==45,C<48); while(x=x*10+(C&15),G>47); f?x=-x:0;} template<class T> void write(T x){if(x<0)P('-'),x=-x; if(x/10)write(x/10); P('0'+x%10);} Il void read(char &c){while(G<33); c=C;} void read(char *s){int p=-1; while(G<33); while(s[++p]=C,G>32); s[++p]='\0';} Il void read(string &s){s.clear(); while(G<33); while(s.push_back(C),G>32);} template<class T=int> Il T read(){T a; read(a); return a;} template<class T,class... Argc> Il void read(T &x,Argc&... argc){read(x),read(argc...);} Il void write(const char &c){P(c);} Il void write(const char *s){for(const char *c=s;*c!='\0';++c) P(*c);} Il void write(const string &s){for(const char& c:s) P(c);} template<class T,class... Argc> Il void write(const T &x,const Argc&... argc){write(x),write(argc...);} template<class T> Il void Write(const T &x){write(x),P(' ');} Il void Write(const char &c){P(c); if(c>32)P(' ');} template<class T,class... Argc> Il void Write(const T &x,const Argc&... argc){Write(x),Write(argc...);} #undef G #undef C #undef P #define endl '\n' }using IO::read; using IO::write; using IO::Write; constexpr int N=4e4+3,mx[]={0,1,-1,0,0},my[]={0,0,0,1,-1}; vector<int> c[N],dp[N]; int n,m,ans=1; int dfs(int x,int y,int k){ if(dp[x][y]) return dp[x][y]; else { For(i,1,4,1){ int nx=x+mx[i],ny=y+my[i]; if(nx>n||nx<1||ny>m||ny<1||c[nx][ny]!=c[x][y]*k) continue; dp[x][y]=max(dp[x][y],dfs(nx,ny,k)); } dp[x][y]+=1; return dp[x][y]; } } int main(){ For(i,1,N-3,1) c[i].push_back(0); read(n,m); For(i,1,m,1){ c[1].push_back(read()); if(c[1][i]==c[1][i-1]){write(-1); return 0;} else if(i!=1&&(c[1][i]%c[1][i-1]==0||c[1][i-1]%c[1][i]==0)) ans=2; } For(i,2,n,1) For(j,1,m,1){ c[i].push_back(read()); if(c[i][j]==c[i][j-1]||c[i][j]==c[i-1][j]){write(-1); return 0;} else if(j!=1&&(c[i][j]%c[i][j-1]==0||c[i][j-1]%c[i][j]==0||c[i][j]%c[i-1][j]==0||c[i-1][j]%c[i][j]==0)) ans=2; } For(i,2,200,1){ For(j,1,n,1) dp[j].clear(),dp[j].resize(m+1); For(a,1,n,1) For(b,1,m,1) ans=max(ans,dfs(a,b,i)); } write(ans); }
-
T4 乘法
这是真唐,整个拉插板子,我也不会,题解也不会 😅😅😅
——————————————————————————————————————————————————————————————————————————————————————————————————————————
本文来自博客园,作者:xrlong,转载请注明原文链接:https://www.cnblogs.com/xrlong/p/18172135