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
More formally, if Alice is at the point
- go north (represented by N), moving to
; - go east (represented by E), moving to
; - go south (represented by S), moving to
; or - go west (represented by W), moving to
.
Alice's movements are predetermined. She has a string
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
The first line of each test case contains three integers
The second line contains a string
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.
大致思路:本题数据范围
代码如下:
#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
Now, Alice really enjoys permutations of
Can you help Alice figure out how many operations she has to do for
Input
Each test contains multiple test cases. The first line contains the number of test cases
The only line of each test case contains three integers
Output
For each test case, if the array can never become a permutation, output
大致思路:首先由于
-
若
, 则对所有的 ,都有 ,所以需要 次操作。 -
若
,此时需要考虑 : -
- 若
, 则 . 此时若 ,只需要 次操作. (因为此时 了). 若 ,则无解。
- 若
-
- 若
, 则令 , 解得 , 意为此时 刚好等于 ,则对于 都会大于 ,均需要删除. 故需要 次操作。
- 若
代码如下:
#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
Alice will cut the cake into
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
Input
Each test contains multiple test cases. The first line contains the number of test cases
The first line of each test case contains three integers
The next line contains
The sum of
Output
For each test case, output the maximum tastiness Alice can achieve for her piece, or
大致题意: 将一个长度为
大致思路:
令
令
- 若
, . - 若
, - 否则:二分找到第一个大于等于
的 ,如果 , .
代码如下:
#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();
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具