天梯赛真题补题单(L2-1 ~ L2-4)

L2-1 点赞狂魔

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL N=200200,M=2020,INF=0x3f3f3f3f;
LL n;
struct node
{
    string s;
    LL sum;
}a[N];
bool cmp(node l,node r)
{
    if(l.sum!=r.sum) return l.sum>r.sum;
    else return l.s>r.s;
}
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    int T=1;
    //cin>>T;
    while(T--)
    {
        cin>>n;
        map<LL,LL> mp;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i].s;
            LL k;
            cin>>k;
            while(k--)
            {
                LL x;
                cin>>x;
                mp[x]++;
                if(mp[x]==1) a[i].sum+=1;
            }
            mp.clear();
        }
        sort(a+1,a+1+n,cmp);
        if(n>=3)
        {
            for(int i=1;i<=3;i++)
            {
                cout<<a[i].s;
                if(i!=3)
                {
                    cout<<" ";
                }
            }
        }
        else
        {
            for(int i=1;i<=n;i++)
            {
                cout<<a[i].s<<" ";
            }
            if(n==1) cout<<"- -";
            else cout<<" -";
        }
    }
    return 0;
}

L2-2 重排链表

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL N=200200,M=2020,INF=0x3f3f3f3f;
LL head,n;
LL address,e[N],ne[N];
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    int T=1;
    //cin>>T;
    while(T--)
    {
        cin>>head>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>address>>e[address]>>ne[address];
        }
        deque<LL> d;
        for(int i=head;i!=-1;i=ne[i])
        {
            d.push_back(i);
        }
        vector<LL> v;
        while(d.size())
        {
            v.push_back(d.back());
            d.pop_back();
            if(d.size()==0) break;
            v.push_back(d.front());
            d.pop_front();
        }
        for(int i=0;i<v.size();i++)
        {
            printf("%05ld %ld ",v[i],e[v[i]]);
            if(i==v.size()-1) printf("-1\n");
            else printf("%05ld\n",v[i+1]);
        }
    }
    return 0;
}

L2-3 图着色问题

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL N=200200,M=2020,INF=0x3f3f3f3f;
LL v,e,k;
LL color[N];
vector<LL> vv[M];
bool check()
{
    for(int i=1;i<=v;i++)
    {
        for(int j=0;j<vv[i].size();j++)
        {
            if(color[vv[i][j]]==color[i])
            {
                return false;
            }
        }
    }
    return true;
}
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    int T=1;
    //cin>>T;
    while(T--)
    {
        cin>>v>>e>>k;
        for(int i=1;i<=e;i++)
        {
            LL x,y;
            cin>>x>>y;
            vv[x].push_back(y);
            vv[y].push_back(x);
        }
        LL n;
        cin>>n;
        LL sum=0;
        map<LL,LL> mp;
        while(n--)
        {
            mp.clear();
            sum=0;
            for(int i=1;i<=v;i++)
            {
                cin>>color[i];
                mp[color[i]]++;
                if(mp[color[i]]==1)
                {
                    sum++;
                }
            }
            if(sum!=k) cout<<"No"<<endl;
            else
            {
                if(check()==false) cout<<"No"<<endl;
                else cout<<"Yes"<<endl;
            }
        }

    }
    return 0;
}

L2-4 部落

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL N=200200,M=2020,INF=0x3f3f3f3f;
LL n;
LL father[N];
LL sum=0;
map<LL,LL> mp;
void init()
{
    for(int i=0;i<=10100;i++)
    {
        father[i]=i;
    }
}
LL find(LL x)
{
    if(father[x]!=x) father[x]=find(father[x]);
    return father[x];
}
void merge(LL a,LL b)
{
    LL fa=find(a),fb=find(b);
    if(fa!=fb) father[fa]=fb;
}
int main()
{
    //cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    int T=1;
    //cin>>T;
    while(T--)
    {
        init();
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            LL k;
            cin>>k;
            LL a;
            cin>>a;
            mp[a]++;
            if(mp[a]==1) sum++;
            for(int j=2;j<=k;j++)
            {
                LL b;
                cin>>b;
                mp[b]++;
                if(mp[b]==1) sum++;
                merge(a,b);
            }
        }
        LL ans=0;
        map<LL,LL> pm;
        for(int i=0;i<=10010;i++)
        {
            if(mp[i]!=0)
            {
                if(pm[find(i)]==0)
                {
                    pm[find(i)]++;
                    ans++;
                }
            }
        }
        cout<<sum<<" "<<ans<<endl;
        LL q;
        cin>>q;
        while(q--)
        {
            LL a,b;
            cin>>a>>b;
            LL fa=find(a);
            LL fb=find(b);
            if(fa!=fb) cout<<"N"<<endl;
            else cout<<"Y"<<endl;
        }
    }
    return 0;
}
posted @ 2024-04-17 11:55  高尔赛凡尔娟  阅读(6)  评论(0编辑  收藏  举报