Codeforces Global Round 17
A. Anti Light’s Cell Guessing
分析:
考虑0的情况就行了。
代码:
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize(2)
#define close(); ios::sync_with_stdio(false);
#define endl '\n'
#define rep(i, l, r) for(int i = l; i <= r; i++)
#define dwn(i, r, l) for(int i = r; i >= l; i--)
typedef long long LL;
const int N = 3e5+100;
void solve()
{
int a, b; cin >> a >> b;
if(a > b) swap(a, b);
if(a == 1 && b == 1) cout << 0 << endl;
else if(a == 1) cout << 1 << endl;
else cout << 2 << endl;
}
int main()
{
int T; cin >> T;
while(T--) solve();
// system("pause");
}
B. Kalindrome Array
分析:
不妨记字符串为s, 长度为m.
分以下情况讨论:
- 本身是回文串, 输出yes
- 本身不是回文串, 那么一定存在一对 s i ! = s j , i = m + 1 − j s_i!=s_j, i = m+1-j si!=sj,i=m+1−j.
- 考虑把 s i s_i si类的字符全部删掉, 再看一遍是不是回文串.
- 考虑把 s j s_j sj类的字符全部删掉, 再看一遍是不是回文串.
为什么是将其类字符全部删掉呢, 这好像和题干有点不太一样. 实际上, 即便不删掉其类字符也是匹配的本身, 和全删掉的情况下判是否是回文串是等价的.
代码:
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize(2)
#define close(); ios::sync_with_stdio(false);
#define endl '\n'
#define rep(i, l, r) for(int i = l; i <= r; i++)
#define dwn(i, r, l) for(int i = r; i >= l; i--)
typedef long long LL;
const int N = 3e5+100;
int a[N];
bool check(int n, int x)
{
int l = 1, r = n;
while(l < r)
{
while(l < r && a[l] == a[x]) l++;
if(l >= r) break;
while(l < r && a[r] == a[x]) r--;
if(l >= r) break;
if(a[l] == a[r]) l++, r--;
else if(a[l] != a[r]) return 0;
}
return 1;
}
void solve()
{
int n; cin >> n;
rep(i, 1, n) cin >> a[i];
int l = 1, r = n;
int u, v; u = v = 0;
while(l < r)
{
if(a[l] == a[r]) l++, r--;
else
{
u = l;
v = r;
break;
}
}
bool f = 0;
if(u == v) f = 1;
else
{
f |= check(n, u);
f |= check(n, v);
}
if(f) cout << "YES\n";
else cout << "NO\n";
}
int main()
{
close();
int T; cin >> T;
while(T--) solve(

本文提供了Codeforces Global Round 17比赛中的四道题目A. Anti Light’s Cell Guessing、B. Kalindrome Array、C. Keshi Is Throwing a Party和D. Not Quite Lee的详细分析和解决方案。每道题目都包含了问题的思路解析和相应的代码实现,旨在帮助读者理解和解答这些问题。

425

被折叠的 条评论
为什么被折叠?



