集訓 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\) \(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 乘法
這是真唐,整個拉插板子,我也不會,題解也不會 😅😅😅