Codeforces Round 898 (Div. 4)


 

Dashboard - Codeforces Round 898 (Div. 4) - Codeforces

F. Money Trees

双指针(需要细心)

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
void solve()
{
    int n, k, ans = 0, a[N], h[N];
    cin >> n >> k;
    for(int i = 0; i < n; i ++)cin >> a[i];
    for(int i = 0; i < n; i ++)cin >> h[i];
    for(int i = 0; i < n; i ++)
    {
    	int l = i, r = i;
    	while(r + 1 < n && h[r] % h[r + 1] == 0)r ++;
    	int sum = 0;
    	for(int i = l; i <= r; i ++)
    	{
    		sum += a[i];
    		while(sum > k && l <= i)
			{
				sum -= a[l];
				l ++;
			}
			if(sum <= k)ans = max(ans, i - l + 1);
		}
		i = r;
	}
	cout << ans << '\n';
}
int main()
{
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int t;
	cin >> t;
	while(t --)
	{
		solve();
	}
	return 0;
}

G. ABBC or BACB

会发现B可以改变连续一段的A,那连续的一段有几个A就可以获得几枚硬币

 故我们可以把连续A的个数记录在数组中,将数组中的数从大到小排序,将数字相加,如果循环到的位数大于B的个数也需要停止,此时没有B让这些A发生改变,(这里应该就是首尾空开的位置)

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, a[N];
bool cmp(int x, int y)
{
	return x > y;
}
bool solve()
{
	string s;
	cin >> s;
	int ans = 0, cnt = 0, num = 0;
	int n = s.size();
	for(int i = 0; i < n; i ++)
	{
		if(s[i] == 'B')num ++;
	}
	for(int i = 0; i < n; i ++)
	{
		if(s[i] == 'A')
		{
			int j = i;
			while(j < n && s[j] != 'B')
			{
				j ++;
			}
			a[++ cnt] = j - i;
			i = j - 1;
		}
	}
	sort(a + 1, a + 1 + cnt, cmp);
	for(int i = 1; i <= cnt && i <= num; i ++)ans += a[i];
	cout << ans << '\n';
}
int main()
{
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int t;
	cin >> t;
	while(t --)
	{
		solve();
	}
	return 0;
}

H. Mad City

能抓到的前提是一开始就不在环上,如果在环上两个人就会不断绕着环跑,先在环上的人一定能赢

猜你喜欢

转载自blog.csdn.net/m0_75087931/article/details/133189772