文章目录
Codeforces Round #698 (Div. 2)
A. Nezzar and Colorful Balls
题意: 给定n个球,每个球有一个数字a[i]。现在要给所有的球染色,要求同一个颜色的球的数字a[i]单调递增。问最少需要多少的颜色才能将所有球染色完毕
题解: 思维。相同数字最多的球是多少个即可。
代码:
#include <bits/stdc++.h>
using namespace std;
int n, m, _;
int a[1000];
int main() {
cin >> _;
while(_--){
cin >> n;
for (int i = 0; i < n;i++) cin >> a[i];
int cnt = 1;
int res = 1;
for (int i = 1; i < n;i++){
if (a[i] == a[i - 1]) cnt++;
else cnt = 1;
res = max(res, cnt);
}
cout << res << endl;
}
return 0;
}
B. Nezzar and Lucky Number
题意: 给定x和d,如果x能够表示为若干个幸运数字的和,那么打印yes,否则no。定义幸运数字为某位上含有k的数字
题解: 思维。首先0 ~ 10 * d的个位上包含了0 ~ 10的所有数字,因此如果一个数字x能够找到一个k的倍数和他的个位相同,那么就能转换为t * d + m * 10, 比如k = 7,x = 58, 那么58 = 28 + 30 = 4 * 7 + 30, 那么我可以将一个7分到后面的30里面,则58 = 3 * 7 + 37 = 7 + 7 + 7 + 37。因此每次对于一个数字x,我只需要枚举一下0 * d ~ 10 * d即可,如果大于等于10 * d, 则一定可以,因为一定能枚举到某个d的倍数
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int const MAXN = 2e5 + 10;
int n, m, T;
int check(int n, int d){
if (n >= 10 * d) return 1;
for (int i = 1; i <= 10 && i * d <= n; ++i) {
if ((n - i * d) % 10 == 0) return 1;
}
return 0;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> T;
while(T--) {
cin >> n >> m;
for (int i = 1, t; i <= n; ++i) {
cin >> t;
if(check(t, m)) cout << "YES\n";
else cout << "NO\n";
}
}
return 0;
}
C. Nezzar and Symmetric Array
题意: 定义长度为2 * n的对称数组a为,对于每个元素a[i],数组中一定存在一个元素a[j] = -a[i],且数组中所有元素都不相同。定义数组d为: d i = ∑ j = 1 j = n ∣ a i − a j ∣ d_i=∑_{j=1}^{j = n}|a_i−aj| di=∑j=1j=n∣ai−aj∣。现在给定数组d,问是否存在对称数组a。
题解: 思维。这个大佬的题解写得很详细了:https://www.cnblogs.com/zwjzwj/p/14343119.html
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int const mxXN = 2e5 + 10;
int n, m, T;
LL d[mxXN];
unordered_map<LL, int> mp;
unordered_map<LL, int> cnt;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> T;
while(T--) {
cin >> n;
mp.clear(), cnt.clear();
for (int i = 1; i <= 2 * n; ++i) cin >> d[i], mp[d[i]]++;;
sort(d + 1, d + 1 + n * 2);
// d为偶数,d成对出现
int flg = 1;
for (int i = 1; i <= n * 2; ++i) {
if ((d[i] & 1) || mp[d[i]] % 2 != 0) flg = 0;
}
LL mx = d[2 * n],sum = 0;
if(d[2 * n] % (2 * n) != 0) flg = 0;
mx /= (2 * n);
cnt[mx]++;
for(int i = 2 * n - 2; i >= 1; i -= 2){
sum += 2 * mx;
if((d[i] - sum) % i != 0) flg = 0;
mx = (d[i] - sum) / i;
if(cnt[mx] == 1) flg = 0;
cnt[mx]++;
if(mx <= 0) flg = 0;
}
if (flg) cout << "YES\n";
else cout << "NO\n";
}
return 0;
}
D. Nezzar and Board
题意: 给定长度为n的a数组,每次可以选择任意2个数字x和y,然后将2 * x - y加入数组,问是否能够通过若干次操作得到k。
题解: 思维题。2 * x - y意思是可以将x加上和y的差值或者减去和y的差值。那么现在对于任意2个差值,通过手推发现,我可以得到他们的gcd差值,而且这个差值可以转移到任意一个数字身上,因此我只需要判断全部差值的gcd是否整除a[1] - k即可。或者更加严谨的证明见这个大佬:https://blog.csdn.net/weixin_45750972/article/details/113389644
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
typedef long long LL;
int t;
LL a[N];
int main() {
cin >> t;
while (t--) {
int n;
LL k;
cin >> n >> k;
LL g = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
g = a[1] - a[0];
for (int i = 1; i < n; i++) {
g = __gcd(a[i] - a[i - 1], g);
}
if(abs(k-a[1])%g==0)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
E. Nezzar and Binary String
题意: Nezzar希望将其长度为n的二进制字符串s与他的最好的朋友Nanako共享。 Nanako将花q天的时间检查二进制字符串。同时,Nezzar希望在这q天内将字符串s更改为字符串f,因为它看起来更好。
众所周知,菜菜子非常喜欢一致性。在第i天,Nanako将检查从位置li到位置ri的字符串s段。如果该段同时包含字符“ 0”和“ 1”,则Nanako会感到不满意并扔掉字符串。
经过检查后,在第i个晚上,Nezzar可以秘密地更改从li到ri(含)范围内的字符的严格少于一半的字符,否则更改将太明显。
现在,Nezzar想知道,是否有可能避免Nanako感到不快乐,同时在这q个昼夜中使字符串等于f。
题解: 倒序思维+线段树。问从s能否到f,但是因为检查是正着检查的,所有如果正着改就会导致后面的修改覆盖前面的修改,因此我们可以从f倒着改,看是否能够改到s。然后改的时候操作是唯一的,线段树维护即可。
代码:
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int maxn = 2e5 + 10;
const int inf = 0x3f3f3f3f;
struct node {
int l, r;
int sum;
int flag;
} t[maxn << 2];
int orig[maxn];
int a[maxn];
pair<int, int> b[maxn];
void pushup(int root) { t[root].sum = t[root << 1].sum + t[root << 1 | 1].sum; }
void pushdown(int root) {
if (t[root].flag != -1) {
t[root << 1].flag = t[root << 1 | 1].flag = t[root].flag;
t[root << 1].sum =
(t[root << 1].r - t[root << 1].l + 1) * t[root << 1].flag;
t[root << 1 | 1].sum =
(t[root << 1 | 1].r - t[root << 1 | 1].l + 1) * t[root << 1 | 1].flag;
t[root].flag = -1;
}
}
void build(int root, int l, int r) {
t[root] = {l, r, 0, -1};
if (l == r) {
t[root].sum = a[l];
return;
}
int mid = l + r >> 1;
build(root << 1, l, mid), build(root << 1 | 1, mid + 1, r);
pushup(root);
}
void update(int root, int l, int r, int val) {
if (l <= t[root].l && t[root].r <= r) {
t[root].flag = val;
t[root].sum = (t[root].r - t[root].l + 1) * t[root].flag;
return;
}
pushdown(root);
int mid = t[root].r + t[root].l >> 1;
if (l <= mid) update(root << 1, l, r, val);
if (r > mid) update(root << 1 | 1, l, r, val);
pushup(root);
}
int query(int root, int l, int r) {
if (l <= t[root].l && t[root].r <= r) return t[root].sum;
pushdown(root);
int mid = t[root].r + t[root].l >> 1;
int res = 0;
if (l <= mid) res += query(root << 1, l, r);
if (r > mid) res += query(root << 1 | 1, l, r);
return res;
}
int main() {
int tt;
cin >> tt;
while (tt--) {
int n, q;
cin >> n >> q;
for (int i = 1; i <= n; i++) scanf("%1d", init + i);
for (int i = 1; i <= n; i++) scanf("%1d", a + i);
for (int i = 1; i <= q; i++) cin >> b[i].first >> b[i].second;
build(1, 1, n);
int flag = 0;
for (int i = q; i >= 1; i--) {
int l = b[i].first, r = b[i].second;
int num = r - l >> 1;
int one = query(1, l, r);
int zero = r - l + 1 - one;
if (zero <= num)
update(1, l, r, 1);
else if (one <= num)
update(1, l, r, 0);
else {
flag = 1;
break;
}
}
for (int i = 1; i <= n; i++)
if (orig[i] != query(1, i, i)) {
flag = 1;
break;
}
if (flag)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
}
F. Nezzar and Nice Beatmap
题意: 给你n个点,要你把n个点的顺序重新排序,使得相邻点连边形成的所有角度都是锐角。
题解: 数学+思维。锐角三角形的两边的平方和大于第三边,因此一开始我们可以随机选一个点,然后每次都找到距离这个点最远的点,这样就能够使得任意两边的平方和大于第三边。
代码:
#include <bits/stdc++.h>
using namespace std;
#define N 5050
#define int long long
int n;
struct point {
int x, y;
void input() { scanf("%lld %lld", &x, &y); }
int dist(const point &p) const {
return (x - p.x) * (x - p.x) + (y - p.y) * (y - p.y);
}
} p[N];
bool vis[N];
signed main() {
scanf("%lld", &n);
for (int i = 1; i <= n; i++) p[i].input();
p[0].x = p[0].y = -1e9 - 7;
for (int i = 1, c = 0; i <= n; i++) {
int Max = 0;
int k = -1;
for (int j = 1; j <= n; j++) {
if (vis[j]) continue;
int d = p[c].dist(p[j]);
if (d > Max) Max = d, k = j;
}
c = k;
vis[c] = 1;
printf("%lld ", c);
}
puts("");
return 0;
}