Codeforces Round 871 (Div. 4)
Codeforces Round 871 (Div. 4)
#include<bits/stdc++.h> using namespace std; typedef pair<int,int>PII; typedef pair<string,int>PSI; const int N=1e5+5,INF=0x3f3f3f3f,Mod=1e6; const double eps=1e-6; typedef long long ll; int n; int main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); string s; cin>>n; string a="codeforces"; while(n--){ cin>>s; int c=0; for(int i=0;i<10;++i){ if(s[i]!=a[i])c++; } cout<<c<<'\n'; } return 0; }
#include<bits/stdc++.h> using namespace std; typedef pair<int,int>PII; typedef pair<string,int>PSI; const int N=1e5+5,INF=0x3f3f3f3f,Mod=1e6; const double eps=1e-6; typedef long long ll; int t,n; int main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); cin>>t; while(t--){ cin>>n; int res=0; for(int i=0,x,ma=0;i<n;++i){ cin>>x; if(x==0)ma++; else ma=0; res=max(res,ma); } cout<<res<<'\n'; } return 0; }
#include<bits/stdc++.h> using namespace std; typedef pair<int,int>PII; typedef pair<string,int>PSI; const int N=1e5+5,INF=0x3f3f3f3f,Mod=1e6; const double eps=1e-6; typedef long long ll; int t,n; int main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); cin>>t; while(t--){ cin>>n; int l=INF,r=INF,lp=-1,rp=-1,a=INF; string s; for(int i=0,m;i<n;++i){ cin>>m>>s; if(s[0]=='1'&&s[1]=='1')a=min(m,a); else{ if(s[0]=='1')l=min(l,m),lp=i; if(s[1]=='1')r=min(r,m),rp=i; } } if((lp==-1||rp==-1)&&a==INF)cout<<"-1\n"; else{ cout<<min(a,l+r)<<'\n'; } } return 0; }
#include<bits/stdc++.h> using namespace std; typedef pair<int,int>PII; typedef pair<string,int>PSI; const int N=1e7+5,INF=0x3f3f3f3f,Mod=1e6; const double eps=1e-6; typedef long long ll; int t,n,m; bool st[N]; bool bfs(){ memset(st,false,sizeof st); queue<int>q; q.push(n); st[n]=true; while(q.size()){ int f=q.front();q.pop(); if(f==m){ return true; } if(f%3==0){ if(!st[f/3*2])q.push(f/3*2),st[f/3*2]=true; if(!st[f/3])q.push(f/3),st[f/3]=true; } } return false; } int main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); cin>>t; while(t--){ cin>>n>>m; if(n>=m&&bfs())cout<<"YES\n"; else cout<<"NO\n"; } return 0; }
#include<bits/stdc++.h> using namespace std; typedef pair<int,int>PII; typedef pair<string,int>PSI; const int N=1e3+5,INF=0x3f3f3f3f,Mod=1e6; const double eps=1e-6; typedef long long ll; int t,n,m; int a[N][N]; int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1}; int bfs(int x,int y){ int res=a[x][y]; queue<PII>q; q.push({x,y}); a[x][y]=0; while(q.size()){ auto f=q.front();q.pop(); int x1=f.first,y1=f.second; for(int i=0;i<4;++i){ int xx=x1+dx[i],yy=y1+dy[i]; if(xx>=0&&xx<n&&yy>=0&&yy<m&&a[xx][yy]>0){ res+=a[xx][yy]; q.push({xx,yy}); a[xx][yy]=0; } } } return res; } int main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); cin>>t; while(t--){ cin>>n>>m; int res=0; for(int i=0;i<n;++i) for(int j=0;j<m;++j)cin>>a[i][j]; for(int i=0;i<n;++i){ for(int j=0;j<m;++j){ if(a[i][j]>0){ res=max(res,bfs(i,j)); } } } cout<<res<<'\n'; } return 0; }
思路:求每个节点的入度,除了叶子节点的入度为1,剩余的入度只有两种或一种;若为两种,数量少的为中心
#include<bits/stdc++.h> using namespace std; typedef pair<int,int>PII; typedef pair<string,int>PSI; const int N=2e2+5,INF=0x3f3f3f3f,Mod=1e6; const double eps=1e-6; typedef long long ll; int t,n,m; int ru[N]; int main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); cin>>t; while(t--){ cin>>n>>m; memset(ru,0,sizeof ru); for(int i=0,u,v;i<m;++i){ cin>>u>>v; ru[u]++,ru[v]++; } map<int,int>mp; for(int i=1;i<=n;++i){ if(ru[i]>1){ mp[ru[i]]++; } } if(mp.size()==1){ int c=mp.begin()->first; cout<<c<<' '<<c-1<<'\n'; } else{ int a[2],b[2],i=0; for(auto u:mp){ a[i]=u.first; b[i++]=u.second; } if(b[0]<b[1])swap(b[0],b[1]),swap(a[0],a[1]); cout<<a[1]<<' '<<a[0]-1<<'\n'; } } return 0; }
思路:a[i][j]表示第i行第j列的数,a[i][j]=a[i-1][j]+a[i-1][j-1]-a[i-2][j-1],(有被重复加的格子);第i层的数有i*(i+1)/2个,前i层的数有i*(i+1)*(i+2)/6个,可以二分求出层数,也可以直接用数组标记每个数的位置
#include<bits/stdc++.h> using namespace std; typedef pair<int,int>PII; typedef pair<string,int>PSI; const int N=1500+5,INF=0x3f3f3f3f,Mod=1e6; const double eps=1e-6; typedef long long ll; ll t,n,a[N],idx; ll b[N][N]; void P(){ ll i; for(i=1;;++i){ a[i]=i*(i+1)/2; if(a[i]>=1e6)break; }idx=i; ll c=1; for(ll k=1;k<=idx;++k){ for(ll j=1;j<=k;++j){ b[k][j]=c*c;c++; b[k][j]+=b[k-1][j]+b[k-1][j-1]; if(k>2&&j!=1&&j!=k)b[k][j]-=b[k-2][j-1]; } } } int main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); cin>>t; P(); while(t--){ cin>>n; ll p= lower_bound(a+1,a+idx+1,n)-a; ll i=p,j=n-a[p-1]; cout<<b[i][j]<<'\n'; } return 0; }
H-Don't Blame Me
思路:dp[i][j]表示在前i个数选择1~i个数and组成j的个数;
1.不选第i个数:dp[i][j]+=dp[i-1][j]
2.选第i个数:dp[i][j&a[i]]+=dp[i-1][j]
3.第i个数作为起点:dp[i][a[i]]+=1;
//返回输入数据中,二进制中‘1’的个数 __builtin_popcount() //对于不同的数据类型 __builtin_popcount = int __builtin_popcountl = long int __builtin_popcountll = long long
#include<bits/stdc++.h> using namespace std; typedef pair<int,int>PII; typedef pair<string,int>PSI; const int N=2e5+5,INF=0x3f3f3f3f,Mod=1e9+7; const double eps=1e-6; typedef long long ll; int t,n,k,a[N]; int dp[N][65]; int main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); cin>>t; while(t--){ for(int i=1;i<=n;++i) for(int j=0;j<64;++j)dp[i][j]=0; cin>>n>>k; for(int i=1;i<=n;++i){ cin>>a[i]; for(int j=0;j<64;++j){ dp[i][j]=(dp[i][j]+dp[i-1][j])%Mod; dp[i][j&a[i]]=(dp[i][j&a[i]]+dp[i-1][j])%Mod; } dp[i][a[i]]=(dp[i][a[i]]+1)%Mod; } int res=0; for(int i=0;i<64;++i){ if(__builtin_popcount(i)==k){ res=(res+dp[n][i])%Mod; } } cout<<res<<'\n'; } return 0; }