codeforces round 806 div4

文章展示了C++编程中的几个实例,涉及字符串处理、字符转换、数组操作、计数、映射使用以及简单的算法技巧,如暴力枚举、数学变换和区间查询。
摘要由CSDN通过智能技术生成

A-C思路很简单,直接贴代码

#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false); cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        string s;
        cin >> s;
        for (int i = 0; i < 3; i++)
        {
            if ('a' <=  s[i] && s[i] <= 'z')s[i] -= 32;
        }
        if (s == "YES")puts("YES");
        else puts("NO");
    }
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int N=60;
int cnt[N];
int main()
{
    ios::sync_with_stdio(false); cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        memset(cnt,0,sizeof cnt);
        int n;
        cin>>n;
        string s;
        cin>>s;
        for(int i=0;i<n;i++)
        {
            cnt[s[i]-'A'+1]++;
        }
        long long res=0;
        for(int  i=1;i<=26;i++)
        {
            if(cnt[i]>0)res+=2+(cnt[i]-1);
        }
        cout<<res<<endl;
    }
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int N=15;
int a[N];

int main()
{
    ios::sync_with_stdio(false); cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)cin>>a[i];
        for(int i=1;i<=n;i++)
        {
            int cnt;
            cin>>cnt;
            string s;
            cin>>s;
            for(int j=0;j<s.size();j++)
            {
                if(s[j]=='D')
                {
                    a[i]++;
                    if(a[i]==10)a[i]=0;
                }
                else 
                {
                    a[i]--;
                    if(a[i]==-1)a[i]=9;
                }
            }
        }
        for(int i=1;i<=n;i++)cout<<a[i]<<' ';
        cout<<endl;
    }
    return 0;
}

D. Double Strings

关键点在与字符串的长度只有8,我们可以暴力枚举每个字符串的分割位置,看一下两部分字符串是否存在,可以用map存一下是否存在

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
string s[N];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;cin>>n;
        map<string,int>map;
        for(int i=0;i<n;i++)
        {
            cin>>s[i];
            map[s[i]]=1;
        }
        for(int i=0;i<n;i++)
        {
            bool flag=false;
            for(int j=1;j<s[i].size();j++)
            {
                string a=s[i].substr(0,j);
                string b=s[i].substr(j);
                if(map[a]&&map[b])
                {
                    flag=true;
                    break;
                }
            }
            if(flag)cout<<1;
            else cout<<0;
        }
        cout<<endl;
    }
    return 0;
}

E. Mirror Grid

数学知识:

原始点a[i][j]

旋转90度:a[j][n-i-+1]

旋转180 :a[n-j+1][i]

旋转270:a[n-i+1][n-j+1]

取这四个点中1或者0个数的最小值,然后记得将这四个点标记为已经遍历

#include<bits/stdc++.h>
using namespace std;
const int N=110;
int cnt1,cnt2;
char a[N][N];
int main()
{
    ios::sync_with_stdio(false); cin.tie(0);
    int t;
    cin >> t;

    while (t--)
    {
        int n;
        cin>>n;

        for(int i=1;i<=n;i++)cin>>a[i]+1;
        int res=0;

        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(a[i][j]!='3')
                {
                    int cnt=0;
                    cnt+=a[i][j]+a[n-j+1][i]+a[j][n-i+1]+a[n-i+1][n-j+1]-4*'0';
                    res+=min(cnt,4-cnt);
                    a[i][j]=a[n-j+1][i]=a[j][n-i+1]=a[n-i+1][n-j+1]='3';
                }
            }
            
        }
        cout<<res<<endl;
    }
    return 0;
}

F. Yet Another Problem About Pairs Satisfying an Inequality

对于数组中的数,我们只需要考虑满足a[i]<i的数。对于当前数字a[j],找到下标满足i<a[j]的个数,累计,最后将当前数的下标加入即可。由于我们是按照下标从小到大枚举的,因此我们可以用vector存储满足a[i]<i的下标i,用lower_bound查找即可(lower_bound返回第一个大于等于a[j]的位置,再减去v.begin,就是满足i<a[j]的个数)。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=2e5+10;
int main()
{
    ios::sync_with_stdio(false); cin.tie(0);
    int t;
    cin >> t;

    while (t--)
    {
        int n;cin>>n;
        vector<int>v;
        LL res=0;
        for(int i=1;i<=n;i++)
        {
            int x;
            cin>>x;
            if(x<i)
            {
                res+=lower_bound(v.begin(),v.end(),x)-v.begin();
                v.push_back(i);
            }
        }
        cout<<res<<endl;

    }
    return 0;
}



G题先留着

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值