Codeforces Round #828 (Div. 3)补题(C,D)

Bonsoir.(*^_^*)

目录

题目:Problem - C - Codeforces

题目:Problem - D - Codeforces


题目:Problem - C - Codeforces

题意:给定一个字符串,由r,y,g三个字母组成,是一个循环串(前后相通),要求给定一个字符c,一定会是三个字母中的一个,给定一个字符串s,要求字符串s中s[i]==c的所有字母中距离其中任意一个'g'最远的距离。

如:

解题思路:

循环串可以把字符串s再复制一遍,再从头往后遍历,简单拿cnt记住字符c到g的距离就好了。(比赛的时候怎么没想到呢

#include <bits/stdc++.h>
#define ll long long
#define sz size()
#define endl '\n'
using namespace std;
typedef pair<int, int> PII;


int main()
{
	io;
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		char c;
		cin >> n >> c;
		for (int i = 0; i < n; i++)
			f[i] = 0;
		string s;
		cin >> s;
		if (c == 'g')
		{
			cout << "0" << endl;
			continue;
		}
		s += s;
		int ans = -1, cnt = 0;
		bool flag = 0;
		for (int i = 0; i < s.sz; i++)
		{
			if (s[i] == c)
				flag = 1;
			if (s[i] == 'g')
				flag = 0, ans = max(ans, cnt), cnt = 0;
			if (flag == 1)
				cnt++;
		}
		cout << ans << endl;
	}
	return 0;
}

题目:Problem - D - Codeforces

题意:给定一个数组a,定义一个操作使得a[i] = a[i] * i,每个i只能操作一次,求找到让数组所有元素的乘积能被2ⁿ整除的最小操作次数。

思路:找每个数有多少个2的因子,当ans>=  n时即可。若需要操作(ans < n),将下标i的2的因子个数存入数组,贪心加入ans。

#include <bits/stdc++.h>
#define ll long long
#define sz size()
#define endl '\n'
using namespace std;
typedef pair<int, int> PII;

const int N = 2e5 + 10;
ll a[N];
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		for (int i = 1; i <= n; i++)
			a[i] = 0;
		ll ans = 0;
		for (int i = 1; i <= n; i++)
		{
			int x;
			cin >> x;
			while (x % 2 == 0)
			{
				ans++;
				x /= 2;
			}
			int cnt = i;
			while (cnt % 2 == 0)
			{
				a[i]++;
				cnt /= 2;
			}
		}
		if (ans >= n)
		{
			cout << "0\n";
			continue;
		}
		bool flag = 0;
		sort(a + 1, a + n + 1, greater<int>());
		for (int i = 1; i <= n; i++)
		{
			ans += a[i];
			if (ans >= n)
			{
				flag = 1;
				cout << i << endl;
				break;
			}
		}
		if (!flag)
			cout << -1 << endl;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值