做题地址:https://oj.niumacode.com/training/42
20250313_1_数组删除
签到题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| #include<bits/stdc++.h>
using namespace std;
const int N = 100010; int a[N];
int main() { int T; cin >> T; while(T -- ) { int n, l , r; cin >> n >> l >> r; unordered_map<int, int> h; for(int i = 0; i < n; i ++ ) { cin >> a[i]; h[a[i]] ++; } vector<int> ans; for(int i = 0; i < n; i ++ ) { if(h[a[i]] >= l && h[a[i]] <= r) continue; ans.push_back(a[i]); } cout << ans.size() << endl; for(auto v : ans) { printf("%d ", v); } cout << endl; } return 0; }
|
20250313_2_最大的和
贪心,异或的性质
虽然题目说的是每次异或相邻两个数,但是由于异或的性质,对一个数异或偶数次还是这个数本身,因此可以每次异或任意两个数。
记录每个数异或后的变化值,优先异或两个对数组之和增加大的,因此要从大到小排序。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| #include<bits/stdc++.h>
using namespace std;
const int N = 100010; long long a[N];
int main() { int T; cin >> T; while(T -- ) { int n, x; cin >> n >> x; long long sum = 0; for(int i = 0; i < n; i ++ ) { cin >> a[i]; sum += a[i]; } vector<long long> gain; for(int i = 0; i < n; i ++ ) { gain.push_back((a[i] ^ x) - a[i]); } sort(gain.rbegin(), gain.rend()); for(int i = 0; i < gain.size() - 1; i += 2) { if(gain[i] + gain[i + 1] > 0) { sum += gain[i] + gain[i + 1]; }else { break; } } cout << sum << endl; } return 0; }
|