Codeforces Round 875 (Div. 2)
思路:让序列全相等为n+1即可
#include<bits/stdc++.h> using namespace std; typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=2e5+5,INF=0x3f3f3f3f,Mod=1e9+7; const double eps=1e-6; typedef long long ll; //#define int long long void solve(){ int n;cin>>n; for(int i=1,x;i<=n;++i){ cin>>x; cout<<1+n-x<<' '; }cout<<'\n'; } int32_t main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int t=1; cin>>t; while(t--){ solve(); } return 0; }View Code
思路:分别求序列中最长连续长度,不同序列中相同数字的可以相加
#include<bits/stdc++.h> using namespace std; typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=2e5+5,INF=0x3f3f3f3f,Mod=1e9+7; const double eps=1e-6; typedef long long ll; //#define int long long void solve(){ int n;cin>>n; map<int,int>mp,mmp; for(int i=1,x,l,c=1;i<=n;++i){ cin>>x; if(i==1)l=x; else if(l!=x)mp[l]=max(mp[l],c),c=1,l=x; else c++; if(i==n)mp[l]=max(mp[l],c); } for(int i=1,x,l,c=1;i<=n;++i){ cin>>x; if(i==1)l=x; else if(l!=x)mmp[l]=max(mmp[l],c),c=1,l=x; else c++; if(i==n)mmp[l]=max(mmp[l],c); } int ma=0; for(auto v:mp){ ma=max(ma,v.second+mmp[v.first]); } for(auto v:mmp){ ma=max(ma,v.second+mp[v.first]); } cout<<ma<<'\n'; } int32_t main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int t=1; cin>>t; while(t--){ solve(); } return 0; }View Code
思路:dfs或bfs或dp。从1开始,遍历每条边w,若当前边在w前,则w与当前边在同一次得到;若当前边在w后,则w在当前边的下一次得到
#include<bits/stdc++.h> using namespace std; typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=5e5+5,INF=0x3f3f3f3f,Mod=1e9+7; //#define endl '\n' const double eps=1e-6; typedef long long ll; //#define int long long void solve(){ int n,ans=1;cin>>n; vector<PII>ve[n+1]; vector<bool>st(n+1,false); for(int i=0,u,v;i<n-1;++i){ cin>>u>>v; ve[u].push_back({v,i}); ve[v].push_back({u,i}); } function<void(int,int,int)> dfs=[&](int u,int pos,int feet){ for(auto v:ve[u]){ if(st[v.first])continue; st[v.first]=true; if(v.second<pos){ ans=max(ans,feet+1); dfs(v.first,v.second,feet+1); }else{ ans=max(ans,feet); dfs(v.first,v.second,feet); } } }; st[1]=true; dfs(1,0,1); cout<<ans<<'\n'; } int main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int t=1; cin>>t; while(t--){ solve(); } return 0; }View Code
#include<bits/stdc++.h> using namespace std; typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef pair<string,string>PSS; const int N=5e5+5,INF=0x3f3f3f3f,Mod=1e9+7; //#define endl '\n' const double eps=1e-6; typedef long long ll; //#define int long long void solve(){ int n,ans=1;cin>>n; vector<PII>dp(n+1,{0,0}),ve[n+1]; vector<bool>st(n+1,false); int p=-1; for(int i=1,u,v;i<n;++i){ cin>>u>>v; if(u==1&&p==-1)p=i; if(v==1&&p==-1)p=i; ve[u].push_back({v,i}); ve[v].push_back({u,i}); } st[1]=true;dp[1]={1,p}; queue<int>q; q.push(1); while(q.size()){ int u=q.front();q.pop(); for(auto v:ve[u]){ if(st[v.first])continue; st[v.first]=true; q.push(v.first); if(v.second<dp[u].second){ dp[v.first].first=dp[u].first+1,dp[v.first].second=v.second; ans=max(ans,dp[v.first].first); } else{ dp[v.first].first=dp[u].first,dp[v.first].second=v.second; ans=max(ans,dp[v.first].first); } } } cout<<ans<<'\n'; } int main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int t=1; cin>>t; while(t--){ solve(); } return 0; }View Code
标签:typedef,const,int,875,Codeforces,long,mmp,solve,Div From: https://www.cnblogs.com/bible-/p/17441714.html