【2021团体程序设计天梯赛】L2部分(PTA,L2-037到L2-040)题解代码

简介: 【2021团体程序设计天梯赛】L2部分(PTA,L2-037到L2-040)题解代码

概述

  • 都是模拟,没啥好说的。

L2-037 包装机 25 69 257 0.27
L2-038 病毒溯源 25 72 277 0.26
L2-039 清点代码库 25 47 487 0.10
L2-040 哲哲打游戏 25 39 223 0.17

L2-037 包装机 (25分)

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e4+10;
string ss[1010];
int f[1010];
int main(){
    int n, m, s;
    cin>>n>>m>>s;
    for(int i = 1; i <= n; i++){
        cin>>ss[i];
    }
    stack<char>stk;
    int x;  
    while(cin>>x && x!=-1){
        if(x==0){
            if(stk.size()==0){
                continue;
            }
            cout<<stk.top(); stk.pop();
        }else{
            if(f[x]>=ss[x].size())continue;
            if(stk.size()==s){
                cout<<stk.top();  stk.pop();
                //if(f[x]<ss[x].size())
                //    stk.push(ss[x][f[x]++]);
            }
            //if(f[x]<ss[x].size())
                stk.push(ss[x][f[x]++]);
        }
    }
    return 0;
}

L2-038 病毒溯源 (25分)

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e4+10;
vector<int>G[maxn];
int ans = 0, ans2=0, root=0;
void dfs(int u, int h){
    ans = max(ans,h);
    for(int v:G[u]){
        dfs(v,h+1);
    }
}

int ok = 0;
vector<int>vc;
void dfs2(int u,int h){
    if(ok==1)return ;
    if(h==ans2 && ok==0){
        ok = 1;
        for(int i = 0; i < ans2; i++){
            if(i!=0)cout<<" "<<vc[i];
            else cout<<vc[i];
        }
        return ;
    }
    for(int v:G[u]){
        vc.push_back(v);
        dfs2(v,h+1);
        vc.pop_back();
    }
}

int main(){
    int n;  cin>>n;
    for(int i = 0; i < n; i++){
        int k;  cin>>k;
        for(int j = 0; j < k; j++){
            int x;  cin>>x;
            G[i].push_back(x);
        }
        if(G[i].size()!=0)sort(G[i].begin(),G[i].end());
    }
    for(int i = 0; i < n; i++){
        dfs(i,1);
        if(ans>ans2){
            ans2 = ans; root = i;
        }
    }
    
    cout<<ans2<<"\n";
    vc.push_back(root);
    dfs2(root,1);
    return 0;
}
//给出一棵树,求最长链
//并查集不要路径压缩,统计每个点所在的链 ps. 可以不是从0开始
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;

int fa[maxn+10];
inline void init(int n){for(int i = 0; i < n; i++)fa[i]=i;}
inline int find(int x){return x==fa[x]?x:find(fa[x]);}
inline void merge(int x, int y){ if(x!=y)fa[y] = x;}

int cc[maxn+10];
inline int size(int x){//TLE5
    int ans = 1;
    while(x!=fa[x])x=fa[x],ans++;
    return ans;
}
bool cmp(vector<int>a, vector<int>b){
    for(int i = 0; i < a.size(); i++)
        if(a[i]!=b[i])return a[i]<b[i];
}
int main(){
    int n;  cin>>n;  init(n);
    for(int i = 0; i < n; i++){
        int k;  scanf("%d",&k);
        for(int j = 1; j <= k; j++){
            int x;  scanf("%d",&x);
            merge(i,x);
        }
    }
    //先找最长链
    int len = 1;
    for(int i = 0; i < n; i++){
        cc[i] = size(i);
        len = max(len, cc[i]);
    }
    //存储所有路径,排序输出最短
    vector<vector<int> >ans;
    for(int i = 0; i < n; i++){
        if(cc[i]!=len)continue;
        vector<int>vc;
        int x = i;
        while(x!=fa[x]){
            vc.push_back(x);
            x = fa[x];
        }
        vc.push_back(x);
        reverse(vc.begin(),vc.end());
        ans.push_back(vc);
    }
    sort(ans.begin(),ans.end(),cmp);
    //cout<<ans[0].size()<<"\n"; TLE5
    int nn = ans[0].size();
    printf("%d\n",nn);
    for(int i = 0; i < nn; i++){
        if(i!=0)printf(" %d",ans[0][i]);
        else printf("%d",ans[0][i]);
    }
    return 0;
}

L2-039 清点代码库 (25分)

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e4+10;
bool cmp(pair<vector<int>, int> a, pair<vector<int>, int> b){
    if(a.second!=b.second)return a.second>b.second;
    for(int i = 0; i < a.first.size(); i++){
        if(a.first[i]!=b.first[i])
            return a.first[i]<b.first[i];
    }
}
int main(){
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int n, m;  cin>>n>>m;
    map<vector<int>, int>ma;
    for(int i = 1; i <= n; i++){
        vector<int>vc;
        for(int j = 1; j <= m; j++){
            int x;  cin>>x;  vc.push_back(x);
        }
        ma[vc]++;
    }
    vector<pair<vector<int>, int> >vc(ma.begin(), ma.end());
    sort(vc.begin(),vc.end(), cmp);
    cout<<vc.size()<<"\n";
    for(int i = 0; i < vc.size(); i++){
        cout<<vc[i].second;
        for(int j = 0; j < vc[i].first.size(); j++){
            cout<<" "<<vc[i].first[j];
        }
        cout<<"\n";
    }
    return 0;
}

L2-040 哲哲打游戏 (25分)

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
vector<int>G[maxn];
int back[110];
int main(){
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int n, m;  cin>>n>>m;
    for(int i = 1; i <= n; i++){
        int k;  cin>>k;
        for(int j = 1; j <= k; j++){
            int x;  cin>>x;
            G[i].push_back(x);
        }
    }
    int now = 1;
    for(int i = 1; i <= m; i++){
        int op, x;  cin>>op>>x;
        if(op==0){
            now = G[now][x-1];
        }else if(op==1){
            back[x] = now;
            cout<<now<<"\n";
        }else{
            now = back[x];
        }
    }
    cout<<now<<"\n";
    return 0;
}
目录
相关文章
|
人工智能 BI 知识图谱
2019年 团体程序设计天梯赛——题解集
⭐L1一阶题 (虽然比较基础但是是很重要的一部分,且一些题目有一定难度哦!) ⭐L1-057 PTA使我精神焕发 (5分) 本题题目链接 以上是湖北经济学院同学的大作。本题就请你用汉语拼音输出这句话。 输入格式: 本题没有输入。
152 0
 2019年 团体程序设计天梯赛——题解集
|
Linux 测试技术 容器
2020年 团体程序设计天梯赛——题解集(1)
⭐L1一阶题 (虽然比较基础但是是很重要的一部分,且一些题目有一定难度哦!) ⭐L1-065 嫑废话上代码 (5分) 本题题目链接!!!!! Linux 之父 Linus Torvalds 的名言是:“Talk is cheap. Show me the code.”(嫑废话,上代码)。本题就请你直接在屏幕上输出这句话。 输入格式: 本题没有输入。
210 0
|
程序员
2017年 团体程序设计天梯赛——题解集
⭐L1-038 新世界 (5分) 本题题目链接👈 👈 👈 👈 👈 这道超级简单的题目没有任何输入。 你只需要在第一行中输出程序员钦定名言“Hello World”,并且在第二行中输出更新版的“Hello New World”就可以了。
330 0
|
前端开发 JavaScript 开发者
2016年 团体程序设计天梯赛——题解集
⭐ L1-028 判断素数 (10分) 本题题目链接 本题的目标很简单,就是判断一个给定的正整数是否素数。 输入格式: 输入在第一行给出一个正整数N(≤ 10),随后N行,每行给出一个小于2 31 的需要判断的正整数。 输出格式: 对每个需要判断的正整数,如果它是素数,则在一行中输出Yes,否则输出No。
240 0
|
芯片
2022年 团体程序设计天梯赛——题解集(1)
⭐L1一阶题 (虽然比较基础但是是很重要的一部分,且一些题目有一定难度哦!) ⭐L1-081 今天我要赢 (5分)——水题 本题题目链接!!!!! 2018 年我们曾经出过一题,是输出“2018 我们要赢”。今年是 2022 年,你要输出的句子变成了“我要赢!就在今天!”然后以比赛当天的日期落款。
358 0
|
人工智能 算法 安全
2022年 团体程序设计天梯赛——题解集(2)
⭐L1一阶题 (虽然比较基础但是是很重要的一部分,且一些题目有一定难度哦!) ⭐L1-081 今天我要赢 (5分)——水题 本题题目链接!!!!! 2018 年我们曾经出过一题,是输出“2018 我们要赢”。今年是 2022 年,你要输出的句子变成了“我要赢!就在今天!”然后以比赛当天的日期落款。
279 0
|
机器学习/深度学习
2018年 团体程序设计天梯赛——题解集
⭐L1-051 打折 (5分) 本题题目链接👈👈👈👈👈 去商场淘打折商品时,计算打折以后的价钱是件颇费脑子的事情。例如原价 ¥988,标明打 7 折,则折扣价应该是 ¥988 x 70% = ¥691.60。本题就请你写个程序替客户计算折扣价。 输入格式: 输入在一行中给出商品的原价(不超过1万元的正整数)和折扣(为[1, 9]区间内的整数),其间以空格分隔。 输出格式: 在一行中输出商品的折扣价,保留小数点后 2 位。
523 0
|
小程序 Linux
2020年 团体程序设计天梯赛——题解集(2)
⭐L1一阶题 (虽然比较基础但是是很重要的一部分,且一些题目有一定难度哦!) ⭐L1-065 嫑废话上代码 (5分) 本题题目链接!!!!! Linux 之父 Linus Torvalds 的名言是:“Talk is cheap. Show me the code.”(嫑废话,上代码)。本题就请你直接在屏幕上输出这句话。 输入格式: 本题没有输入。
219 0
|
C语言 C++
PTA团体程序设计天梯赛-练习集: L1-050 倒数第N个字符串 ( 15分 )
给定一个完全由小写英文字母组成的字符串等差递增序列,该序列中的每个字符串的长度固定为 L,从 L 个 a 开始,以 1 为步长递增。例如当 L 为 3 时,序列为 { aaa, aab, aac, ..., aaz, aba, abb, ..., abz, ..., zzz }。这个序列的倒数第27个字符串就是 zyz。对于任意给定的 L,本题要求你给出对应序列倒数第 N 个字符串。 输入格式: 输入在一行中给出两个正整数 L(2 ≤ L ≤ 6)和 N(≤105)。 输出格式: 在一行中输出对应序列倒数第 N 个字符串。题目保证这个字符串是存在的。 输入样例:
154 0
|
测试技术 C语言 C++
PTA团体程序设计天梯赛-练习集:L1-003 个位数统计
给定一个 k 位整数 N=dk−110k−1+⋯+d1101+d0 (0≤di≤9, i=0,⋯,k−1, dk−1>0),请编写程序统计每种不同的个位数字出现的次数。例如:给定 N=100311,则有 2 个 0,3 个 1,和 1 个 3。 输入格式: 每个输入包含 1 个测试用例,即一个不超过 1000 位的正整数 N。 输出格式: 对 N 中每一种不同的个位数字,以 D:M 的格式在一行中输出该位数字 D 及其在 N 中出现的次数 M。要求按 D 的升序输出。
177 0