【套题】东北大学2019新生集训

目录【收录】【未收录】:

  ※1.1 - N个数求和

  ※1.2 - 比较大小

  ※1.3 - A-B

  ※1.4 - 计算指数

  ※1.5 - 计算阶乘和

  ※1.6 - 简单题

  ※1.7 - 跟奥巴马一起画方块

  ※1.8 - 查验身份证

  ※1.9 - 集合相似度

  ※1.10 - 树的遍历

  ※1.11 - 家庭房产

  ※1.12 - 最长对称子串

  ※1.13 - 肿瘤诊断

  ※1.14 - 垃圾箱分布

  ※1.15 - 迎风一刀斩

  ※2.1 - 到底有多二

  ※2.2 - 大笨钟

  ※2.3 - 谁先倒

  ※2.4 - 帅到没朋友

  ※2.5 - 重要的话说三遍

  ※2.6 - 奇偶分家

  ※2.7 - 输出GPLT

  ※2.8 - 后天

  ※2.9 - 抢红包

  ※2.10 - 排座位

  ※2.11 - 玩转二叉树

  ※2.12 - 关于堆的判断

  ※2.13 - 天梯地图

  ※2.14 - 喊山

  ※3.1 - 正整数A+B

  ※3.2 - I Love GPLT

  ※3.3 - 出租

  ※3.4 - 判断素数

  ※3.5 - 是不是太胖了

  ※3.6 - 一帮一

  ※3.7 - 到底是不是太胖了

  ※3.8 - Left-pad

  ※3.9 - 红色警报

  ※3.10 - 列车调度

  ※3.11 - 互评成绩

  ※3.12 - 愿天下有情人都是失散多年的兄妹

  ※3.13 - 是否完全二叉搜索树

  ※3.14 - 直捣黄龙

  ※4.1 - 出生年

  ※4.2 - 点赞

  ※4.3 - 情人节

  ※4.4 - A乘以B

  ※4.5 - A除以B

  ※4.6 - 新世界

  ※4.7 - 古风排版

  ※4.8 - 最佳情侣身高差

  ※4.9 - 人以群分

  ※4.10 - 多项式A除以B

  ※4.11 - 悄悄关注

  ※4.12 - 功夫传人

  ※4.13 - 非常弹的球

  ※5.1 - PTA使我精神焕发

  ※5.2 - 6翻了

  ※5.3 - 敲笨钟

  ※5.4 - 心理阴影面积

  ※5.5 - 新胖子公式

  ※5.6 - 幸运彩票

  ※5.7 - 吃鱼还是吃肉

  ※5.8 - 估值一亿的AI核心代码

  ※5.9 - 特立独行的幸福

  ※5.10 - 冰岛人

  ※5.11 - 深入虎穴

  ※5.12 - 彩虹瓶

  ※5.13 - 地铁一日游

  ※5.14 - 计算图

  ※5.15 - Oriol和David

题1.1 - N个数求和

gcd实时化简

最后对答案正负再进行判断

开长整型

 1 #include<cstdio>
 2 using namespace std;
 3 typedef long long ll;
 4 ll gcd(ll a,ll b){
 5     return !b?a:gcd(b,a%b);
 6 }
 7 int main(){
 8     int N;
 9     ll a,b,up=0,down=1,real=0,lcm,d;
10     scanf("%d",&N);
11     while(N--){
12         scanf("%lld/%lld",&a,&b);
13         lcm=down/gcd(down,b)*b;
14         
15         d=lcm/down;
16         up*=d;
17         down=lcm;
18         
19         up+=lcm/b*a;
20         
21         d=gcd(up,down);
22         up/=d;
23         down/=d;
24         
25         d=up/down;
26         up%=down;
27         real+=d;
28     }
29     if(down<0)
30         up=-up,down=-down;
31     if(real){
32         if(up<0){
33             if(real>0){
34                 real--;
35                 up+=down;
36             }
37             else
38                 up=-up;
39         }
40         else if(up>0){
41             if(real<0){
42                 real++;
43                 if(!real)
44                     up=-up;
45                 up+=down;
46             }
47         }
48     }
49     if(real==0&&up==0)
50         puts("0");
51     else if(real!=0&&up==0)
52         printf("%lld\n",real);
53     else if(real!=0&&up!=0)
54         printf("%lld %lld/%lld\n",real,up,down);
55     else
56         printf("%lld/%lld\n",up,down);
57     
58     return 0;
59 }

题1.2 - 比较大小

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4     int a[3];
 5     cin>>a[0]>>a[1]>>a[2];
 6     sort(a,a+3);
 7     cout<<a[0]<<"->"<<a[1]<<"->"<<a[2]<<'\n';
 8     
 9     return 0;
10 }

题1.3 - A-B

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 char a[10050];
 4 int main(){
 5     bool b[128];
 6     fill(b,b+128,true);
 7     cin.getline(a,10050);
 8     int len=strlen(a),i;
 9     char c;
10     while((c=getchar())!='\n'&&c!=EOF)
11         b[c]=false;
12     for(i=0;i<len;i++)
13         if(b[a[i]])
14             putchar(a[i]);
15     
16     return 0;
17 }

题1.4 - 计算指数

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4     int n,ans=1;
 5     cin>>n;
 6     cout<<"2^"<<n<<" = ";
 7     while(n--)
 8         ans*=2;
 9     cout<<ans;
10     
11     return 0;
12 }

题1.5 - 计算阶乘和

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4     int n,s=0,f=1,i;
 5     cin>>n;
 6     for(i=1;i<=n;i++)
 7         f*=i,s+=f;
 8     cout<<s;
 9     
10     return 0;
11 }

题1.6 - 简单题

1 #include<bits/stdc++.h>
2 using namespace std;
3 int main(){
4     cout<<"This is a simple problem.";
5     return 0;
6 }

题1.7 - 跟奥巴马一起画方块

四舍五入

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4     int n,i,j;
 5     string s;
 6     cin>>n>>s;
 7     for(i=0;i<(n+1)/2;i++){
 8         for(j=0;j<n;j++)
 9             putchar(s[0]);
10         putchar('\n');
11     }
12     
13     return 0;
14 }

题1.8 - 查验身份证

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 vector<string> v;
 4 int ave[17]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
 5 char val[11]={'1','0','X','9','8','7','6','5','4','3','2'};
 6 void solve(){
 7     string s;
 8     cin>>s;
 9     int flag=0,i;
10     for(i=0;i<17;i++)
11         if(s[i]>='0'&&s[i]<='9')
12             flag=(flag+ave[i]*(s[i]-'0'))%11;
13         else{
14             v.push_back(s);
15             return;
16         }
17     if(s[17]!=val[flag])
18         v.push_back(s);
19 }
20 int main(){
21     ios::sync_with_stdio(0);
22     cin.tie(0);cout.tie(0);
23     int i,N;
24     cin>>N;
25     while(N--)
26         solve();
27     if(v.empty())
28         cout<<"All passed\n";
29     else
30         for(i=0;i<v.size();i++)
31             cout<<v[i]<<'\n';
32     
33     return 0;
34 }

题1.9 - 集合相似度

set去重,迭代器遍历

题意为取交集和并集的元素个数

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 set<int> s[55];
 4 set<int>::iterator it;
 5 int main(){
 6     ios::sync_with_stdio(0);
 7     cin.tie(0);cout.tie(0);
 8     int N,M,K,i,j,d,a,b;
 9     scanf("%d",&N);
10     for(i=1;i<=N;i++){
11         scanf("%d",&M);
12         for(j=0;j<M;j++){
13             scanf("%d",&d);
14             s[i].insert(d);
15         }
16     }
17     scanf("%d",&K);
18     while(K--){
19         scanf("%d%d",&a,&b);
20         if(s[a].size()>s[b].size())
21             swap(a,b);
22         int same=0,all=s[b].size();
23         for(it=s[a].begin();it!=s[a].end();it++){
24             if(s[b].find(*it)==s[b].end())
25                 all++;
26             else
27                 same++;
28         }
29         printf("%.2f%c\n",100.0*same/all,'%');
30     }
31     
32     return 0;
33 }

题1.10 - 树的遍历

建树后再bfs

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 struct node{
 4     int id,l,r;
 5 }tree[35];
 6 int LRD[35],LDR[35],cnt=0;
 7 int buildTree(int ml,int mr,int bl,int br){
 8     if(ml>mr)
 9         return 0;
10     node data;
11     data.id=LRD[br];
12     int p;
13     for(p=ml;p<=mr;p++)
14         if(LDR[p]==LRD[br])
15             break;
16     data.l=buildTree(ml,p-1,bl,bl+p-ml-1);
17     data.r=buildTree(p+1,mr,br+p-mr,br-1);
18     tree[++cnt]=data;
19     return cnt;
20 }
21 int main(){
22     ios::sync_with_stdio(0);
23     cin.tie(0);cout.tie(0);
24     int N,i;
25     cin>>N;
26     for(i=1;i<=N;i++)
27         cin>>LRD[i];
28     for(i=1;i<=N;i++)
29         cin>>LDR[i];
30     int root=buildTree(1,N,1,N);
31     vector<int> ans;
32     queue<int> q;
33     q.push(root);
34     while(!q.empty()){
35         int cur=q.front();
36         q.pop();
37         ans.push_back(tree[cur].id);
38         if(tree[cur].l)
39             q.push(tree[cur].l);
40         if(tree[cur].r)
41             q.push(tree[cur].r);
42     }
43     cout<<ans[0];
44     for(i=1;i<N;i++)
45         cout<<' '<<ans[i];
46     
47     return 0;
48 }

题1.11 - 家庭房产

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 struct node{
 5     int cnt,sum;
 6     double squ;
 7 }peo[10010];
 8 struct node2{
 9     int id,cnt,sum;
10     double squ;
11     friend bool operator < (const node2& a,const node2& b){
12         if(a.squ/a.sum!=b.squ/b.sum)
13             return a.squ/a.sum>b.squ/b.sum;
14         return a.id<b.id;
15     }
16 }ans[10010];
17 int gp[10010];
18 bool vis[10010];
19 int findp(int p){
20     return p==gp[p]?p:(gp[p]=findp(gp[p]));
21 }
22 void merge(int a,int b){
23     int gpa=findp(a),gpb=findp(b);
24     if(gpa<gpb)
25         gp[gpb]=gpa;
26     else
27         gp[gpa]=gpb;
28 }
29 void move(int from,int to){
30     peo[to].cnt+=peo[from].cnt;
31     peo[to].squ+=peo[from].squ;
32     peo[to].sum+=peo[from].sum;
33 }
34 int main(){
35     int N,i,cur,id,k,cnt;
36     for(i=0;i<10000;i++){
37         gp[i]=i;
38         peo[i].sum=1;
39     }
40     scanf("%d",&N);
41     while(N--){
42         scanf("%d",&cur);
43         vis[cur]=true;
44         for(i=0;i<2;i++){
45             scanf("%d",&id);
46             if(id==-1)
47                 continue;
48             vis[id]=true;
49             merge(cur,id);
50         }
51         scanf("%d",&k);
52         while(k--){
53             scanf("%d",&id);
54             if(id==-1)
55                 continue;
56             vis[id]=true;
57             merge(cur,id);
58         }
59         scanf("%d%lf",&peo[cur].cnt,&peo[cur].squ);
60     }
61     for(i=9999;i>=0;i--)
62         if(i!=gp[i])
63             move(i,gp[i]);
64     cnt=0;
65     for(i=0;i<10000;i++)
66         if(vis[i]&&i==gp[i]){
67             ans[cnt].id=i;
68             ans[cnt].squ=peo[i].squ;
69             ans[cnt].cnt=peo[i].cnt;
70             ans[cnt].sum=peo[i].sum;
71             cnt++;
72         }
73     sort(ans,ans+cnt);
74     printf("%d\n",cnt);
75     for(i=0;i<cnt;i++)
76         printf("%04d %d %.3f %.3f\n",ans[i].id,ans[i].sum,1.0*ans[i].cnt/ans[i].sum,ans[i].squ/ans[i].sum);
77     
78     return 0;
79 }

题1.12 - 最长对称子串

枚举中间位置左右搜索

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 char s[1010];
 4 int len;
 5 int g(int p){
 6     int l=p,r=p;
 7     while(l>=0&&r<len&&s[l]==s[r])
 8         l--,r++;
 9     return r-l-1;
10 }
11 int g2(int p){
12     int l=p,r=p+1;
13     while(l>=0&&r<len&&s[l]==s[r])
14         l--,r++;
15     return r-l-1;
16 }
17 int main(){
18     cin.getline(s,1010);
19     int i,ans=1;
20     len=strlen(s);
21     for(i=1;i<len-1;i++)
22         ans=max(ans,max(g(i),g2(i)));
23     cout<<ans<<'\n';
24     return 0;
25 }

题1.13 - 肿瘤诊断

三维bfs

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 struct d3p{
 5     int x,y,z;
 6     d3p(int a,int b,int c){
 7         x=a;
 8         y=b;
 9         z=c;
10     }
11 };
12 int M,N,L,T,px[65][1300][130],ansd;
13 int dx[6]={-1,0,1,0,0,0},dy[6]={0,0,0,0,1,-1},dz[6]={0,-1,0,1,0,0};
14 queue<d3p> q;
15 inline bool prim(int x,int y,int z){
16     return x>0&&y>0&&z>0&&x<=L&&y<=M&&z<=N;
17 }
18 void bfs(int x,int y,int z){
19     q.push(d3p(x,y,z));
20     px[x][y][z]=0;
21     ansd=1;
22     int i,xx,yy,zz;
23     while(!q.empty()){
24         d3p d=q.front();
25         q.pop();
26         for(i=0;i<6;i++){
27             xx=d.x+dx[i];
28             yy=d.y+dy[i];
29             zz=d.z+dz[i];
30             if(prim(xx,yy,zz)&&px[xx][yy][zz]){
31                 ansd++;
32                 px[xx][yy][zz]=0;
33                 q.push(d3p(xx,yy,zz));
34             }
35         }
36     }
37 }
38 int main(){
39     ios::sync_with_stdio(0);
40     cin.tie(0);cout.tie(0);
41     int i,j,k,d,ans=0;
42     cin>>M>>N>>L>>T;
43     for(i=1;i<=L;i++)
44         for(j=1;j<=M;j++)
45             for(k=1;k<=N;k++)
46                 cin>>px[i][j][k];
47     for(i=1;i<=L;i++)
48         for(j=1;j<=M;j++)
49             for(k=1;k<=N;k++)
50                 if(px[i][j][k]){
51                     bfs(i,j,k);
52                     if(ansd>=T)
53                         ans+=ansd;
54                 }
55     cout<<ans<<'\n';
56     
57     return 0;
58 }

题1.14 - 垃圾箱分布

最短路后对答案进行处理

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef pair<int,int> P;
 4 struct node{
 5     int id,min;
 6     double ave;
 7     bool operator < (const node& a) const{
 8         if(min!=a.min)
 9             return min>a.min;
10         if(ave!=a.ave)
11             return ave<a.ave;
12         return id<a.id;
13     }
14 };
15 int N,M,K,Ds,dis[1050];
16 vector<P> v[1050];
17 void sp(int st){
18     memset(dis,0x3f,sizeof dis);
19     queue<int> q;
20     q.push(st);
21     dis[st]=0;
22     int d,cnt,i,ddis;
23     while(!q.empty()){
24         d=q.front();
25         q.pop();
26         cnt=v[d].size();
27         for(i=0;i<cnt;i++){
28             ddis=dis[d]+v[d][i].second;
29             if(dis[v[d][i].first]>ddis){
30                 dis[v[d][i].first]=ddis;
31                 q.push(v[d][i].first);
32             }
33         }
34     }
35 }
36 int main(){
37     ios::sync_with_stdio(0);
38     cin.tie(0);cout.tie(0);
39     cin>>N>>M>>K>>Ds;
40     string s1,s2;
41     int i,j,a,b,d,mindis;
42     long long sumdis;
43     vector<node> ans;
44     while(K--){
45         cin>>s1>>s2>>d;
46         if(s1[0]=='G'){
47             s1=s1.substr(1);
48             a=stoi(s1)+N;
49         }
50         else
51             a=stoi(s1);
52         if(s2[0]=='G'){
53             s2=s2.substr(1);
54             b=stoi(s2)+N;
55         }
56         else
57             b=stoi(s2);
58         v[a].push_back(P(b,d));
59         v[b].push_back(P(a,d));
60     }
61     for(i=N+1;i<=N+M;i++){
62         sp(i);
63         mindis=0x7fffffff;
64         sumdis=0;
65         for(j=1;j<=N;j++){
66             if(dis[j]>Ds){
67                 mindis=-1;
68                 break;
69             }
70             mindis=min(mindis,dis[j]);
71             sumdis+=dis[j];
72         }
73         if(mindis!=-1){
74             node nd;
75             nd.id=i-N;
76             nd.min=mindis;
77             nd.ave=1.0*sumdis/N;
78             ans.push_back(nd);
79         }
80     }
81     if(ans.size()){
82         sort(ans.begin(),ans.end());
83         printf("G%d\n%d.0 %.1f",ans[0].id,ans[0].min,ans[0].ave);
84     }
85     else
86         cout<<"No Solution";
87     
88     return 0;
89 }

题2.1 - 到底有多二

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4     string s;
 5     cin>>s;
 6     int i,len=s.length(),num=0;
 7     double deep=100.0;
 8     if(s[0]=='-'){
 9         deep=150.0;
10         s=s.substr(1);
11         len--;
12     }
13     for(i=0;i<len;i++)
14         if(s[i]=='2')
15             num++;
16     if((s[len-1]-'0')%2==0)
17         deep*=2;
18     cout<<fixed<<setprecision(2)<<deep*num/len<<"%\n";
19     
20     return 0;
21 }

题2.2 - 大笨钟

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4     int h,m,i;
 5     scanf("%d:%d",&h,&m);
 6     if(h>=0&&h<12||h==12&&m==0)
 7         printf("Only %02d:%02d.  Too early to Dang.",h,m);
 8     else{
 9         if(m)
10             h++;
11         h-=12;
12         for(i=0;i<h;i++)
13             printf("Dang");
14     }
15     
16     return 0;
17 }

题2.3 - 谁先倒

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4     ios::sync_with_stdio(0);
 5     cin.tie(0);cout.tie(0);
 6     int A,B,N,AA=0,BB=0,a,b,c,d,e;
 7     cin>>A>>B>>N;
 8     while(N--){
 9         cin>>a>>b>>c>>d;
10         e=a+c;
11         if((b==e)==(d==e))
12             continue;
13         if(b==e)
14             AA++;
15         else
16             BB++;
17         if(AA>A){
18             cout<<"A\n"<<BB<<'\n';
19             break;
20         }
21         else if(BB>B){
22             cout<<"B\n"<<AA<<'\n';
23             break;
24         }
25     }
26     
27     return 0;
28 }

题2.4 - 帅到没朋友

没朋友的只可能没出现,或者单独成组

所以取每个人最大朋友圈的人数,取那些人数为0和1的即可

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int m[100010];
 4 set<int> s;
 5 vector<int> ans;
 6 int main(){
 7     ios::sync_with_stdio(0);
 8     cin.tie(0);cout.tie(0);
 9     int N,M,K,i,d;
10     cin>>N;
11     while(N--){
12         cin>>K;
13         for(i=0;i<K;i++){
14             cin>>d;
15             m[d]=max(m[d],K);
16         }
17     }
18     cin>>M;
19     while(M--){
20         cin>>d;
21         if(s.find(d)==s.end()){
22             s.insert(d);
23             if(m[d]<=1)
24                 ans.push_back(d);
25         }
26     }
27     if(ans.size())
28         for(i=0;i<ans.size();i++)
29             printf("%05d%c",ans[i],((i==ans.size()-1)?'\n':' '));
30     else
31         puts("No one is handsome");
32     
33     return 0;
34 }

题2.5 - 重要的话说三遍

1 #include<bits/stdc++.h>
2 using namespace std;
3 int main(){
4     string s="I'm gonna WIN!";
5     for(int i=0;i<3;i++)
6         cout<<s<<'\n';
7     
8     return 0;
9 }

题2.6 - 奇偶分家

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4     int N,a=0,b=0,d;
 5     cin>>N;
 6     while(N--){
 7         cin>>d;
 8         if(d%2)
 9             a++;
10         else
11             b++;
12     }
13     cout<<a<<' '<<b;
14     
15     return 0;
16 }

题2.7 - 输出GPLT

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4     string s;
 5     cin>>s;
 6     int len=s.length(),i,d=0,r[4]={0};
 7     for(i=0;i<len;i++)
 8         if(s[i]=='g'||s[i]=='G')
 9             r[0]++,d++;
10         else if(s[i]=='p'||s[i]=='P')
11             r[1]++,d++;
12         else if(s[i]=='l'||s[i]=='L')
13             r[2]++,d++;
14         else if(s[i]=='t'||s[i]=='T')
15             r[3]++,d++;
16     for(i=0;d;i=(i+1)%4)
17         if(r[i]){
18             r[i]--;
19             d--;
20             if(i==0)
21                 putchar('G');
22             else if(i==1)
23                 putchar('P');
24             else if(i==2)
25                 putchar('L');
26             else
27                 putchar('T');
28         }
29     
30     return 0;
31 }

题2.8 - 后天

1 #include<bits/stdc++.h>
2 using namespace std;
3 int main(){
4     int d;
5     cin>>d;
6     cout<<(d+1)%7+1;
7     
8     return 0;
9 }

题2.9 - 抢红包

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 struct node{
 4     int id,cnt,sum;
 5 }ar[10010];
 6 bool cmp(node a,node b){
 7     if(a.sum!=b.sum)
 8         return a.sum>b.sum;
 9     if(a.cnt!=b.cnt)
10         return a.cnt>b.cnt;
11     return a.id<b.id;
12 }
13 int main(){
14     int N,K,a,b,i,j;
15     scanf("%d",&N);
16     for(i=1;i<=N;i++){
17         ar[i].id=i;
18         scanf("%d",&K);
19         while(K--){
20             scanf("%d%d",&a,&b);
21             ar[a].cnt++;
22             ar[a].sum+=b;
23             ar[i].sum-=b;
24         }
25     }
26     sort(ar+1,ar+1+N,cmp);
27     for(i=1;i<=N;i++)
28         printf("%d %.2f\n",ar[i].id,0.01*ar[i].sum);
29     
30     return 0;
31 }

题2.10 - 排座位

朋友使用并查集,敌人直接判断

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int gp[110];
 4 bool fight[110][110];
 5 int findp(int p){
 6     return p==gp[p]?p:(gp[p]=findp(gp[p]));
 7 }
 8 void merge(int a,int b){
 9     gp[findp(a)]=findp(b);
10 }
11 int main(){
12     int N,M,K,i,a,b,d;
13     scanf("%d%d%d",&N,&M,&K);
14     for(i=1;i<=N;i++)
15         gp[i]=i;
16     while(M--){
17         cin>>a>>b>>d;
18         if(d==1)
19             merge(a,b);
20         else
21             fight[a][b]=fight[b][a]=true;
22     }
23     while(K--){
24         cin>>a>>b;
25         bool f=fight[a][b];
26         a=findp(a);
27         b=findp(b);
28         if(a==b&&f)
29             puts("OK but...");
30         else if(a==b&&!f)
31             puts("No problem");
32         else if(a!=b&&f)
33             puts("No way");
34         else
35             puts("OK");
36     }
37     
38     return 0;
39 }

题2.11 - 玩转二叉树

题类似于1.10,翻转可以看成先查找右儿子再查找左儿子

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 struct node{
 4     int id,l,r;
 5 }tree[35];
 6 int DLR[35],LDR[35],cnt=0;
 7 int buildTree(int ml,int mr,int al,int ar){
 8     if(ml>mr)
 9         return 0;
10     node data;
11     data.id=DLR[al];
12     int p;
13     for(p=ml;p<=mr;p++)
14         if(LDR[p]==DLR[al])
15             break;
16     data.l=buildTree(ml,p-1,al+1,al+p-ml);
17     data.r=buildTree(p+1,mr,ar+p-mr+1,ar);
18     tree[++cnt]=data;
19     return cnt;
20 }
21 int main(){
22     ios::sync_with_stdio(0);
23     cin.tie(0);cout.tie(0);
24     int N,i;
25     cin>>N;
26     for(i=1;i<=N;i++)
27         cin>>LDR[i];
28     for(i=1;i<=N;i++)
29         cin>>DLR[i];
30     int root=buildTree(1,N,1,N);
31     vector<int> ans;
32     queue<int> q;
33     q.push(root);
34     while(!q.empty()){
35         int cur=q.front();
36         q.pop();
37         ans.push_back(tree[cur].id);
38         if(tree[cur].r)
39             q.push(tree[cur].r);
40         if(tree[cur].l)
41             q.push(tree[cur].l);
42     }
43     cout<<ans[0];
44     for(i=1;i<N;i++)
45         cout<<' '<<ans[i];
46     
47     return 0;
48 }

题2.12 - 关于堆的判断

建堆判断,关于父节点和子节点不需要追溯,取相邻即可……

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int N,M,tree[1010];
 4 void insertNode(int p){
 5     while(tree[p]<tree[p/2]&&p!=1){
 6         swap(tree[p],tree[p/2]);
 7         p/=2;
 8     }
 9 }
10 int findp(int nodeval){
11     for(int i=1;i<=N;i++)
12         if(tree[i]==nodeval)
13             return i;
14 }
15 void isroot(int nodev){
16     cout<<(nodev==tree[1]?"T":"F")<<'\n';
17 }
18 void issiblings(int nodev1,int nodev2){
19     int p1=findp(nodev1),p2=findp(nodev2);
20     if(p1/2==p2/2)
21         cout<<"T\n";
22     else
23         cout<<"F\n";
24 }
25 void isparent(int nodepar,int nodechd){
26     int p=findp(nodechd);
27     if(tree[p/2]==nodepar)
28         cout<<"T\n";
29     else
30         cout<<"F\n";
31 }
32 int main(){
33     ios::sync_with_stdio(0);
34     cin.tie(0);cout.tie(0);
35     int i,d,id1,id2;
36     string sd;
37     cin>>N>>M>>tree[1];
38     for(i=2;i<=N;i++){
39         cin>>tree[i];
40         insertNode(i);
41     }
42     while(M--){
43         cin>>id1>>sd;
44         if(sd=="and"){
45             cin>>id2>>sd>>sd;
46             issiblings(id1,id2);
47         }
48         else{
49             cin>>sd;
50             if(sd=="a"){
51                 cin>>sd>>sd>>id2;
52                 isparent(id2,id1);
53             }
54             else{
55                 cin>>sd;
56                 if(sd=="root")
57                     isroot(id1);
58                 else{
59                     cin>>sd>>id2;
60                     isparent(id1,id2);
61                 }
62             }
63         }
64     }
65     
66     return 0;
67 }

题2.13 - 天梯地图

最短路+路径记录

用string和vector动态记录一条路径和走这条路时的最短路长度,最后再处理

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 typedef pair<int,int> P;
  4 typedef pair<int,string> PT;
  5 int ST,ED,dis[505],tim[505],tmpdis[505][505];
  6 vector<P> vdis[505],vtim[505];
  7 string sdis[505],stim[505],finaltimstr,finaldisstr;
  8 vector<PT> vsdis,vstim;
  9 queue<int> q;
 10 int getDistance(string in){
 11     int i,len=in.size(),tmp=0,res=0,from,to;
 12     bool flag=false,flag2=false;
 13     for(i=0;i<len;i++){
 14         if(in[i]>='0'&&in[i]<='9'){
 15             flag=true;
 16             tmp=tmp*10+in[i]-'0';
 17         }
 18         else{
 19             if(flag){
 20                 flag=false;
 21                 if(!flag2){
 22                     flag2=true;
 23                     to=tmp;
 24                 }
 25                 else{
 26                     from=to;
 27                     to=tmp;
 28                     res+=tmpdis[from][to];
 29                 }
 30                 tmp=0;
 31             }
 32         }
 33     }
 34     return res;
 35 }
 36 int getNodeCnt(string in){
 37     int res=1,i,len=in.size();
 38     for(i=0;i<len;i++)
 39         if(in[i]=='=')
 40             res++;
 41     return res;
 42 }
 43 void dealAns(){
 44     int mn,cnt,tmp,i,cur;
 45     cnt=vstim.size();
 46     mn=0x3f3f3f3f;
 47     for(i=0;i<cnt;i++){
 48         if(vstim[i].first!=tim[ED])
 49             continue;
 50         tmp=getDistance(vstim[i].second);
 51         if(tmp<mn){
 52             mn=tmp;
 53             cur=i;
 54         }
 55     }
 56     finaltimstr=vstim[cur].second;
 57     cnt=vsdis.size();
 58     mn=999;
 59     for(i=0;i<cnt;i++){
 60         if(vsdis[i].first!=dis[ED])
 61             continue;
 62         tmp=getNodeCnt(vsdis[i].second);
 63         if(tmp<mn){
 64             mn=tmp;
 65             cur=i;
 66         }
 67     }
 68     finaldisstr=vsdis[cur].second;
 69 }
 70 void deal(){
 71     memset(dis,0x3f,sizeof dis);
 72     memset(tim,0x3f,sizeof tim);
 73     dis[ST]=tim[ST]=0;
 74     sdis[ST]=stim[ST]=to_string(ST);
 75     q.push(ST);
 76     while(!q.empty()){
 77         int cur=q.front(),cnt,tmp,p,i;
 78         q.pop();
 79         cnt=vdis[cur].size();
 80         for(i=0;i<cnt;i++){
 81             tmp=dis[cur]+vdis[cur][i].second;
 82             p=vdis[cur][i].first;
 83             if(dis[p]>=tmp){//必须要等于号
 84                 dis[p]=tmp;
 85                 sdis[p]=sdis[cur]+" => "+to_string(p);
 86                 if(p==ED)
 87                     vsdis.push_back(PT(dis[p],sdis[p]));
 88                 else
 89                     q.push(p);
 90             }
 91         }
 92     }
 93     q.push(ST);
 94     while(!q.empty()){
 95         int cur=q.front(),cnt,tmp,p,i;
 96         q.pop();
 97         cnt=vtim[cur].size();
 98         for(i=0;i<cnt;i++){
 99             tmp=tim[cur]+vtim[cur][i].second;
100             p=vtim[cur][i].first;
101             if(tim[p]>=tmp){
102                 tim[p]=tmp;
103                 stim[p]=stim[cur]+" => "+to_string(p);
104                 if(p==ED)
105                     vstim.push_back(PT(tim[p],stim[p]));
106                 else
107                     q.push(p);
108             }
109         }
110     }
111 }
112 int main(){
113     ios::sync_with_stdio(0);
114     cin.tie(0);cout.tie(0);
115     int N,M,V1,V2,OW,LL,TT;
116     memset(tmpdis,0x3f,sizeof tmpdis);
117     cin>>N>>M;
118     while(M--){
119         cin>>V1>>V2>>OW>>LL>>TT;
120         vdis[V1].push_back(P(V2,LL));
121         tmpdis[V1][V2]=min(tmpdis[V1][V2],LL);
122         vtim[V1].push_back(P(V2,TT));
123         if(!OW){
124             vdis[V2].push_back(P(V1,LL));
125             tmpdis[V2][V1]=min(tmpdis[V2][V1],LL);
126             vtim[V2].push_back(P(V1,TT));
127         }
128     }
129     cin>>ST>>ED;
130     deal();
131     dealAns();
132     if(finaldisstr!=finaltimstr)
133         cout<<"Time = "<<tim[ED]<<": "<<finaltimstr<<"\nDistance = "<<dis[ED]<<": "<<finaldisstr;
134     else
135         cout<<"Time = "<<tim[ED]<<"; Distance = "<<dis[ED]<<": "<<finaldisstr;
136     
137     return 0;
138 }

题2.14 - 喊山

bfs

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 vector<int> v[10010];
 4 queue<int> q;
 5 int n,dis[10010];
 6 int bfs(int st){
 7     int res=0,cur,cnt,i,d;
 8     memset(dis,0x3f,sizeof dis);
 9     dis[st]=0;
10     q.push(st);
11     while(!q.empty()){
12         cur=q.front();
13         q.pop();
14         cnt=v[cur].size();
15         for(i=0;i<cnt;i++)
16             if(dis[cur]+1<dis[v[cur][i]]){
17                 dis[v[cur][i]]=dis[cur]+1;
18                 q.push(v[cur][i]);
19                 res=max(res,dis[v[cur][i]]);
20             }
21     }
22     if(!res)
23         return 0;
24     for(i=1;i<=n;i++)
25         if(dis[i]==res)
26             return i;
27 }
28 int main(){
29     ios::sync_with_stdio(0);
30     cin.tie(0);cout.tie(0);
31     int m,k,i,a,b;
32     cin>>n>>m>>k;
33     while(m--){
34         cin>>a>>b;
35         v[a].push_back(b);
36         v[b].push_back(a);
37     }
38     while(k--){
39         cin>>a;
40         cout<<bfs(a)<<'\n';
41     }
42     
43     return 0;
44 }

题3.1 - 正整数A+B

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 char s[10000];
 4 int main(){
 5     int i,len,a=0,b=0;
 6     cin.getline(s,10000);
 7     len=strlen(s);
 8     bool flag=false,p1=true,p2=true;
 9     for(i=0;i<len;i++){
10         if(s[i]==' '){
11             flag=true;
12             continue;
13         }
14         if(!flag&&p1){
15             if(s[i]>='0'&&s[i]<='9'){
16                 a=a*10+s[i]-'0';
17                 if(a>1000)
18                     p1=false;
19             }
20             else
21                 p1=false;
22         }
23         else if(flag&&p2){
24             if(s[i]>='0'&&s[i]<='9'){
25                 b=b*10+s[i]-'0';
26                 if(b>1000)
27                     p2=false;
28             }
29             else
30                 p2=false;
31         }
32     }
33     if(!a)
34         p1=false;
35     if(!b)
36         p2=false;
37     if(p1){
38         if(p2)
39             cout<<a<<" + "<<b<<" = "<<a+b;
40         else
41             cout<<a<<" + ? = ?";
42     }
43     else{
44         if(p2)
45             cout<<"? + "<<b<<" = ?";
46         else
47             cout<<"? + ? = ?";
48     }
49     
50     return 0;
51 }

题3.2 - I Love GPLT

1 #include<bits/stdc++.h>
2 using namespace std;
3 int main(){
4     string s="I Love GPLT";
5     for(int i=0;i<11;i++)
6         cout<<s[i]<<'\n';
7     
8     return 0;
9 }

题3.3 - 出租

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4     ios::sync_with_stdio(0);
 5     cin.tie(0);cout.tie(0);
 6     int i,cnt=0,to[10];
 7     bool vis[10];
 8     string s;
 9     cin>>s;
10     fill(vis,vis+10,false);
11     for(i=0;i<11;i++)
12         vis[s[i]-'0']=true;
13     cout<<"int[] arr = new int[]{";
14     for(i=9;i>=0;i--)
15         if(vis[i]){
16             to[i]=cnt;
17             if(!cnt)
18                 cout<<i;
19             else
20                 cout<<','<<i;
21             cnt++;
22         }
23     cout<<"};\nint[] index = new int[]{"<<to[s[0]-'0'];
24     for(i=1;i<11;i++)
25         cout<<','<<to[s[i]-'0'];
26     cout<<"};";
27     
28     return 0;
29 }

题3.4 - 判断素数

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 void solve(){
 4     int n,d,i;
 5     cin>>n;
 6     if(n==1){
 7         cout<<"No\n";
 8         return;
 9     }
10     d=sqrt(n);
11     for(i=2;i<=d;i++)
12         if(n%i==0){
13             cout<<"No\n";
14             return;
15         }
16     cout<<"Yes\n";
17 }
18 int main(){
19     ios::sync_with_stdio(0);
20     cin.tie(0);cout.tie(0);
21     int N;cin>>N;while(N--)
22         solve();
23     
24     return 0;
25 }

题3.5 - 是不是太胖了

1 #include<bits/stdc++.h>
2 using namespace std;
3 int main(){
4     double H;
5     scanf("%lf",&H);
6     printf("%.1f",(H-100)*1.8);
7     
8     return 0;
9 }

题3.6 - 一帮一

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef pair<int,string> P;
 4 P r[55];
 5 bool vis[55];
 6 int main(){
 7     ios::sync_with_stdio(0);
 8     cin.tie(0);cout.tie(0);
 9     int N,d,i,j;
10     string name;
11     cin>>N;
12     for(i=1;i<=N;i++){
13         cin>>d>>name;
14         r[i]=P(d,name);
15     }
16     for(d=0;d<N;d+=2){
17         for(i=1;i<=N;i++)
18             if(!vis[i])
19                 break;
20         for(j=N;j;j--)
21             if(!vis[j]&&r[i].first!=r[j].first)
22                 break;
23         vis[i]=vis[j]=true;
24         cout<<r[i].second<<' '<<r[j].second<<'\n';
25     }
26     
27     return 0;
28 }

题3.7 - 到底是不是太胖了

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4     ios::sync_with_stdio(0);
 5     cin.tie(0);cout.tie(0);
 6     int N;
 7     double H,W,dW;
 8     cin>>N;
 9     while(N--){
10         cin>>H>>W;
11         dW=(H-100)*1.8;
12         if(fabs(W-dW)<0.1*dW)
13             cout<<"You are wan mei!\n";
14         else if(W-dW>0)
15             cout<<"You are tai pang le!\n";
16         else
17             cout<<"You are tai shou le!\n";
18     }
19     
20     return 0;
21 }

题3.8 - Left-pad

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 string ans,s;
 4 int main(){
 5     char c,d;
 6     int N=0,i,len;
 7     while((c=getchar())!=' ')
 8         N=N*10+c-'0';
 9     d=getchar();
10     getchar();
11     while((c=getchar())!='\n')
12         s+=c;
13     len=s.size();
14     for(i=len-1;i>=0&&N;i--,N--)
15         ans=s[i]+ans;
16     while(N--)
17         ans=d+ans;
18     cout<<ans;
19     
20     return 0;
21 }

题3.9 - 红色警报

考察并查集

输入时开始建图,保存所有路径,然后开exist数组来表示哪些城市失去了,哪些城市还在,初始化为true,即都还在

第一次建图完毕后,寻找这个图中的连通块数量储存在cnt中(即并查集根节点等于自己的数量)

其后每一次失去一个城市,就把exist标记为false,初始化重新建图

重新建图时忽略已经失去城市的那些边,其他的边把两边的城市继续并查集合并

然后重新寻找连通块数量储存在cntd中

这里把已经失去的城市也当作一个块

因为只有在改变了其他城市之间的连通性时才会发出红色警报

如果国家格局没有被改变,说明此时连通块数量等于上一次连通块数量,即cntd==cnt

  或者如果原本一个连通块中只存在两个城市,这一次失去了二者其中之一,那么此时连通块数量会增加1,但是其他城市连通性并没有被改变,所以此时cntd==cnt+1

  再或者如果只有一个城市处在一个连通块中,这一次这个城市失去了,也不改变其他城市连通性,此时对于算法而言失去与不失去连通块数量都是一样,即cntd==cnt

综上,只有当前连通块数量等于前一次连通块数量或者等于前一次+1时,正常输出,否则红色警报

(注意正常输出后面时一个点红色警报后面是感叹号!!!!!)

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 vector<int> v[510];
 4 int gp[510];
 5 bool exist[510];
 6 int findp(int p){
 7     return p==gp[p]?p:(gp[p]=findp(gp[p]));
 8 }
 9 void merge(int a,int b){
10     gp[findp(a)]=findp(b);
11 }
12 int main(){
13     ios::sync_with_stdio(0);
14     cin.tie(0);cout.tie(0);
15     int N,M,K,i,j,k,a,b,id,len,cnt,cntd;
16     cin>>N>>M;
17     memset(exist,true,sizeof exist);
18     for(i=0;i<N;i++)
19         gp[i]=i;
20     for(i=0;i<M;i++){
21         cin>>a>>b;
22         v[a].push_back(b);
23         merge(a,b);
24     }
25     for(cnt=i=0;i<N;i++)
26         if(gp[i]==i)
27             cnt++;
28     cin>>K;
29     for(k=1;k<=K;k++){
30         cin>>id;
31         exist[id]=false;
32         for(i=0;i<N;i++)
33             gp[i]=i;
34         for(i=0;i<N;i++)
35             if(exist[i]){
36                 len=v[i].size();
37                 for(j=0;j<len;j++)
38                     if(exist[v[i][j]])
39                         merge(i,v[i][j]);
40             }
41         for(cntd=i=0;i<N;i++)
42             if(gp[i]==i)
43                 cntd++;
44         if(cntd==cnt||cntd==cnt+1)
45             cout<<"City "<<id<<" is lost.\n";
46         else
47             cout<<"Red Alert: City "<<id<<" is lost!\n";
48         cnt=cntd;
49         if(N==k)
50             cout<<"Game Over.\n";
51     }
52     
53     return 0;
54 }

题3.10 - 列车调度

因为输出是要按照从大到小输出,所以每条轨道上的车从前往后也都需要按照从大到小排序

我们每次贪心可以得知,为了能让后面加入的车能有更好的放置条件,所以当前车需要放在大于它的id的最小的id的那辆车后面

使用ar数组记录每条轨道最后一辆车的id,cnt记录当前有多少条轨道

又因为如果要加入的车的id大于所有的轨道最后一辆车的id时

就需要多开一条轨道放车了

可以发现轨道从1到cnt,最后一辆车的id是递增的

所以判断是否大于所有轨道最后一辆车id时,实际上只需要对此时的cnt指向的轨道最后一辆车对比就行

如果比这辆车的id小,说明前面有可以放这辆车的轨道

可以二分查找大于当前车id的最小id车所在轨道p(代码直接使用了upper_bound)

然后更新ar[p]=id即可

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=100050;
 4 int ar[maxn];
 5 int main(){
 6     ios::sync_with_stdio(0);
 7     cin.tie(0);cout.tie(0);
 8     int N,i,id,cnt=0,p;
 9     cin>>N;
10     fill(ar,ar+maxn,maxn);
11     ar[0]=0;
12     for(i=0;i<N;i++){
13         cin>>id;
14         if(id>ar[cnt])
15             ar[++cnt]=id;
16         else{
17             p=upper_bound(ar+1,ar+cnt+1,id)-ar;
18             ar[p]=id;
19         }
20     }
21     cout<<cnt<<'\n';
22     
23     return 0;
24 }

题3.11 - 互评成绩

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 vector<double> ans;
 4 int main(){
 5     int N,K,M,i,j,sum,d,mn,mx;
 6     scanf("%d%d%d",&N,&K,&M);
 7     for(i=0;i<N;i++){
 8         sum=0;
 9         mn=100;
10         mx=0;
11         for(j=0;j<K;j++){
12             scanf("%d",&d);
13             sum+=d;
14             mn=min(mn,d);
15             mx=max(mx,d);
16         }
17         sum-=mn+mx;
18         ans.push_back(1.0*sum/(K-2));
19     }
20     sort(ans.begin(),ans.end());
21     printf("%.3f",ans[N-M]);
22     for(i=N-M+1;i<N;i++)
23         printf(" %.3f",ans[i]);
24     
25     return 0;
26 }

题3.12 - 愿天下有情人都是失散多年的兄妹

搜索只需要开一个vis数组记录是否被访问过,每次初始化为false

然后先dfs其中一人的五代

再dfs另外一人的五代,在过程中如果出现了vis=true的情况则说明五代内有亲属

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 struct node{
 4     char xb;
 5     int f,m;
 6 }ar[100010];
 7 bool vis[100010],flag;
 8 void dfs(int id,int bf){
 9     if(bf>=5||!flag)
10         return;
11     if(ar[id].f!=-1){
12         if(vis[ar[id].f]){
13             flag=false;
14             return;
15         }
16         vis[ar[id].f]=true;
17         dfs(ar[id].f,bf+1);
18     }
19     if(ar[id].m!=-1){
20         if(vis[ar[id].m]){
21             flag=false;
22             return;
23         }
24         vis[ar[id].m]=true;
25         dfs(ar[id].m,bf+1);
26     }
27 }
28 void solve(){
29     int id1,id2;
30     cin>>id1>>id2;
31     if(ar[id1].xb==ar[id2].xb){
32         cout<<"Never Mind\n";
33         return;
34     }
35     memset(vis,false,sizeof vis);
36     flag=true;
37     dfs(id1,1);
38     dfs(id2,1);
39     cout<<(flag?"Yes\n":"No\n");
40 }
41 int main(){
42     ios::sync_with_stdio(0);
43     cin.tie(0);cout.tie(0);
44     int N,K,i,id,fah,moh;
45     string s;
46     for(i=0;i<100000;i++)
47         ar[i].f=ar[i].m=-1;
48     cin>>N;
49     while(N--){
50         cin>>id>>s>>fah>>moh;
51         ar[id].xb=s[0];
52         if(fah!=-1){
53             ar[id].f=fah;
54             ar[fah].xb='M';
55         }
56         if(moh!=-1){
57             ar[id].m=moh;
58             ar[moh].xb='F';
59         }
60     }
61     cin>>K;
62     while(K--)
63         solve();
64     
65     return 0;
66 }

题3.13 - 是否完全二叉搜索树

建树后直接bfs层序遍历,然后借助id的连续性判断是否完全二叉树

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int tree[100];
 4 void build(int p,int val){
 5     if(!tree[p]){
 6         tree[p]=val;
 7         return;
 8     }
 9     if(val>tree[p])
10         build(2*p,val);
11     else
12         build(2*p+1,val);
13 }
14 int main(){
15     ios::sync_with_stdio(0);
16     cin.tie(0);cout.tie(0);
17     int N,i,val,cur;
18     memset(tree,0,sizeof tree);
19     cin>>N;
20     for(i=1;i<=N;i++){
21         cin>>val;
22         build(1,val);
23     }
24     queue<int> q;
25     vector<int> ans;
26     q.push(1);
27     while(!q.empty()){
28         cur=q.front();
29         q.pop();
30         ans.push_back(tree[cur]);
31         if(tree[cur*2])
32             q.push(cur*2);
33         if(tree[cur*2+1])
34             q.push(cur*2+1);
35     }
36     cout<<ans[0];
37     for(i=1;i<N;i++)
38         cout<<' '<<ans[i];
39     cout<<'\n';
40     bool flag=true;
41     for(i=1;i<=N;i++)
42         if(!tree[i]){
43             flag=false;
44             break;
45         }
46     cout<<(flag?"YES":"NO");
47     
48     return 0;
49 }

题3.14 - 直捣黄龙

给每个地点字符串定义一个编号

用id这个map映射这个编号

city数组用编号取字符串

eid表示敌人大本营代表的编号(EnemyID)

ecnt数组存每个地点敌军士兵的数量(EnemyCount)

dis存最短路值(最快时间)

cycnt数组存在取最短路的前提下所能够解放的城市最大数量(CityCount)

kcnt数组存在取最短路并且解放城市数量最大的前提下所能够杀的敌方士兵数量(KillCount)

pathcnt数组存在取最短路的前提下相同最短路方案数(PathCount)

path数组存到某个城市的最优解的格式类似答案的字符串

然后跑dijkstra最短路,套条件求出最佳答案即可

另,本题最好不要用宽搜求最短路,方案数重叠难处理

坑:注意第二行输出的第一个值,是指取到最短路时最短路路径有多少种方案,此时不需要限制其他条件

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int INF=0x3f3f3f3f;
 4 int N,eid,ecnt[210],dis[210],cycnt[210],kcnt[210],pathcnt[210];
 5 string ally,enemy,city[210],path[210];
 6 map<string,int> id;
 7 int road[210][210];
 8 bool vis[210];
 9 void dijkstra(){
10     memset(vis,false,sizeof vis);
11     memset(kcnt,0,sizeof kcnt);
12     memset(cycnt,0,sizeof cycnt);
13     memset(pathcnt,0,sizeof pathcnt);
14     fill(path,path+210,ally);
15     dis[0]=0;
16     pathcnt[0]=1;//最开始方案数为1
17     vis[0]=true;
18     int i,j,mn,p,ddis,dcycnt,dkill;
19     string dpath;
20     for(i=1;i<N;i++){
21         dis[i]=road[0][i];
22         if(dis[i]!=INF){
23             pathcnt[i]=1;
24             cycnt[i]=1;
25             path[i]=path[0]+"->"+city[i];
26             kcnt[i]=ecnt[i];
27         }
28     }
29     for(i=1;i<N;i++){
30         mn=INF;
31         for(j=0;j<N;j++)
32             if(!vis[j]&&dis[j]<mn){
33                 mn=dis[j];
34                 p=j;
35             }
36         vis[p]=true;
37         for(j=0;j<N;j++)
38             if(road[p][j]!=INF){
39                 ddis=dis[p]+road[p][j];
40                 dcycnt=cycnt[p]+1;
41                 dpath=path[p]+"->"+city[j];
42                 dkill=kcnt[p]+ecnt[j];
43                 if(dis[j]>ddis){
44                     pathcnt[j]=pathcnt[p];//此时最短路的答案被更新了,所以方案数直接继承自p
45                     dis[j]=ddis;
46                     cycnt[j]=dcycnt;
47                     path[j]=dpath;
48                     kcnt[j]=dkill;
49                 }
50                 else if(dis[j]==ddis){
51                     pathcnt[j]+=pathcnt[p];//此时最短路的答案没有被更新,说明并列最短路,方案数加上p
52                     if(cycnt[j]<dcycnt){
53                         path[j]=dpath;
54                         cycnt[j]=dcycnt;
55                         kcnt[j]=dkill;
56                     }
57                     else if(cycnt[j]==dcycnt){
58                         if(kcnt[j]<dkill){
59                             path[j]=dpath;
60                             kcnt[j]=dkill;
61                         }
62                     }    
63                 }
64             }
65     }
66 }
67 int main(){
68     ios::sync_with_stdio(0);
69     cin.tie(0);cout.tie(0);
70     int K,i,d,id1,id2;
71     string s1,s2;
72     cin>>N>>K>>ally>>enemy;
73     id[ally]=0;
74     city[0]=ally;
75     for(i=1;i<N;i++){
76         cin>>s1>>ecnt[i];
77         id[s1]=i;
78         city[i]=s1;
79     }
80     eid=id[enemy];
81     memset(road,INF,sizeof road);
82     while(K--){
83         cin>>s1>>s2>>d;
84         id1=id[s1];
85         id2=id[s2];
86         road[id1][id2]=road[id2][id1]=d;
87     }
88     dijkstra();
89     cout<<path[eid]<<'\n'<<pathcnt[eid]<<' '<<dis[eid]<<' '<<kcnt[eid]<<'\n';
90     
91     return 0;
92 }

题4.1 - 出生年

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int num(int in){
 4     int i,d[10]={0},r=0;
 5     for(i=0;i<4;i++){
 6         d[in%10]=1;
 7         in/=10;
 8     }
 9     for(i=0;i<10;i++)
10         r+=d[i];
11     return r;
12 }
13 void solve(){
14     int x,y,n;
15     cin>>x>>n;
16     y=x;
17     while(num(y)!=n)
18         y++;
19     cout<<y-x<<' '<<setw(4)<<setfill('0')<<y<<'\n';
20 }
21 int main(){
22     ios::sync_with_stdio(0);
23     cin.tie(0);cout.tie(0);
24     solve();
25     
26     return 0;
27 }

题4.2 - 点赞

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 struct node{
 4     int cnt,id;
 5     bool operator < (const node& a) const{
 6         if(cnt!=a.cnt)
 7             return cnt>a.cnt;
 8         return id>a.id;
 9     }
10 }ar[1010];
11 void solve(){
12     int N,K,d,i;
13     cin>>N;
14     for(i=1;i<1001;i++)
15         ar[i].id=i;
16     while(N--){
17         cin>>K;
18         while(K--){
19             cin>>d;
20             ar[d].cnt++;
21         }
22     }
23     sort(ar+1,ar+1001);
24     cout<<ar[1].id<<' '<<ar[1].cnt<<'\n';
25 }
26 int main(){
27     ios::sync_with_stdio(0);
28     cin.tie(0);cout.tie(0);
29     solve();
30     
31     return 0;
32 }

题4.3 - 情人节

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 void solve(){
 4     int cnt=0;
 5     string s1,s2,s;
 6     while(cin>>s){
 7         if(s==".")
 8             break;
 9         cnt++;
10         if(cnt==2)
11             s1=s;
12         else if(cnt==14)
13             s2=s;
14     }
15     if(cnt<2)
16         cout<<"Momo... No one is for you ...\n";
17     else if(cnt<14)
18         cout<<s1<<" is the only one for you...\n";
19     else
20         cout<<s1<<" and "<<s2<<" are inviting you to dinner...\n";
21 }
22 int main(){
23     ios::sync_with_stdio(0);
24     cin.tie(0);cout.tie(0);
25     solve();
26     
27     return 0;
28 }

题4.4 - A乘以B

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 void solve(){
 4     int a,b;
 5     cin>>a>>b;
 6     cout<<a*b;
 7 }
 8 int main(){
 9     solve();
10     
11     return 0;
12 }

题4.5 - A除以B

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 void solve(){
 4     int a,b;
 5     cin>>a>>b;
 6     cout<<a<<'/';
 7     if(b<0)
 8         cout<<'('<<b<<')'<<'=';
 9     else
10         cout<<b<<'=';
11     if(!b)
12         cout<<"Error";
13     else
14         cout<<fixed<<setprecision(2)<<1.0*a/b<<'\n';
15 }
16 int main(){
17     ios::sync_with_stdio(0);
18     cin.tie(0);cout.tie(0);
19     solve();
20     
21     return 0;
22 }

题4.6 - 新世界

#include<bits/stdc++.h>
using namespace std;
void solve(){
    cout<<"Hello World\nHello New World";
}
int main(){
    solve();
    
    return 0;
}

题4.7 - 古风排版

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 string s,d[105];
 4 void solve(){
 5     int N,len,i;
 6     cin>>N;
 7     cin.get();//抛弃一个换行
 8     getline(cin,s);
 9     len=s.size();
10     for(i=0;i<len;i++)
11         d[i%N]=s[i]+d[i%N];
12     for(;i%N;i++)
13         d[i%N]=' '+d[i%N];
14     for(i=0;i<N;i++)
15         cout<<d[i]<<'\n';
16 }
17 int main(){
18     ios::sync_with_stdio(0);
19     cin.tie(0);cout.tie(0);
20     solve();
21     
22     return 0;
23 }

题4.8 - 最佳情侣身高差

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 void solve(){
 4     int N;
 5     double h;
 6     string s;
 7     cin>>N;
 8     while(N--){
 9         cin>>s>>h;
10         if(s=="M")
11             cout<<fixed<<setprecision(2)<<h/1.09<<'\n';
12         else
13             cout<<fixed<<setprecision(2)<<h*1.09<<'\n';
14     }
15 }
16 int main(){
17     ios::sync_with_stdio(0);
18     cin.tie(0);cout.tie(0);
19     solve();
20     
21     return 0;
22 }

题4.9 - 人以群分

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int ar[100050];
 4 void solve(){
 5     int N,i,sum=0;
 6     cin>>N;
 7     for(i=0;i<N;i++){
 8         cin>>ar[i];
 9         sum+=ar[i];
10     }
11     sort(ar,ar+N);
12     for(i=0;i<N/2;i++)
13         sum-=2*ar[i];
14     cout<<"Outgoing #: "<<N-N/2<<"\nIntroverted #: "<<N/2<<"\nDiff = "<<sum<<'\n';
15 }
16 int main(){
17     ios::sync_with_stdio(0);
18     cin.tie(0);cout.tie(0);
19     solve();
20     
21     return 0;
22 }

题4.11 - 悄悄关注

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 struct node{
 4     string name;
 5     int n;
 6     bool operator < (const node& a) const{
 7         return n>a.n;
 8     }
 9 }ar[10050];
10 void solve(){
11     int N,M,i,sum=0;
12     string s;
13     cin>>N;
14     set<string> st;
15     while(N--){
16         cin>>s;
17         st.insert(s);
18     }
19     cin>>M;
20     for(i=0;i<M;i++){
21         cin>>ar[i].name>>ar[i].n;
22         sum+=ar[i].n;
23     }
24     sort(ar,ar+M);
25     double ave=1.0*sum/M;
26     set<string> ans;
27     for(i=0;i<M;i++){
28         if(ar[i].n<=ave)
29             break;
30         if(st.find(ar[i].name)==st.end())
31             ans.insert(ar[i].name);
32     }
33     if(ans.empty())
34         cout<<"Bing Mei You\n";
35     else{
36         set<string>::iterator it;
37         for(it=ans.begin();it!=ans.end();it++)
38             cout<<(*it)<<'\n';
39     }
40 }
41 int main(){
42     ios::sync_with_stdio(0);
43     cin.tie(0);cout.tie(0);
44     solve();
45     
46     return 0;
47 }

题4.13 - 非常弹的球

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const double g=9.8;
 4 void solve(){
 5     double w,p,ans=0.0,x,E=1000.0;
 6     scanf("%lf%lf",&w,&p);
 7     p=100-p;
 8     while((x=E*200.0/w/g)>=1e-7){
 9         ans+=x;
10         E=E*p/100.0;
11     }
12     printf("%.3f\n",ans);
13 }
14 int main(){
15     solve();
16     
17     return 0;
18 }

待补

猜你喜欢

转载自www.cnblogs.com/stelayuri/p/12249297.html