一时间不知道要干啥了,随便写点题目吧
亚运奖牌榜
很简单的模拟,题目看错了整了半天。。
#include<bits/stdc++.h>
using namespace std;
int n;
struct a
{int pai[4];
}c1,c2;
int main()
{cin>>n;memset(c1.pai,0,sizeof(c1.pai));memset(c2.pai,0,sizeof(c2.pai));for(int i=1;i<=n;i++){int nm,k;cin>>nm>>k;if(nm==0)c1.pai[k]++;if(nm==1)c2.pai[k]++;}cout<<c1.pai[1]<<" "<<c1.pai[2]<<" "<<c1.pai[3]<<endl;cout<<c2.pai[1]<<" "<<c2.pai[2]<<" "<<c2.pai[3]<<endl;if((c1.pai[1]>c2.pai[1])||(c1.pai[1]==c2.pai[1]&&c1.pai[2]>c2.pai[2])||(c1.pai[1]==c2.pai[1]&&c1.pai[2]==c2.pai[2]&&c1.pai[3]>c2.pai[3])){cout<<"The first win!";return 0;}else if((c1.pai[1]<c2.pai[1])||(c1.pai[1]==c2.pai[1]&&c1.pai[2]<c2.pai[2])||(c1.pai[1]==c2.pai[1]&&c1.pai[2]==c2.pai[2]&&c1.pai[3]<c2.pai[3])){cout<<"The second win!";return 0;}
}
出院
一道字符串的模拟,还是要看题!!
#include<bits/stdc++.h>
using namespace std;
const int N=105;
int n,m;
struct sss
{string ss;char a;
}s[N];
int main()
{cin>>n>>m;for(int i=1;i<=n;i++)cin>>s[i].ss>>s[i].a;for(int i=1;i<=m;i++){string sp;cin>>sp;string sf="",ssf="";int f=0,cnt=0;for(int j=1;j<=n;j++){if(s[j].ss==sp){f=2;cout<<s[j].a<<endl;break;}}if(f==2)continue;for(int j=0;j<sp.length();j++){sf+=sp[j];for(int k=1;k<=n;k++)if(sf==s[k].ss){ssf+=s[k].a;f=1;cnt++;sf="";}}if(f==1&&cnt<=2)cout<<ssf<<endl;elsecout<<"D"<<endl;}
}
相对论大师
这题思路很好想,用map存路径,然后一直bfs找路径,有更短的就给换了~~
主要是一开始的存入路径,我觉得这个比较重要,如下:
for(int i=1;i<=n;i++)
{int a,b;cin>>s1>>a1>>s2>>a2;if(!mp.count(s1)){mp[s1]=idx;name[idx]=s1;name[idx+1]=s1;idx+=2;}if(!mp.count(s2)){mp[s2]=idx;name[idx]=s2;name[idx+1]=s2;idx+=2;}if(a1==0) a=mp[s1];else if(a1==1) a=mp[s1]+1;if(a2==0) b=mp[s2];else if(a2==1) b=mp[s2]+1;add(a,b);
}
我用的是链式前向星存图,也可以用邻接表
代码:
#include<bits/stdc++.h>
using namespace std;
const int N=2e3+5;
string s1,s2;
int a1,a2;
string name[N];
int idx;
map<string,int> mp;
int pre[N<<2];
int n;
int cnt;
int head[N<<2];
struct e
{int to,from;
}edge[N<<2];
void add(int a,int b)
{cnt++;edge[cnt].to=b;edge[cnt].from=head[a];head[a]=cnt;
}
vector<int> bfs(int sta,int tar)
{queue<int> q;q.push(sta);memset(pre,0,sizeof(pre));while(!q.empty()){int k=q.front();q.pop();if(k==tar)break;for(int i=head[k];i;i=edge[i].from){int v=edge[i].to;if(!pre[v]){q.push(v);pre[v]=k;}}}vector<int> r;do{r.push_back(tar);tar=pre[tar];}while(tar!=0);return r;
}
int main()
{cin>>n;idx=1;for(int i=1;i<=n;i++){int a,b;cin>>s1>>a1>>s2>>a2;if(!mp.count(s1)){mp[s1]=idx;name[idx]=s1;name[idx+1]=s1;idx+=2;}if(!mp.count(s2)){mp[s2]=idx;name[idx]=s2;name[idx+1]=s2;idx+=2;}if(a1==0)a=mp[s1];else if(a1==1)a=mp[s1]+1;if(a2==0)b=mp[s2];else if(a2==1)b=mp[s2]+1;add(a,b);
// cout<<s1<<" "<<a1<<" "<<mp[s1]<<" "<<name[mp[s1]]<<" "<<endl;
// cout<<s2<<" "<<a2<<" "<<mp[s2]<<" "<<name[mp[s2]]<<" "<<endl;}vector<int> t=bfs(1,2);vector<int> res(N,0);for(int i=1;i<=idx;i+=2){vector<int> t1=bfs(i,i+1);vector<int> t2=bfs(i+1,i);if(res.size()>t1.size()&&t1.size()>1)res=t1;if(res.size()>t2.size()&&t2.size()>1)res=t2;}
// cout<<res.size();
// return 0;
// for(int i=0;i<=idx+2;i++)
// {
// cout<<name[i]<<" ";
// }
// return 0;for(int i=res.size()-1;i>=1;i--){cout<<name[res[i]]<<" "<<res[i]-mp[name[res[i]]]<<" ";cout<<name[res[i-1]]<<" "<<res[i-1]-mp[name[res[i-1]]]<<" ";}cout<<"=";cout<<name[res[res.size()-1]]<<" "<<res[res.size()-1]-mp[name[res[res.size()-1]]]<<" ";cout<<name[res[0]]<<" "<<res[0]-mp[name[res[0]]]<<endl;}
相对成功
这题是一个稍微复杂的最长不上升子序列,先对原始数据排一下,然后dp(找出以0,1为结束的最长的不上升子序列)
代码:
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int t;
int n;
int a,b;
int maxx;
int s[N];
int main()
{cin>>t;for(int i=1;i<=t;i++){cin>>n;maxx=0; for(int j=1;j<=n;j++){cin>>a>>b;s[j]=a+1-b;}int dp[3];memset(dp,0,sizeof(dp));for(int j=1;j<=n;j++){int x;cin>>x;for(int k=s[x];k<3;k++)dp[s[x]]=max(dp[s[x]],dp[k]+1);maxx=max(dp[s[x]],maxx);}cout<<n-maxx<<endl;}
}
......