Codeforces Round 986 (Div. 2) A-C

LiangSue發表於2024-11-26

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, y-1)\); or
  • go west (represented by W), moving to \((x-1, 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\) (\(1 \le t \le 500\)). The description of the test cases follows.

The first line of each test case contains three integers \(n\), \(a\), \(b\) (\(1 \le n\), \(a\), \(b \le 10\)) — 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.

大致思路:本題資料範圍 $ 1 \le n,a , b \le 10$ 非常小,故判斷是否能達到目的地或陷入死迴圈,只需要迴圈足夠多的次數,若迴圈結束仍未到達終點,則認為無法到達,輸出 \(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 \(a_i = b\cdot (i - 1) + c\) for \(1\le i\le n\). For example, if \(n=3\), \(b=2\), and \(c=1\), then \(a=[2 \cdot 0 + 1, 2 \cdot 1 + 1, 2 \cdot 2 + 1] = [1, 3, 5]\).

Now, Alice really enjoys permutations of \([0, \ldots, n-1]\)\(^{\text{∗}}\) and would like to transform \(a\) into a permutation. In one operation, Alice replaces the maximum element of \(a\) with the \(\operatorname{MEX}\)\(^{\text{†}}\) 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.

\(^{\text{∗}}\)A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(0\) to \(n-1\) 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).

\(^{\text{†}}\)The \(\operatorname{MEX}\) of an array is the smallest non-negative integer that does not belong to the array. For example, the \(\operatorname{MEX}\) of \([0, 3, 1, 3]\) is \(2\) and the \(\operatorname{MEX}\) of \([5]\) is \(0\).

Input

Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^5\)). The description of the test cases follows.

The only line of each test case contains three integers \(n\), \(b\), \(c\) (\(1\le n\le 10^{18}\); \(0\le b\), \(c\le 10^{18}\)) — 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.

大致思路:首先由於 \(a_i = b\cdot (i - 1) + c\).

  • \(c >= n\), 則對所有的 \(i\),都有 \(a_i \ge n\),所以需要 \(n\) 次操作。

  • \(c < n\),此時需要考慮 \(b\)

    • \(b = 0\), 則 \(a_i = c\). 此時若 \(c >= n-2\),只需要 \(n - 1\) 次操作. (因為此時 \(c< n\) 了). 若 \(c < n - 2\),則無解。
    • \(b \neq 0\), 則令 \(b\cdot (i-1) + c = n - 1\), 解得 \(i = \frac{n - 1 -c}{b} + 1\), 意為此時 \(a_i\) 剛好等於 \(n - 1\),則對於\(a_j\;(j>i)\) 都會大於 \(n-1\),均需要刪除. 故需要 \(n - i\) 次操作。

程式碼如下

#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 \(a_1, a_2, \ldots, a_n\). 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\) (\(1 \le t \le 10^4\)). The description of the test cases follows.

The first line of each test case contains three integers \(n, m, v\) (\(1\le m\le n\le 2\cdot 10^5\); \(1\le v\le 10^9\)) — the number of sections, the number of creatures, and the creatures' minimum requirement for tastiness, respectively.

The next line contains \(n\) space separated integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) — the tastinesses of the sections.

The sum of \(n\) over all test cases does not exceed \(2\cdot 10^5\).
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\) 個區間滿足它們每一個的區間和 \(\ge v\), 輸出剩餘的一個區間和的最大值 (可以為 \(0\))。

大致思路

\((prefix) \; pfx[i]\): 表示在前 \(i\) 個位置時,可以劃分的區間數;\((suffix) \; sfx[j]\): 表示在後 \(j\) 個位置時,可以劃分的區間數。假設 \(Alice\) 得到的區間為 \(a[i:j]\),則此時 \(pfx[i] + sfx[j] \ge m\). 移項得: \(sfx[j] \ge m - pfx[i]\). 由於\(sfx\) 陣列單調,則對於每一個 \(i\) 都可以透過二分找到滿足條件的 \(j\).

\(res = -1\) 為最終答案,\(sum\) 為字首和陣列.

  • \(pfx[i] \ge m\), \(res = max(res, sum[n] - sum[i])\).
  • \(sfx[i] \ge m\), \(res = max(res, sum[n-i])\)
  • 否則:二分找到第一個大於等於 \(m - pfx[i]\)\(sfx[j]\),如果 \(i<j\), \(res = max\{res,sum[n-j]-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();
}

相關文章