牛客周赛 Round 1

A-游游画U

在这里插入图片描述

题解:找规律即可

实现代码

#include <iostream>

using namespace std;

int main()
{
    int n;
    cin >> n;
    
    for(int i = 1 ; i <= 4 * n - n - 1 ; i ++ )
    {
        if(i <= 4 * n - n - 1)
        {
            for(int j = 1 ; j <= n ; j ++ ) cout << "*";
            for(int j = 1 ; j <= n * 2 ; j ++ ) cout << ".";
            for(int j = 1 ; j <= n ; j ++ ) cout << '*';
        }
        cout << endl;
    }
    for(int i = 0 ; i < n + 1 ; i ++ )
    {
        for(int j = 0 ; j < i ; j ++ ) cout << '.';
        for(int j = 0 ; j < n ; j ++ ) cout << '*';
        for(int j = 0 ; j < 2 * n - i * 2 ; j ++ ) cout << ".";
        for(int j = 0 ; j < n ; j ++ ) cout << '*';
        for(int j = 0 ; j < i ; j ++ ) cout << '.';
        cout << endl;
    }
}

B–游游的数组染色

在这里插入图片描述

题解:要求我们找到不同颜色但相同数值的取法,那么先用map存取数值中两种颜色各为多少,然后在相乘,最后将所有的叠加。

实现代码

#include <iostream>
#include <map>

using namespace std;

const int N = 200010;

typedef long long LL;

int n;
LL a[N];
map<LL , LL> mp , mp1;

int main()
{
    int n;
    cin >> n;
    
    for(int i = 0 ; i < n ; i ++ ) cin >> a[i];
    string s;
    cin >> s;
    
    LL sum = 0 , cnt = 0;
    for(int i = 0 ; i < n ; i ++ )
        if(s[i] == 'R') mp[a[i]] ++;
        else mp1[a[i]] ++ ;
    
    long long ans = 0;
    for(auto [x , y] : mp) ans += y * mp1[x];
    
    cout << ans << endl;
}

C-- 游游的交换字符

在这里插入图片描述

题解:首先需要我们01字符串相邻不同,所以只有两种可能,10101010…
或者01010101… , 当n为奇数时,放在首位的一定是多的那个数,比如10101 ,01010 , 当偶数时,分别计算取最小值,首先记录每个1的位置,然后计算将其放到指定位置时需要几步,叠加就是最优解的答案

实现代码

#include <iostream>
#include <cstring>
#include <vector>

using namespace std;

typedef long long LL;

int main()
{
    string s;
    cin >> s;
    
    LL n = s.size();
    LL sum = 0;
    
    for(int i = 0 ; i < n ; i ++ ) if(s[i] == '1') sum ++;
    
    if(n & 1)
    {
        vector<LL> a(n);
        char c;
        if(sum == n / 2 + 1) c = '1';
        else c = '0';
        
        LL k = 0;
        for(LL i = 0 ; i < n ; i ++ )
            if(s[i] == c) a[k ++ ] = i;
        LL ans = 0;
        for(LL i = 0 ; i < k ; i ++ )
            ans += abs(2 * i - a[i]);
        
        cout << ans << endl;
    }else 
    {
        LL ans = 0 , res = 0;
        vector<LL> a(n);
        LL k = 0;
        for(LL i = 0 ; i < n ; i ++ ) if(s[i] == '1') a[k ++ ] = i;
        for(LL i = 0 ; i < k ; i ++ )
            ans += abs(2 * i - a[i]);
        
        for(LL i = 0 ; i < k ; i ++ )
            res += abs(2 * i + 1 - a[i]);
        
        cout << min(ans , res) << endl;
    }
    
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值