A
模拟
#include <bits/stdc++.h>
using namespace std;
int a[100];
int main() {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
int mask = 0;
for (int i = 0; i < m; i++) {
int x;
scanf("%d", &x);
mask |= 1 << x;
}
for (int i = 0; i < n; i++) {
if ((mask >> a[i]) & 1) {
printf("%d ", a[i]);
}
}
printf("\n");
return 0;
}
B
开个堆搞一搞。
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, k;
int s[N], v[N], o[N];
long long res[N];
int main() {
scanf("%d %d", &n, &k);
for (int i = 0; i < n; i++) {
scanf("%d", &s[i]);
}
for (int i = 0; i < n; i++) {
scanf("%d", &v[i]);
}
if (k == 0) {
for (int i = 0; i < n; i++) {
printf("%d%c", v[i], " \n"[i == n - 1]);
}
return 0;
}
iota(o, o + n, 0);
sort(o, o + n, [](int x, int y) {
return s[x] < s[y];
});
multiset<int> heap;
long long sum = 0;
for (int i = 0; i < n; i++) {
int ii = o[i];
res[ii] = sum + v[ii];
if ((int) heap.size() < k) {
sum += v[ii];
heap.emplace(v[ii]);
} else if (v[ii] > *(heap.begin())) {
sum += v[ii] - *(heap.begin());
heap.erase(heap.begin());
heap.emplace(v[ii]);
}
}
for (int i = 0; i < n; i++) {
printf("%lld%c", res[i], " \n"[i == n - 1]);
}
return 0;
}
C
枚举一下平面上的点,看看有没有公共的。
#include <bits/stdc++.h>
using namespace std;
const int inf = 2e9;
struct square {
int xmin;
int xmax;
int ymin;
int ymax;
square() {
xmin = inf;
xmax = -inf;
ymin = inf;
ymax = -inf;
for (int i = 0; i < 4; i++) {
int x, y;
scanf("%d %d", &x, &y);
xmin = min(xmin, x);
xmax = max(xmax, x);
ymin = min(ymin, y);
ymax = max(ymax, y);
}
}
bool inside(int x, int y) {
return xmin <= x && x <= xmax && ymin <= y && y <= ymax;
}
bool inside2(int x, int y) {
if (x < xmin || xmax < x) {
return false;
}
int diff = abs(x - (xmin + xmax) / 2);
return ymin + diff <= y && y <= ymax - diff;
}
};
int main() {
square a = square();
square b = square();
for (int x = -100; x <= 100; x++) {
for (int y = -100; y <= 100; y++) {
if (a.inside(x, y) && b.inside2(x, y)) {
puts("yes");
return 0;
}
}
}
puts("no");
return 0;
}
D
沙雕文字游戏题。
#include <bits/stdc++.h>
using namespace std;
const int N = 20;
int n, m;
pair<int, int> a[N], b[N];
int match(pair<int, int>& a, pair<int, int>& b) {
if (a == b) {
return -1;
}
if (a.first == b.first) {
return a.first;
}
if (a.first == b.second) {
return a.first;
}
if (a.second == b.first) {
return a.second;
}
if (a.second == b.second) {
return a.second;
}
return -1;
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%d %d", &a[i].first, &a[i].second);
if (a[i].first > a[i].second) {
swap(a[i].first, a[i].second);
}
}
for (int i = 0; i < m; i++) {
scanf("%d %d", &b[i].first, &b[i].second);
if (b[i].first > b[i].second) {
swap(b[i].first, b[i].second);
}
}
vector< set<int> > sa(n), sb(m);
set<int> v;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int res = match(a[i], b[j]);
if (res == -1) {
continue;
}
v.emplace(res);
sa[i].emplace(res);
sb[j].emplace(res);
}
}
if ((int) v.size() == 1) {
printf("%d\n", *(v.begin()));
return 0;
}
for (int i = 0; i < n; i++) {
if ((int) sa[i].size() > 1) {
puts("-1");
return 0;
}
}
for (int i = 0; i < m; i++) {
if ((int) sb[i].size() > 1) {
puts("-1");
return 0;
}
}
puts("0");
return 0;
}
E
钦定某些船对射,统计下每个 \(y\) 的贡献然后拼一拼。
#include <bits/stdc++.h>
using namespace std;
map< int, pair<long long, long long> > mp;
int a[100], b[100];
int main() {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (int i = 0; i < m; i++) {
scanf("%d", &b[i]);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
mp[a[i] + b[j]].first |= 1ll << i;
mp[a[i] + b[j]].second |= 1ll << j;
}
}
int ans = 0;
for (auto x : mp) {
for (auto y : mp) {
ans = max(ans, __builtin_popcountll(x.second.first | y.second.first) + __builtin_popcountll(x.second.second | y.second.second));
}
}
printf("%d\n", ans);
return 0;
}
常识和思考
- E 在处理小数时要非常小心,\(-1 / 2 = 1 / 2\) 卡了半天。。。
原文地址:https://www.cnblogs.com/arg-53/p/9196450.html
时间: 2024-11-14 12:52:44