Bonsoir.(*^_^*)
目录
题目: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;
}