Codeforces Round 986 (Div. 2) A-C

A. Alice's Adventures in "Chess"

Alice is trying to meet up with the Red Queen in the countryside! Right now, Alice is at position (0,0), and the Red Queen is at position (a,b). Alice can only move in the four cardinal directions (north, east, south, west).

More formally, if Alice is at the point (x,y), she will do one of the following:

  • go north (represented by N), moving to (x,y+1);
  • go east (represented by E), moving to (x+1,y);
  • go south (represented by S), moving to (x,y1); or
  • go west (represented by W), moving to (x1,y).

Alice's movements are predetermined. She has a string s representing a sequence of moves that she performs from left to right. Once she reaches the end of the sequence, she repeats the same pattern of moves forever.

Can you help Alice figure out if she will ever meet the Red Queen?

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1t500). The description of the test cases follows.

The first line of each test case contains three integers n, a, b (1n, a, b10) — the length of the string and the initial coordinates of the Red Queen.

The second line contains a string s of length n consisting only of the characters N, E, S, or W.

Output

For each test case, output a single string "YES" or "NO" (without the quotes) denoting whether Alice will eventually meet the Red Queen.

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

大致思路:本题数据范围 1n,a,b10 非常小,故判断是否能达到目的地或陷入死循环,只需要循环足够多的次数,若循环结束仍未到达终点,则认为无法到达,输出 NO ,否则认为可以到达,输出 YES

代码如下

#include<bits/stdc++.h>
using namespace std;
#define long long ll
const int N = 2e5+5;

void fio()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
void solution()
{
	int n,a,b;
	cin >> n >> a >> b;
	int x = 0,y = 0;
	string s;
	cin >> s;
	int times = 0;
	while(1){
		times++;
		if(times > 100)
		{
			cout<<"NO"<<endl;
			return ;
		}
		for(int i=0;i<s.size();i++)
		{
			if(s[i] == 'N')x = x + 1;
			if(s[i] == 'E')y = y + 1;
			if(s[i] == 'S')x = x - 1;
			if(s[i] == 'W')y = y - 1;
			if(x == a && y == b)
			{
				cout << "YES" <<endl;
				return;
			}
		}
	}
}
int main()
{
	fio();
	int t; cin >> t;
	while(t--)
	{
		solution();
	}
}

B. Alice's Adventures in Permuting

Alice mixed up the words transmutation and permutation! She has an array a specified via three integers n, b, c: the array a has length n and is given via ai=b(i1)+c for 1in. For example, if n=3, b=2, and c=1, then a=[20+1,21+1,22+1]=[1,3,5].

Now, Alice really enjoys permutations of [0,,n1] and would like to transform a into a permutation. In one operation, Alice replaces the maximum element of a with the MEX of a. If there are multiple maximum elements in a, Alice chooses the leftmost one to replace.

Can you help Alice figure out how many operations she has to do for a to become a permutation for the first time? If it is impossible, you should report it.

A permutation of length n is an array consisting of n distinct integers from 0 to n1 in arbitrary order. Please note, this is slightly different from the usual definition of a permutation. For example, [1,2,0,4,3] is a permutation, but [0,1,1] is not a permutation (1 appears twice in the array), and [0,2,3] is also not a permutation (n=3 but there is 3 in the array).

The MEX of an array is the smallest non-negative integer that does not belong to the array. For example, the MEX of [0,3,1,3] is 2 and the MEX of [5] is 0.

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1t105). The description of the test cases follows.

The only line of each test case contains three integers n, b, c (1n1018; 0b, c1018) — the parameters of the array.

Output

For each test case, if the array can never become a permutation, output 1. Otherwise, output the minimum number of operations for the array to become a permutation.

大致思路:首先由于 ai=b(i1)+c.

  • c>=n, 则对所有的 i,都有 ain,所以需要 n 次操作。

  • c<n,此时需要考虑 b

    • b=0, 则 ai=c. 此时若 c>=n2,只需要 n1 次操作. (因为此时 c<n 了). 若 c<n2,则无解。
    • b0, 则令 b(i1)+c=n1, 解得 i=n1cb+1, 意为此时 ai 刚好等于 n1,则对于aj(j>i) 都会大于 n1,均需要删除. 故需要 ni 次操作。

代码如下

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5+5;
ll a[N];
void fio()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
 
void solution()
{
	ll n,b,c;
	cin >> n >> b >> c;
	if(n <= c)cout << n << endl;
	else 
	{
		if(b == 0)
		{
			if(c >= n - 2) cout << n - 1 <<endl;
			else cout << -1 << endl;
		}
		else cout << n - ( 1 + (n - c - 1) / b) <<endl;
		
	}
}
int main()
{
	fio();
	int t; cin >> t;
	while(t--)solution();
}


C. Alice's Adventures in Cutting Cake

Alice is at the Mad Hatter's tea party! There is a long sheet cake made up of n sections with tastiness values a1,a2,,an. There are m creatures at the tea party, excluding Alice.

Alice will cut the cake into m+1 pieces. Formally, she will partition the cake into m+1 subarrays, where each subarray consists of some number of adjacent sections. The tastiness of a piece is the sum of tastiness of its sections. Afterwards, she will divvy these m+1 pieces up among the m creatures and herself (her piece can be empty). However, each of the m creatures will only be happy when the tastiness of its piece is v or more.

Alice wants to make sure every creature is happy. Limited by this condition, she also wants to maximize the tastiness of her own piece. Can you help Alice find the maximum tastiness her piece can have? If there is no way to make sure every creature is happy, output 1.

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1t104). The description of the test cases follows.

The first line of each test case contains three integers n,m,v (1mn2105; 1v109) — the number of sections, the number of creatures, and the creatures' minimum requirement for tastiness, respectively.

The next line contains n space separated integers a1,a2,,an (1ai109) — the tastinesses of the sections.

The sum of n over all test cases does not exceed 2105.
Output

For each test case, output the maximum tastiness Alice can achieve for her piece, or 1 if there is no way to make sure every creature is happy.

大致题意: 将一个长度为 n 数组分为 m+1 个区间,其中 m 个区间满足它们每一个的区间和 v, 输出剩余的一个区间和的最大值 (可以为 0)。

大致思路

(prefix)pfx[i]: 表示在前 i 个位置时,可以划分的区间数;(suffix)sfx[j]: 表示在后 j 个位置时,可以划分的区间数。假设 Alice 得到的区间为 a[i:j],则此时 pfx[i]+sfx[j]m. 移项得: sfx[j]mpfx[i]. 由于sfx 数组单调,则对于每一个 i 都可以通过二分找到满足条件的 j.

res=1 为最终答案,sum 为前缀和数组.

  • pfx[i]m, res=max(res,sum[n]sum[i]).
  • sfx[i]m, res=max(res,sum[ni])
  • 否则:二分找到第一个大于等于 mpfx[i]sfx[j],如果 i<j, res=max{res,sum[nj]sum[i]}.

代码如下

#include<bits/stdc++.h>
using namespace std;
#define ll long long 
const int N = 2e5+5;


void fio()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}

int x[N],sfx[N],pfx[N]; 
ll lsum[N],rsum[N];
void solution()
{
	int n,m,v;
	cin >> n >> m >> v;
	for(int i = 1;i <= n;i++) cin >> x[i],lsum[i] = lsum[i - 1] + x[i];
	ll temp = 0; int cnt = 0;
	for(int i = 1;i <= n;i++)
	{
		if(lsum[i] - temp >= v) cnt ++,temp  = lsum[i];
		pfx[i] = cnt ;
	}
	reverse(x+1,x+1+n);
	temp = 0;cnt = 0;
	for(int i = 1 ;i <= n; i++)
	{
		rsum[i] = rsum[i - 1] + x[i];
		if(rsum[i] - temp >= v)cnt ++ , temp = rsum[i];
		sfx[i] = cnt;
	}
	ll res = -1;
	for(int i = 1; i <= n; i++)
	{
		if(m - pfx[i] <= 0)res = max(res,lsum[n] - lsum[i]);
		if(m - sfx[i] <= 0) res = max(res,lsum[n - i]);
		int j = lower_bound(sfx+1,sfx+1+n, m - pfx[i]) - sfx;
		j = n + 1 - j;
		if(i < j) res = max(res,lsum[j - 1] - lsum[i]);
		
	}
	cout << res << endl;
}

int main()
{
	fio();
	int t; cin >> t;
	while(t--)solution();
}
posted @   LiangSue  阅读(21)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示