【2023团体程序设计天梯赛CCCC】GPLT2023,L1~L2部分(PTA,L1-089~L1-096,L2-045~L2-048)题解代码&复盘

概要

L1部分:L1-089~L1-096
L2部分:L2-045~L2-048
L3部分:L3-033~L3-036

L1-089 最好的文档 5

#include<bits/stdc++.h>
using namespace std;
int main(){
    
    
	cout<<"Good code is its own best documentation.\n";
	return 0;
}

L1-090 什么是机器学习 5

#include<bits/stdc++.h>
using namespace std;
int main(){
    
    
	int a, b;  cin>>a>>b;
	int c = a+b;
	cout<<c-16<<"\n"<<c-3<<"\n"<<c-1<<"\n"<<c<<"\n";
	return 0;
}

L1-091 程序员买包子 10

#include<bits/stdc++.h>
using namespace std;
int main(){
    
    
	int n, m, k;  string x; 
	cin>>n>>x>>m>>k;
	if(k==n){
    
    
		cout<<"mei you mai "<<x<<" de\n";
	}else if(k==m){
    
    
		cout<<"kan dao le mai "<<x<<" de\n";
	}else{
    
    
		cout<<"wang le zhao mai "<<x<<" de"<<"\n";
	}
	return 0;
}

L1-092 进化论 10

#include<bits/stdc++.h>
using namespace std;
int main(){
    
    
	int n;  cin>>n;
	for(int i = 1; i <= n; i++){
    
    
		int a, b, c;  cin>>a>>b>>c;
		if(c==a*b){
    
    
			cout<<"Lv Yan\n";
		}else if(c==a+b){
    
    
			cout<<"Tu Dou\n";
		}else{
    
    
			cout<<"zhe du shi sha ya!\n";
		}
	}
	return 0;
}

L1-093 猜帽子游戏 15

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1010;
int a[maxn];
int main(){
    
    
	int n;  cin>>n;
	for(int i = 1; i <= n; i++){
    
    
		cin>>a[i];
	}
	int k;  cin>>k;
	while(k--){
    
    
		int ok = 0, ok2 = 1;
		for(int i = 1; i <= n; i++){
    
    
			int x;  cin>>x;
			if(x==0)continue;
			else if(x==a[i]){
    
    
				ok = 1;
			}else{
    
    
				ok2 = 0;
			}
		}
		if(ok2==1 && ok==1){
    
    
			cout<<"Da Jiang!!!\n";
		}else{
    
    
			cout<<"Ai Ya\n";
		}
	}
	return 0;
}

L1-094 剪切粘贴 15

#include<bits/stdc++.h>
using namespace std;
int main(){
    
    
	string s;  cin>>s;
	int T;  cin>>T;
	while(T--){
    
    
		int x, y;  string a, b;  cin>>x>>y>>a>>b;
		string t = s.substr(x-1, y-x+1);
		s.erase(x-1,y-x+1);
		int cur = 0;
		int ok = 1;
		while(s.find(a,cur) != s.npos && cur < s.size()){
    
    
			int p = s.find(a,cur);
			string tt = s.substr(p+a.size(),b.size());
			if(tt == b){
    
    
				s.insert(p+a.size(),t);
				ok = 0;
				break;
			}else{
    
    
				cur++;
			}
		}
		if(s.find(a, cur) == s.npos && ok == 1){
    
    
			s.insert(s.size(), t);
		}
	}
	cout<<s<<"\n";
	return 0;
}

L1-095 分寝室 20

#include<bits/stdc++.h>
using namespace std;
int main(){
    
    
	int x, y, n;  cin>>x>>y>>n;
	int xx = -1, yy = -1, rs = 1e5+10;
	int zx = 0, zy = 0;
	for(int i = 1; i < n; i++){
    
    
		int j = n-i;
		if(x%i!=0 || y%j!=0)continue;
		int xi = x/i, yj = y/j;
		if(xi==1 || yj==1)continue;
		if(abs(xi-yj) < rs){
    
    
			xx = xi;
			yy = yj;
			zx = i;
			zy = j;
			rs = abs(xi-yj);
		}
	}
	if(xx!=-1)cout<<zx<<" "<<zy<<"\n";
	else cout<<"No Solution\n";
	return 0;
}

L1-096 谁管谁叫爹 20

#include<bits/stdc++.h>
using namespace std;
int get(int x){
    
    
	int res = 0;
	while(x){
    
    
		res += x%10;
		x /= 10;
	}
	return res;
}
int main(){
    
    
	int n;  cin>>n;
	while(n--){
    
    
		int na, nb;  cin>>na>>nb;
		int sa = get(na), sb = get(nb);
		if(na%sb==0&&nb%sa==0){
    
    
			if(na > nb){
    
    
				cout<<"A\n";
			}else{
    
    
				cout<<"B\n";
			}
		}
		else if(na%sb==0)cout<<"A\n";
		else if(nb%sa==0)cout<<"B\n";
		else if(na > nb){
    
    
			cout<<"A\n";
		}else{
    
    
			cout<<"B\n";
		}
	}
	return 0;
}

L2-045 堆宝塔 25

//思路:
//看到堆盘子想到水杯/栈 /(汉诺塔/递归,并没有)之类的东西,按题意无脑模拟即可
#include<bits/stdc++.h>
using namespace std;
int main(){
    
    
	int n;  cin>>n;
	stack<int>a, b;
	int cnt = 0, mx = 0;
	while(n--){
    
    
		int x;  cin>>x;
		if(a.empty() || x<a.top()){
    
    
			a.push(x);
		}else{
    
    
			if(b.empty() || x>b.top()){
    
    
				b.push(x);
			}else{
    
    
				mx = max(mx, (int)a.size());
				while(!a.empty())a.pop();
				cnt++;
				while(!b.empty() && b.top()>x){
    
    
					a.push(b.top());
					b.pop();
				}
				a.push(x);
			}
		}
	}
	if(a.size()!= 0)cnt++;
	if(b.size()!= 0)cnt++;
	mx = max(mx, (int)a.size());
	cout<<cnt<<" "<<mx<<"\n";
	return 0;
}

L2-046 天梯赛的赛场安排

//思路:
//按题意模拟即可,注意细节点比较多,每一轮对当前未安排的人数最多的学校进行处理,只处理一个考场,然后剩余人数是要放回去的。
//最开始的找人数最多可以重载优先队列,找编号最小的和新开考场其实注意到数组范围不大,其实可以开个数组for。
//好久没写代码了,一下子没想到开数组,也忘记怎么打重载了,所以就乱搞了一波,强行暴力多for了一下。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 5050;
string sc[maxn];
int num[maxn], rs[maxn];
int main(){
    
    
	int n, c;  cin>>n>>c;
	priority_queue<pair<int,int>>q2;
	for(int i = 1; i <= n; i++){
    
    
		cin>>sc[i]>>num[i];
		q2.push({
    
    num[i], i});
	}
	int cnt = 0;
	int tot = 0;
	priority_queue<pair<int,int>>q;
	int xxx = 0;
	while(q2.size()){
    
    
		int nm = q2.top().first;
		int id = q2.top().second;  q2.pop();
		if(nm > c){
    
    
			rs[id]++;
			cnt++;
			nm -= c;
			q2.push({
    
    nm,id});
			continue;
		}
		if(nm != 0){
    
    
			if(!q.empty() && q.top().first >= nm){
    
    
				vector<pair<int,int>>vc;
				while(!q.empty() && q.top().first >= nm){
    
    
					vc.push_back(q.top()); q.pop();
				}
				int t = vc.back().first; 
				int id2 = vc.back().second;
				t -= nm;
				rs[id]++;
				if(t != 0)q.push({
    
    t, id});
				for(int i = 0; i < vc.size()-1; i++){
    
    
					q.push(vc[i]);
				}
			}else{
    
    
				q.push({
    
    c-nm, ++tot});
				rs[id]++;
				cnt++;
			}
		}
    }
	for(int i = 1; i <= n; i++){
    
    
		cout<<sc[i]<<" "<<rs[i]<<"\n";
	}
	cout<<cnt<<"\n";
	return 0;
}


L2-047 锦标赛 25

在这里插入图片描述

//题意:2^k个人两两比赛,最后剩1个。给出第i轮第j场的失败者,求最开始的所有人顺序。
//思路:完美二叉树,底层一半的值确定了,然后往上走一层,如果当前值比前一层对应位置的某个值大,那么就可以加入答案中,否则加无解了。
//数组维护上一层每个节点在答案中的位置(尚未被填入值的),然后从底往上走依次把值填进去即可。
#include<bits/stdc++.h>
using namespace std;
#define lch (j<<1)
#define rch (j<<1|1)

const int maxv = 50, maxn = (1<<20);
int a[maxv][maxn], o[maxv][maxn]; //第i层第j个的能力,以及兄弟节点在答案中的位置
int res[maxn];

int main(){
    
    
    int k;  cin>>k;
    int ok = 1;
    for(int i = 1; i <= k; i++){
    
    
        int n = 1<<(k-i);
        for(int j = 0; j < n; j++){
    
      //枚举每一层
            cin>>a[i][j];
            if(i==1){
    
    
                res[lch] = a[i][j];  o[i][j] = rch;
                continue;
            }
            int mx = max(a[i][j], max(a[i-1][lch], a[i-1][rch]));
            if(a[i][j] < a[i-1][lch] && a[i][j] < a[i-1][rch]){
    
    
                ok = 0;
                break;
            }else if(a[i][j] >= a[i-1][lch]){
    
     //如果当前是胜者, 就放到另一个位置
                res[o[i-1][lch]] = a[i][j];
                o[i][j] = o[i-1][rch]; 
            }else{
    
    
                res[o[i-1][rch]] = a[i][j];
                o[i][j] = o[i-1][lch];
            }  
            a[i][j] = mx;  //记录子树最大值
        }
    }
    int w;   cin>>w;
    if(a[k][0] <= w) res[o[k][0]] = w; else ok = 0;
    if(ok==0){
    
     cout<<"No Solution\n"; return 0; }
    for(int i = 0; i < (1<<k); i++){
    
    
        cout<<res[i]<<" \n"[i==(1<<k)];
    }
    return 0;
}

L2-048 寻宝图 25

//题意:Floodfill 例题,找联通块。
//岛屿的个数:dfs找有多少个联通块即可,注意1e5x1e5开不下bool/int数组和判重,可以用变长string,然后走过一个点就直接改掉
//宝藏岛屿:注意特判刚进去的那个点是不是宝藏,没判断的话是15分
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
int n, m;
string a[maxn];
int dx[] = {
    
    0, 0, -1, 1};
int dy[] = {
    
    1, -1, 0, 0};
int ok = 0;
void dfs(int x, int y){
    
    
	if(a[x][y]!='0' && a[x][y] != '1')ok = 1;
	for(int i = 0; i < 4; i++){
    
    
		int nx = x+dx[i], ny = y+dy[i];
		if(nx<=0||nx>n || ny<=0 || ny > m)continue;
		if(a[nx][ny]=='0')continue;
		if(a[nx][ny]!='1')ok = 1;
		a[nx][ny] = '0';
		dfs(nx, ny);
	}
}
int main(){
    
    
	cin>>n>>m;
	for(int i = 1; i <= n; i++) {
    
    
		cin>>a[i];
		a[i] = "0"+a[i];
	}
	int cnt = 0, res = 0;
	for(int i = 1; i <= n; i++){
    
    
		for(int j = 1; j <= m; j++){
    
    
			if(a[i][j]!='0'){
    
    
				ok = 0;
				dfs(i,j);
				cnt++;
				if(ok==1)res++;
			}
		}
	}
	cout<<cnt<<" "<<res<<"\n";
	return 0;
}

L3-035 完美树(骗分16)

//骗分:最后只输出一个数字,暴力从1开始枚举看看能不能骗分
//发现0有1分,20有15分。 然后试试限一下n的范围把两个点都骗进去。
#include<bits/stdc++.h>
using namespace std;

int main(){
    
    
    int n;  cin>>n;
    if(n<10)cout<<"0";//1分
    else cout<<"20";   //15分
    return 0;
}

参考资料

官方题解:链接

猜你喜欢

转载自blog.csdn.net/qq_33957603/article/details/130351115