天梯赛练习题L2(026-027)

L2-026 小字辈

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=100020,M=4002;
//unordered_map<LL,LL> a[N];
//priority_queue<LL> pq;
//priority_queue<LL,vector<LL>,greater<LL>> pq2;
LL n,root=-1,maxn=1;
LL a[N];
vector<LL> res,v[N];
vector<PII> ans;
void dfs(LL x,LL last,LL depth)
{
    maxn=max(maxn,depth);
    if(v[x].size()==0)
    {
        ans.push_back({x,depth});
        return ;
    }
    for(int i=0;i<v[x].size();i++)
        dfs(v[x][i],x,depth+1);
}
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            if(a[i]==-1) root=i;
            else v[a[i]].push_back(i);
        }
        dfs(root,-1,1);
        cout<<maxn<<endl;
        for(int i=0;i<ans.size();i++)
        {
            //cout<<ans[i].first<<" "<<ans[i].second<<endl;
            if(ans[i].second==maxn) //cout<<ans[i].first<<endl;
                res.push_back(ans[i].first);
        }
        sort(res.begin(),res.end());
        for(int i=0;i<res.size();i++)
        {
            if(i!=res.size()-1) cout<<res[i]<<" ";
            else cout<<res[i];
        }
    }
    return 0;
}

L2-027 名人堂与代金券

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=10200,M=4002;
//unordered_map<LL,LL> a[N];
//priority_queue<LL> pq;
//priority_queue<LL,vector<LL>,greater<LL>> pq2;
LL n,m,k;
struct node
{
    string s;
    LL x,y=0;
    LL id;
}a[N];
bool cmp(node l,node r)
{
    if(l.x!=r.x) return l.x>r.x;
    else return l.s<r.s;
}
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        cin>>n>>m>>k;
        LL sum=0;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i].s>>a[i].x;
            if(a[i].x>=m) a[i].y=50;
            else if(a[i].x>=60) a[i].y=20;
            sum+=a[i].y;
        }
        sort(a+1,a+1+n,cmp);
        cout<<sum<<endl;
        LL last;
        for(int i=1;i<=n;i++)
        {
            if(i==1)
            {
                last=1;
                if(last>k) break;
                cout<<"1 "<<a[i].s<<" "<<a[i].x<<endl;
            }
            else
            {
                if(a[i].x==a[i-1].x) cout<<last<<" "<<a[i].s<<" "<<a[i].x<<endl;
                else
                {
                    last=i;
                    if(last>k) break;
                    cout<<last<<" "<<a[i].s<<" "<<a[i].x<<endl;
                }
            }
            if(last>k) break;
        }
    }
    return 0;
}
posted @ 2023-03-06 16:53  高尔赛凡尔娟  阅读(12)  评论(0编辑  收藏  举报