牛客周赛Round1

1.游游画U

A-游游画U_牛客周赛 Round 1 (nowcoder.com)

题目意思:按照题目给的例子来画U

思路:将'U'分成两部分,一部分是上面不变的竖线,第二部分是‘U’变化的底部。接着按照这两部分的规律写出代码就可以了

#include <iostream>
using namespace std;

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

2.游游数组染色

B-游游的数组染色_牛客周赛 Round 1 (nowcoder.com)

题目意思:数字相同而颜色不同的两组算为一种取法,求一共有多少种取法

思路:用map将不同颜色分成两组,map记录的是数字的类型和该类型的数量,最后用

for(auto i : rp)遍历其中一个颜色的map,再二者相乘得到的值就是要求的。

总结:for(auto i : rp)中,即使遍历的是map,i依旧可以遍历。

#include <iostream>
#include <set>
#include <map>
#include <vector>
using namespace std;

map<int, int> rp;
map<int, int> bp;
vector<int> a;
long long nums = 0;

int main()
{
    int n, e;
    cin >> n;

    for (int i = 0; i < n; i++) {
        cin >> e;
        a.push_back(e);
    }
    string s;
    cin >> s;


    for (int i = 0; i < n; i++) {
        if(s[i] == 'R') rp[a[i]]++;
        else bp[a[i]]++;
    }
    
    for (auto i : rp) {
        nums += i.second * bp[i.first];
    }

    cout << nums << endl;
    return 0;

3.游游的交换字符

C-游游的交换字符_牛客周赛 Round 1 (nowcoder.com)

题目意思:拿到一个仅由‘0’和‘1’组成的字符串,每次交换相邻的两个字符,求交换多少次可以使相邻的两个字符不等

eg.从11001,最后变成10101

思路:由题意我们可以发现‘0’的数量和‘1’的数量的绝对值差值不会大于1,所以我们总体可以从字符串长度为奇数还是偶数来考虑

奇数情况:

1.101010101

2.010101010

偶数情况:

1.1010101010

2.0101010101

观察上图我们可以发现,其实每个数都有对应的位置来达到移动的次数最少。故我们可以选定1在前还是0在前然后通过位置的差值的累加(i * 2 - a[i])来计算需要移动多少次。//a[i]记入的是下标

代码如下:

#include <iostream>
#include <vector>
using namespace std;

typedef long long ll;

ll nums, res = 0;

int main()
{
	string s;
	cin >> s;
	ll n = s.size();


	for (int i = 0; i < n; i++) {
		if (s[i] == '1') nums++;
	}

	if (n & 1) {//判断长度奇偶
		vector<int> a(n);
		char c;
		if (n / 2 + 1 == nums) c = '1';
		else c = '0';

		int k = 0;
		for (int i = 0; i < n; i++) {//登记1或0的位置
			if (s[i] == c) a[k++] = i;
		}
		for (int i = 0; i < k; i++) {//计算每个c的移位
			res += abs(i * 2 - a[i]);
		}
		cout << res << endl;
	}
	else {
		vector<int> a(n);
		vector<int> b(n);
		int k = 0;
		for (int i = 0; i < n; i++) {
			if (s[i] == '1') a[k++] = i;
		}
		
		for (int i = 0; i < k; i++) {
			res += abs(i * 2 - a[i]);
		}
		ll res1 = 0;
		for (int i = 0; i < k; i++) {
			res1 += abs(i * 2 + 1 - a[i]);
		}
		cout << min(res, res1) << endl;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值