ABC 292 ABCD

https://atcoder.jp/contests/abc292/tasks

来水一篇题解嘻嘻🤭

A - CAPS LOCK

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18,MINN=-1e18;
const LL N=1e6+10,M=4010;
const LL mod=998244353;
const double PI=3.1415926535;
#define endl '\n'
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    int T=1;
    //cin>>T;
    while(T--)
    {
        string s;
        cin>>s;
        for(int i=0;i<s.size();i++)
        {
            if(s[i]>='a'&&s[i]<='z') cout<<(char)(s[i]-32);
            else cout<<s[i];
        }
        cout<<endl;
    }
    return 0;
}

B - Yellow and Red Card

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18,MINN=-1e18;
const LL N=1e6+10,M=4010;
const LL mod=998244353;
const double PI=3.1415926535;
#define endl '\n'
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    int T=1;
    //cin>>T;
    while(T--)
    {
        LL n,m;
        cin>>n>>m;
        map<LL,LL> yl,rd;
        for(int i=1;i<=m;i++)
        {
            LL x,y;
            cin>>x>>y;
            if(x==1) yl[y]++;
            else if(x==2) rd[y]++;
            else if(x==3)
            {
                if(yl[y]>=2||rd[y]>=1) cout<<"Yes"<<endl;
                else cout<<"No"<<endl;
            }
        }
    }
    return 0;
}

C - Four Variables(贪心/思维)

题目大意:

让我们求A*B+C*D==N(2≤N≤2×10e5)的个数。
Sample Input 1  
4
Sample Output 1  
8 

(A,B,C,D)=(1,1,1,3)
(A,B,C,D)=(1,1,3,1)
(A,B,C,D)=(1,2,1,2)
(A,B,C,D)=(1,2,2,1)
(A,B,C,D)=(1,3,1,1)
(A,B,C,D)=(2,1,1,2)
(A,B,C,D)=(2,1,2,1)
(A,B,C,D)=(3,1,1,1)

直接四维暴力肯定是不行的,由于成2倍关系,所以我们可以优化成二维,再进行一个O(n)的判定运算。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18,MINN=-1e18;
const LL N=1e6+10,M=4010;
const LL mod=998244353;
const double PI=3.1415926535;
#define endl '\n'
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    int T=1;
    //cin>>T;
    while(T--)
    {
        LL n;
        cin>>n;
        map<LL,LL> mp;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j*i<=n;j++)
            {
                mp[i*j]++;
            }
        }
        LL sum=0;
        for(int i=1;i<=n;i++)
        {
            LL sum1=mp[i],sum2=mp[n-i];
            sum+=sum1*sum2;
        }
        cout<<sum<<endl;
    }
    return 0;
}

D - Unicyclic Components(并查集/图论)

题目大意:

给定n个点,m条边,问我们每一块图片中是不是都是点数==边数?

注意:可以有自环,可以有环,无向边,无向图。
Sample Input 1 
3 3
2 3
1 1
2 3
Sample Output 1 
Yes
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18,MINN=-1e18;
const LL N=1e6+10,M=4010;
const LL mod=998244353;
const double PI=3.1415926535;
#define endl '\n'
LL father[N],sum[N],num[N];//sum边数,num点数
int find(int x)
{
    if(father[x]!=x) father[x]=find(father[x]);
    return father[x];
}
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    int T=1;
    //cin>>T;
    while(T--)
    {
        LL n,m;
        cin>>n>>m;
        for(int i=1;i<=max(n,m);i++)
        {
            father[i]=i;
            sum[i]=0;
            num[i]=1;
        }
        for(int i=1;i<=m;i++)
        {
            LL u,v;
            cin>>u>>v;
            if(u==v)
            {
                LL fu=find(u);
                sum[fu]++;
            }
            else
            {
                LL fu=find(u);
                LL fv=find(v);
                if(fu!=fv)//没有连接过的,才需要进行运算
                {
                    father[max(fu,fv)]=min(fu,fv);
                    num[min(fu,fv)]+=num[max(fu,fv)];
                    sum[min(fu,fv)]+=sum[max(fu,fv)];
                }
                sum[min(fu,fv)]++;
            }
        }
        bool flag=true;
        for(int i=1;i<=n;i++)
        {
            LL fi=find(i);
            if(fi==i)
            {
                //边数 点数
                //cout<<i<<" "<<sum[i]<<" "<<num[i]<<endl;
                if(sum[i]!=num[i])
                {
                    flag=false;
                    break;
                }
            }
        }
        if(flag==false) cout<<"No"<<endl;
        else cout<<"Yes"<<endl;
    }
    return 0;
}
posted @ 2023-03-07 17:46  高尔赛凡尔娟  阅读(8)  评论(1编辑  收藏  举报