Codeforces Round 875 (Div. 2)
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; }
思路:分别求序列中最长连续长度,不同序列中相同数字的可以相加

#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; }
思路: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; }

#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; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】