SMU 2024 spring 天梯赛1(补题)

        发挥一般,水平也一般,不想再多说了,哑口无言,继续努力。

1.7-6 吉老师的回归

        关键点在于对回车字符的读取::getchar()

#include <bits/stdc++.h>
using namespace  std;

int main()
{

    string b;
    int n,m;
    cin>>n>>m;
    vector<string>a;
    ::getchar();
    for(int i=0;i<=n-1;i++)
    {
        getline(cin,b);
        if((b.find("easy")==-1) and (b.find("qiandao")==-1)) {
            a.push_back(b);
        }
    }
    if(a.size()<=m) cout<<"Wo AK le";
    else cout<<a[m];
    return 0;
}
2.7-7 静静的推荐

        按题意模拟即可,理解较为简单。

#include <bits/stdc++.h>
using namespace  std;
#define PII pair<int ,int>
int main()
{
    int n,k,s;
    cin>>n>>k>>s;
    map<int,int>q,t;
    for(int i=0;i<n;i++)
    {
        int a,b;
        cin>>a>>b;
        if(a<175) continue;
        q[a]++;
        if(b>=s)
            t[a]++;
    }
    int ans=0;
    for(auto [a,b]:q){
        int kl=max(0,b-t[a]);
        ans+=t[a];
        ans+=min(k,kl);
    }
    cout<<ans<<endl;
    return 0;
}
3.7-8 机工士姆斯塔迪奥

        越到后面好像越简单了,模拟即可。

#include <bits/stdc++.h>
using namespace  std;
#define PII pair<int ,int>
int main()
{
    int n,m,q;
    cin>>n>>m>>q;
    int a=0,b=0;
    set<int>x,y;
    for(int i=0;i<q;i++)
    {
        cin>>a>>b;
        if(a==0) x.insert(b);
        else
        y.insert(b);
    }
    cout<<(n-x.size())*(m-y.size());
    return 0;
}
4.7-9 彩虹瓶

        根据题意,存储在货架上的箱子模拟为栈,用stack容器储存未放入瓶中的颜料,然后按题模拟即可。

#include <bits/stdc++.h>
using namespace  std;
int n,m,k;
void spring()
{
    stack<int>a;
    int idx=1;
    queue<int>b;
    for(int i=0;i<n;i++)
    {
        int x;
        cin>>x;
        b.push(x);
    }
    while(idx<=n){
        if(a.size())
        {
            int t=a.top();
            if(t==idx)
            {
                idx++;
                a.pop();
                continue;
            }
        }
        if(b.empty()){
            cout<<"NO"<<endl;
            return ;
        }
        int tp=b.front();
        b.pop();
        if(tp==idx)
        {
            idx++;
            continue;

        }
        else{
            if(a.size()<m)
            {
                a.push(tp);
            }
            else
            {
                cout<<"NO"<<endl;
                return ;
            }
        }

    }
    cout<<"YES"<<endl;
    return ;
}
int main()
{
    cin>>n>>m>>k;
    for(int i=0;i<k;i++)
    {
        spring();
    }
    return 0;

}
5.7-10 简单计算器

        按照题意进行模拟,注意当s1中只剩下一个数据时输出,整体比较简单。

#include <bits/stdc++.h>
using namespace  std;
int main()
{
    int n;
    cin>>n;
    stack<int>a;
    stack<char>b;
    for(int i=0;i<n;i++)
    {
        int x=0;
        cin>>x;
        a.push(x);
    }
    for(int i=0;i<n-1;i++)
    {
        char k;
        cin>>k;
        b.push(k);
    }
    if(n==1) {
        cout<<a.top();
        return 0;
    }
    while(a.size()>1)
    {
        int x,y;
        char z;
        x=a.top();
        a.pop();
        y=a.top();
        a.pop();
        z=b.top();
        b.pop();

        if(x==0&&z=='/')
        {
            cout<<"ERROR: "<<y<<"/0"<<endl;
            return 0;
        }
        if(z=='+') a.push(y+x);
        else if(z=='-')  a.push(y-x);
        else if(z=='*')  a.push(y*x);
        else a.push(y/x);
    }
    if(a.size()==1) cout<<a.top();
    return 0;

}
6.7-11 龙龙送外卖

        因为是从根节点到其他外卖点,当你到达一个外卖店时,是可能回去走两次之前的边的,但最后一个到达的那个外卖店,是不用回去的,因此那个不回的必定是深度最大的那个,因此就是对于每个外卖点的覆盖路径 * 2-最长的那个 

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int mod=1e9+7;
const int N=2e5+1;
#define PII pair<int,int>
vector<int>g[N+1];
int vis[N];
int fa[N];
int s[N];
//求深度
void dfs(int u,int deep){
    s[u]=deep;
    for(auto so:g[u]) {
        if (so != 0) {
            dfs(so, deep + 1);
        }
    }
}
void solve(){
    int n,m;
    cin>>n>>m;
    int st=-1;
    for (int i = 1; i <=n ; ++i) {
        int x;
        cin>>x;
        if(x==-1){
            st=i;
        }
        else
            g[x].push_back(i),fa[i]=x;
    }
    dfs(st,0);
    int max_deep=0;
    int ans=0;
    while (m--){
        int x;
        cin>>x;
        max_deep=max(max_deep,s[x]);
        while (x!=st and vis[x]==0){
            vis[x]=1;
            ans+=2;
            x=fa[x];
        }
        cout<<ans-max_deep<<endl;
    }
}
signed main() {
     ios::sync_with_stdio(false),cin.tie(0);
    int t=1;
    // cin>>t;
    while (t--){
        solve();

    }
    return 0;
}
7.7-12 智能护理中心统计

        这是一个森林(有多个树组成),首先字符串我们映射成点,1到n是老人,>n+1的是管理节点,然后我们记录一下每一个节点是由哪个节点管理的,然后我们对所有的老人节点往上搜索,对他们的所有管理节点记录贡献,当我们要修改的时候,也是往上搜索减去这个点,然后再将移动后的那个点往上增加,查询的时候直接输出S[u]即可

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int mod=1e9+7;
const int N=2e5+1;
#define PII pair<int,int>
int fa[N];
int s[N];
void solve(){
    int n,m;
    cin>>n>>m;
    map<string,int >mp;
    int idx=n+1;
    for (int i = 1; i <=m ; ++i) {
        string a,b;
        cin>>a>>b;
        int od=1;
        for (auto j:a) {
            if(j>='A' and j<='Z'){
                od=0;
                break;
            }
        }
        if(od) {
            int bh = 0;
            for (auto j:a) {
                bh=bh*10+j-'0';
            }
            if(mp[b]==0){
                mp[b]=idx++;
            }
            int dq=mp[b];
            fa[bh]=dq;
        }
        else {
            if (mp[a] == 0) {
                mp[a] = idx++;
            }
            if (mp[b] == 0) {
                mp[b] = idx++;
            }
            fa[mp[a]] = mp[b];
        }
    }
    for (int i = 1; i <=n ; ++i) {
        int u=i;
        while (fa[u]!=0){
            s[fa[u]]++;
            if(fa[u]==u or u==0)break;
            u=fa[u];
        }
    }
    while (1){
        char op;
        cin>>op;
        if(op=='E')return;
        if(op=='Q'){
            string a;
            cin>>a;
            cout<<s[mp[a]]<<endl;
        }
        else if(op=='T'){
            int u;
            cin>>u;
            string a;
            cin>>a;
            while (u!=0){
                s[fa[u]]--;
               // cout<<u<<' ';
              //  cout<<fa[u]<<' '<<fa[12]<<endl;
                u=fa[u];
                if(fa[u]==u or u==0)break;
            }
            fa[u]=mp[a];
            while (fa[u]!=0){
                s[fa[u]]++;
                u=fa[u];
            }
        }
    }
}
signed main() {
   ios::sync_with_stdio(false),cin.tie(0);
    int t=1;
   // cin>>t;
    while (t--){
        solve();

    }
    return 0;
}
8.7-13 计算图

        不会

9.7-14 教科书般的亵渎

        不会

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值