Codeforces Round #698 (Div. 2)
目录
A. Nezzar and Colorful Balls
直接求出现次数最多的数字的个数就好了。
// Codeforces Round #698 (Div. 2)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
void solve()
{
int n;
cin >> n;
int t = 0, cnt = 0;
int ans = 0;
while (n--) {
int x; cin >> x;
if (x == t) ++cnt;
else ans = max(ans, cnt), t = x, cnt = 1;
}
ans = max(ans, cnt);
cout << ans << endl;
}
int main()
{
int t;
scanf("%d", &t);
while (t--) {
solve();
}
return 0;
}
B. Nezzar and Lucky Number
\(a_i\ge10d\) 时, \(a_i\) 必为 \(lucky\ number\) ;否则暴力判断 \(a_i\) 是否为 \(lucky\ number\) 之和。
// Codeforces Round #698 (Div. 2)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
void solve()
{
int q, d;
scanf("%d%d", &q, &d);
while (q--) {
int x;
scanf("%d", &x);
if (x>=d*10) { // 考虑不周,导致WA了三四次。
puts("YES");
continue;
}
bool flag = false;
for (int i = 1; i <= 10; ++i) {
int t = x-d*i;
if (t<0) break;
if (t%10 == 0) {
flag = true;
break;
}
}
puts(flag?"YES":"NO");
}
}
int main()
{
int t;
scanf("%d", &t);
while (t--) {
solve();
}
return 0;
}
C. Nezzar and Symmetric Array
// Codeforces Round #698 (Div. 2)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5+5;
void solve()
{
int n;
scanf("%d", &n);
vector<LL> v(2*n);
for (auto&p:v) scanf("%lld", &p);
sort(v.begin(), v.end());
for (int i = 0; i < n; ++i) {
if ((v[2*i]&1) || v[2*i] != v[2*i+1]) {
puts("NO");
return;
}
v[i] = v[2*i];
}
LL sum = v[0]/2;
for (int i = n-1; i > 0; --i) v[i] -= v[i-1];
for (int i = 1; i < n; ++i) {
LL t = v[i] / (2*i);
if (v[i] % (2*i) || t == 0) {
puts("NO");
return;
}
sum -= t*(n-i);
if (sum <= 0) break;
}
if (sum > 0 && sum % n == 0) puts("YES");
else puts("NO");
}
int main()
{
int t;
scanf("%d", &t);
while (t--) {
solve();
}
return 0;
}
D. Nezzar and Board
注意到 \(2x-y\) 恰好是,\(y\) 关于 \(x\) 的对称点。
不断将坐标轴上已有的点关于其它点做对称,最终会在坐标轴上形成一系列间距相等的点,这个间距就是最初所有点间间距的最大公约数 。
最后判断 \(k\) 是否在这一系列点中即可。
// Codeforces Round #698 (Div. 2)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5+5;
void solve()
{
LL n, k;
scanf("%lld%lld", &n, &k);
vector<LL> x(n);
for (auto &p : x) scanf("%lld", &p);
sort(x.begin(), x.end());
LL gcd = 0;
for (int i = 0; i < n-1; ++i) {
gcd = __gcd(gcd, x[i+1]-x[i]);
}
puts((k-x[0])%gcd?"NO":"YES");
}
int main()
{
int t;
scanf("%d", &t);
while (t--) {
solve();
}
return 0;
}
E. Nezzar and Binary String(线段树)
采用逆向思维,从 \(f\) 出发,每次将区间 \([l_i, r_i]\) 变为全 \(0\) 或全 \(1\) 。
判断最终能否得到 \(s\) 即可。
涉及区间查询与修改,可用线段树实现,时间复杂度 \(O(n\log n)\) 。
// Codeforces Round #698 (Div. 2)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5+5;
int tree[N << 2], lazy[N << 2];
char s[N], f[N];
void build(int L, int R, int rt)
{
lazy[rt] = -1;
if (L == R) {
tree[rt] = f[L]^'0';
return;
}
int m = (L+R)>>1;
build(L, m, rt<<1);
build(m+1, R, rt<<1|1);
tree[rt] = tree[rt<<1]+tree[rt<<1|1];
}
void pushDown(int L, int R, int rt)
{
if (lazy[rt] == -1) return;
tree[rt] = lazy[rt]*(R-L+1);
if (L != R) {
int m = (L+R)>>1;
lazy[rt<<1] = lazy[rt<<1|1] = lazy[rt];
tree[rt<<1] = lazy[rt]*(m-L+1);
tree[rt<<1|1] = lazy[rt]*(R-m);
}
lazy[rt] = -1;
}
int query(int x, int y, int L, int R, int rt)
{
pushDown(L, R, rt);
if (x <= L && R <= y) {
return tree[rt];
}
int ans = 0;
int m = (L+R)>>1;
if (x<=m) ans += query(x, y, L, m, rt<<1);
if (y>m) ans += query(x, y, m+1, R, rt<<1|1);
return ans;
}
void update(int x, int y, int val, int L, int R, int rt)
{
if (x <= L && R <= y) {
lazy[rt] = val;
tree[rt] = val*(R-L+1);
return;
}
pushDown(L, R, rt);
int m = (L+R)>>1;
if (x<=m) update(x, y, val, L, m, rt<<1);
if (y>m) update(x, y, val, m+1, R, rt<<1|1);
tree[rt] = tree[rt<<1]+tree[rt<<1|1];
}
bool ok(int L, int R, int rt)
{
pushDown(L, R, rt);
if (L == R) {
return tree[rt] == (s[L]^'0');
}
bool flag = true;
int m = (L+R)>>1;
flag &= ok(L, m, rt<<1);
flag &= ok(m+1, R, rt<<1|1);
return flag;
}
struct node {int x, y;};
void solve()
{
int n, q;
scanf("%d%d%*c", &n, &q);
gets(s+1), gets(f+1);
build(1,n,1);
vector<node> v(q);
for (auto &t : v) scanf("%d%d", &t.x, &t.y);
for (int i = q-1; i >= 0; --i) {
node &t = v[i];
int sum = query(t.x, t.y, 1, n, 1);
int total = t.y-t.x+1;
if (sum == 0 || sum == total) continue;
if (sum*2 == total) {
// cout << "#" << i << '#' << t.x << '#' << t.y << '#' << sum << endl;
puts("NO");
return;
}
// cout << i << '#' << sum << '#' << t.x << '#' << t.y << '#' << (sum*2>total) << endl;
update(t.x, t.y, sum*2>total, 1, n, 1);
}
puts(ok(1, n, 1)?"YES":"NO");
}
int main()
{
int t;
scanf("%d", &t);
while (t--) {
solve();
}
return 0;
}
F. Nezzar and Nice Beatmap
从任意点开始,每次选择距离最远的点(与三角形最长边相邻的两个角必为锐角)。
// Codeforces Round #698 (Div. 2)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
struct node {int x, y;};
LL dis(node a, node b)
{
return 1LL*(a.x-b.x)*(a.x-b.x)+1LL*(a.y-b.y)*(a.y-b.y);
}
void solve()
{
int n;
scanf("%d", &n);
vector<node> v(n);
vector<bool> vis(n);
for (auto &t : v) scanf("%d%d", &t.x, &t.y);
vector<int> ans;
ans.push_back(0); vis[0] = 1;
int pre = 0;
for (int i = 1; i < n; ++i) {
LL d = 0, p;
for (int j = 1; j < n; ++j) {
if (vis[j]) continue;
LL td = dis(v[pre], v[j]);
if (td > d) d = td, p = j;
}
ans.push_back(p); vis[p] = 1;
pre = p;
}
cout << ans[0]+1;
for (int i = 1; i < n; ++i) cout << ' ' << ans[i]+1;
cout << endl;
}
int main()
{
int t = 1;
// scanf("%d", &t);
while (t--) {
solve();
}
return 0;
}