题意:给定一个长度为 $n$ 的序列,每次操作可以选择一个位置 $i$ ,执行 $a_i += 1$,当所有数的和与积均不为0时,停止操作,求所需要操作的最少次数。
思路:所有0的数字+1,之后如果和为0再+1.
#include <bits/stdc++.h> #define pb(x) push_back(x) #define mp(x, y) make_pair(x, y) #define fast ios::sync_with_stdio(false) #define mset(a, n) memset(a, n, sizeof(a)) #define forn(i, n) for (int i = 0; i < (n); ++i) #define forab(i, a, b) for (int i = (a); i <= (b); ++i) #define forba(i, b, a) for (int i = (b); i >= (a); --i) #define db double #define ll long long #define endl ' ' #define fi first #define se second using namespace std; typedef pair<int, int> P; inline int lowbit(int x) { return x & (-x); } int main(){ fast; int t; cin >> t; while (t--) { int n; cin >> n; int zero = 0, sum = 0; int ans = 0; forn(i, n){ int x; cin >> x; if (x == 0) ++zero; sum += x; } ans += zero; sum += zero; if (sum == 0) ++ans; cout << ans << " "; } return 0; }
题意:将 $2n$ 个学生分为两奇数组,使得中位数之差最小。
思路:排序后,$a_{n} - a_{n - 1}$ 就是答案。
#include <bits/stdc++.h> #define pb(x) push_back(x) #define mp(x, y) make_pair(x, y) #define fast ios::sync_with_stdio(false) #define mset(a, n) memset(a, n, sizeof(a)) #define forn(i, n) for (int i = 0; i < (n); ++i) #define forab(i, a, b) for (int i = (a); i <= (b); ++i) #define forba(i, b, a) for (int i = (b); i >= (a); --i) #define db double #define ll long long #define endl ' ' #define fi first #define se second using namespace std; typedef pair<int, int> P; inline int lowbit(int x) { return x & (-x); } const int N = 1e5 + 5; int a[N << 1]; int main(){ fast; int t; cin >> t; while (t--) { int n; cin >> n; forn(i, 2 * n){ cin >> a[i]; } sort(a, a + 2 * n); cout << a[n] - a[n - 1] << " "; } return 0; }
题意:规定函数 $f(x, y) = (x | y) - y$ ,同时给定长度为 $n$ 的数组,求
的最大值。
思路:观察可得每次运算的结果为:将$x$ 中所有$y$ 中为1的位数变为0(好像有点绕。。自己手写一下就能发现规律了),预处理出来或的前缀和与后缀和,之后遍历一遍求 max(a_{i} & ~(pre[i - 1] | suf[i + 1]))即可,找出第一个数之后,后面的数顺序无所谓。
#include <bits/stdc++.h> #define pb(x) push_back(x) #define mp(x, y) make_pair(x, y) #define fast ios::sync_with_stdio(false) #define mset(a, n) memset(a, n, sizeof(a)) #define forn(i, n) for (int i = 0; i < (n); ++i) #define forab(i, a, b) for (int i = (a); i <= (b); ++i) #define forba(i, b, a) for (int i = (b); i >= (a); --i) #define db double #define ll long long #define endl ' ' #define fi first #define se second using namespace std; typedef pair<int, int> P; inline int lowbit(int x) { return x & (-x); } const int N = 1e5 + 5; int a[N], pre[N], suf[N]; int main(){ fast; int n; cin >> n; forab(i, 1, n){ cin >> a[i]; } forab(i, 1, n){ pre[i] = pre[i - 1] | a[i]; } forba(i, n, 1){ suf[i] = suf[i + 1] | a[i]; } int ans = 0, pos = 1; forab(i, 1, n){ if ((a[i] & ~(pre[i - 1] | suf[i + 1])) > ans){ ans = a[i] & ~(pre[i - 1] | suf[i + 1]); pos = i; } } cout << a[pos] << " "; forab(i, 1, n){ if (i != pos){ cout << a[i] << " "; } } cout << " "; return 0; }
题意:给定一个凸多边形,平移该多边形使得顶点与零点重合,问得到的多边形和原多边形是否相似。
思路:显然,奇数顶点的多边形不成立,针对偶数顶点的多边形,只有中心对称的图形成立。
#include <bits/stdc++.h> #define pb(x) push_back(x) #define mp(x, y) make_pair(x, y) #define fast ios::sync_with_stdio(false) #define mset(a, n) memset(a, n, sizeof(a)) #define forn(i, n) for (int i = 0; i < (n); ++i) #define forab(i, a, b) for (int i = (a); i <= (b); ++i) #define forba(i, b, a) for (int i = (b); i >= (a); --i) #define db double #define ll long long #define endl ' ' #define fi first #define se second using namespace std; //typedef pair<int, int> P; inline int lowbit(int x) { return x & (-x); } const int N = 1e5 + 5; struct P{ db x, y; P(){} P(db _x, db _y){ x = _x; y = _y; } } p[N]; int main(){ fast; int n; cin >> n; forab(i, 1, n){ db x, y; cin >> x >> y; p[i] = (P){x, y}; } if (n & 1){ cout << "nO "; } else { db posx = (p[1].x + p[1 + n / 2].x) / 2; db posy = (p[1].y + p[1 + n / 2].y) / 2; bool flag = true; forab(i, 1, n / 2){ if ((p[i].x + p[i + n / 2].x) / 2 != posx || (p[i].y + p[i + n / 2].y) / 2 != posy){ flag = false; break; } } if (flag){ cout << "YES "; } else { cout << "nO "; } } return 0; }
。。。上英语课写题被gank了。。。E题下课了再做吧。。。。