codeforces round #803(div.2) A - C
A XOR Mixup
题意:
给定一个数组序列,输出其中一个元素x,使得数组其他元素的异或等于x
思路:
算出数组的总异或x,遍历数组,如果a[i] xor x == a[i]就输出ai
代码:
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
int t, n, a[1010];
int main () {
cin >> t ;
while (t--) {
cin >> n;
cin >> a[1];
int x = a[1];
for (int i = 2; i <= n; i++) cin >> a[i], x = x xor a[i];
for (int i = 1; i <= n; i++) {
if (x xor a[i] == a[i]) {
cout << a[i] << endl;
break;
}
}
}
return 0;
}
B. Rising Sand
题意:
给定n,k,一个n个元素的数组,你可以将一个区间长度为k的区间[l,r]中所有元素+1,这一步骤可以执行任意次(含0次),求满足
的元素个数的最大值
思路:
可以类似差分数组的形式观察一下,发现当k>2时满足条件的元素个数随着操作只减不增,直接输出开始时的满足元素个数即可。
k=1时,首先可以把n个元素加到相同,然后不断进行操作使第2个,第4个... 满足条件,n为奇数时最大为n/2个,偶数时最大为n/2-1个
代码:
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
int t, n, k, a[200010], b[200010] ;
int main () {
cin >> t ;
while (t--) {
int cnt = 0;
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 1; i < n - 1; i++) {
b[i] = a[i] - a[i - 1] - a[i + 1];
if (b[i] > 0) cnt ++;
}
if (k != 1) cout << cnt << endl;
else {
if (n % 2 == 1) cout << n / 2 << endl;
else cout << n / 2 - 1 << endl;
}
}
return 0;
}
C. 3SUM Closure
题意:
给定一个数组,询问是否满足:对于任意三个下标
思路:
如果都是正数,即正数多于两个,那么最大三个数相加一定大于数组中最大的数,不满足题意
所以正数负数都要小于等于两个。
于是可以对于满足条件的数组长度进行限制:
n>=5: 满足条件的是全0,或者多个0,1个正数,负数
n=3,4:直接暴力检验
代码:
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
ll t, n, a[200010];
int main () {
cin >> t ;
while (t--) {
cin >> n ;
int cnt1 = 0, cnt2 = 0, cnt3 = 0, ok = 1;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] > 0) cnt1++;
else if (a[i] < 0) cnt2++;
else cnt3++;
if (a[i] != 0) ok = 0;
}
if (ok) cout << "YES" << endl;
else if (cnt3 && cnt2 >= 2 || cnt3 && cnt1 >= 2) cout << "NO" << endl;
else if (cnt3 && cnt1 == 1 && cnt2 == 1) {
ll sum = 0;
for (int i = 1; i <= n; i++) sum += a[i];
if (sum) cout << "NO" << endl;
else cout << "YES" << endl;
}
else if (cnt3 && cnt1 == 1 || cnt3 && cnt2 == 1) cout << "YES" << endl;
else if (cnt1 >= 3 || cnt2 >= 3 || cnt1 == 0 && cnt2 >= 2 || cnt2 == 0 && cnt1 >= 2) cout << "NO" << endl;
else if (n == 3) {
ll sum = 0;
for (int i = 1; i <= 3; i++) sum += a[i];
if (sum != a[1] && sum != a[2] && sum != a[3]) cout << "NO" << endl;
else cout << "YES" << endl;
}
else {
ll x1 = a[1] + a[2] + a[3], x2 = a [1] + a[2] + a[4], x3 = a[1] + a[3] + a[4], x4 = a[2] + a[3] + a[4];
if ((x1 != a[1] && x1 != a[2] && x1 != a[3] && x1 != a[4]) || (x2 != a[1] && x2 != a[2] && x2 != a[3] && x2 != a[4]) || (x3 != a[1] && x3 != a[2] && x3 != a[3] && x3 != a[4]) || (x4 != a[1] && x4 != a[2] && x4 != a[3]) && x4 != a[4]) cout << "NO" << endl;
else cout << "YES" << endl;
}}
return 0;
}
本文作者:misasteria
本文链接:https://www.cnblogs.com/misasteria/p/16428370.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步