Public NOIP Round1,2,3部分题解

1. Public NOIP Round #1 (Div. 1, 提高 2022-09-10 14:00:00)

A. 斜二等轴测图
就是个大模拟:将整个图形分为三个部分即可,简单计算亿下就可以了

点击查看代码丑爆了
#include <stdio.h>
#include <string.h>
char map[1000][1000];

int main() {
	int T, a, b, c;
	scanf("%d", &T);
	while(T --) {
		scanf("%d%d%d", &a, &b, &c);
		for(int i = 0; i < ((b + c) << 1 | 1); i ++)
			for(int j = 0; j < ((a + b) << 1 | 1); j ++) map[i][j] = '.';
		for(int i = 0; i < (b << 1); i ++) {
			if(i & 1) {
				for(int j = 0; j < (a << 1 | 1); j += 2) map[i][(b << 1) - i + j] = '/';
				for(int j = 1; j < (a << 1 | 1); j += 2) map[i][(b << 1) - i + j] = '.';
				for(int j = 0; j < (c << 1); j ++)
					map[i + 1 + j][((a + b) << 1) - i] = j & 1 ? '/' : '.';
			} else {
				for(int j = 0; j < (a << 1 | 1); j += 2) map[i][(b << 1) - i + j] = '+';
				for(int j = 1; j < (a << 1 | 1); j += 2) map[i][(b << 1) - i + j] = '-';
				for(int j = 0; j < (c << 1); j ++)
					map[i + 1 + j][((a + b) << 1) - i] = j & 1 ? '+' : '|';
			}
		}
		for(int i = 0; i < (c << 1); i ++)
			if(i & 1) {
				for(int j = 0; j < (a << 1 | 1); j += 2) map[i + (b << 1)][j] = '|';
				for(int j = 1; j < (a << 1 | 1); j += 2) map[i + (b << 1)][j] = '.';
			} else {
				for(int j = 0; j < (a << 1 | 1); j += 2) map[i + (b << 1)][j] = '+';
				for(int j = 1; j < (a << 1 | 1); j += 2) map[i + (b << 1)][j] = '-';
			}
		for(int j = 0; j < (a << 1 | 1); j += 2) map[(b + c) << 1][j] = '+';
		for(int j = 1; j < (a << 1 | 1); j += 2) map[(b + c) << 1][j] = '-';
		for(int i = 0; i < ((b + c) << 1 | 1); i ++, putchar(10))
			for(int j = 0; j < ((a + b) << 1 | 1); j ++) putchar(map[i][j]);
	}
	return 0;
}

B 冲塔

先选所有横坐标相同的最左边或最右边
从(纵坐标)外往内枚举,如果个数>2则将纵坐标在中间的往横坐标移动,使用set维护即可

点击查看代码

#include <set>
#include <vector>
#include <stdio.h>
#include <string.h>
#include <algorithm>
const int N = 1e6 + 5;
typedef std::pair<int, int> PII;
int n;
struct P { int x, y, id; } a[N];
std::set<PII> set[N];
bool used[N];
std::vector<PII> rem;
int main() {
	scanf("%d", &n);
	for(int i = 0; i < n; i ++) scanf("%d%d", &a[i].x, &a[i].y), a[i].id = i;
	std::sort(a, a + n, [](const P &x, const P &y) { return x.x < y.x || (x.x == y.x && x.y < y.y); });
	for(int i = 0; i < n; i ++)
		if((!i || a[i].x != a[i - 1].x) || (a[i].x != a[i + 1].x))
			set[a[i].y].insert({a[i].x, i});
	for(int t = 0; t < 4; t ++) {
		for(int i = 1; i <= 1000000; i ++)
			if(set[i].size() > 2U) {
				for(auto it = ++ set[i].begin(), ed = -- set[i].end(); it != ed; it ++) {
					int id = it->second;
					if(a[id + 1].x == a[id].x)
						set[a[id + 1].y].insert({a[id + 1].x, id + 1}), rem.push_back(*it);
				}
				while(rem.size()) set[i].erase(rem.back()), rem.pop_back();
			}
		for(int i = 1000000; i; i --)
			if(set[i].size() > 2U) {
				for(auto it = ++ set[i].begin(), ed = -- set[i].end(); it != ed; it ++) {
					int id = it->second;
					if(id && a[id - 1].x == a[id].x)
						set[a[id - 1].y].insert({a[id - 1].x, id - 1}), rem.push_back(*it);
				}
				while(rem.size()) set[i].erase(rem.back()), rem.pop_back();
			}
	}
	for(int i = 1; i <= 1000000; i ++)
		if(set[i].size()) used[a[set[i].begin()->second].id] = used[a[set[i].rbegin()->second].id] = true;
	for(int i = 0; i < n; i ++) printf("%d", int(used[i]));
	putchar(10);
	return 0;
}
2. Public NOIP Round #2 (Div. 1, 提高 2022-10-04 08:30:00)

A 恰钱
先打个表(递推或者dfs等枚举),然后使用lower_bound找到大于等于l的最小满足条件的数,判断这个数与r的大小关系

点击查看代码
#include <stdio.h>
#include <string.h>
#include <algorithm>
const int N = 1e7 + 5;
int num[N], top[N], tot; // top为最高位的1的位置
int T, l, r, res;
int main() {
	num[++ tot] = 2, top[tot] = 1;
	for(int i = 2; i <= 30; i ++) for(int j = 1; j <= tot; j ++) // 扩展
		if(i > top[j] && !(num[j] << 1 >> i & 1) && (num[j] << 1 | (1 << i)) <= 1000000000)
			num[++ tot] = num[j] << 1 | (1 << i), top[tot] = i;
	scanf("%d", &T);
	while(T --) {
		scanf("%d%d", &l, &r);
		res = *std::lower_bound(num + 1, num + tot + 1, l);
		printf("%d\n", !res || res > r ? -1 : res);
	}
	return 0;
}

B 排序
使用DP: 设 f[i] 表示处理好 [i,n] 且 a[i] 必选的子序列长度的最大值
使用单调栈求出比这个数小的第一个数的下标 nxt[i]
f[i]<-f[j]+1 其中 j∈[nxt[i],nxt[nxt[i]])
答案为 max{f[j]} 其中 j∈[1,nxt[1]]
方便枚举: 将这些按照 a 排序

点击查看代码
#include <stdio.h>
#include <string.h>
#include <algorithm>
const int N = 5e5 + 5;
int n, ord[N], nxt[N], a[N], tr[N << 2];
inline int ls(int u) { return u << 1; }
inline int rs(int u) { return u << 1 | 1; }
void modify(int u, int l, int r, int x, int y) {
	if(l == r) tr[u] = std::max(tr[u], y);
	else {
		int mid = (l + r) >> 1;
		if(x <= mid) modify(ls(u), l, mid, x, y);
		else modify(rs(u), mid + 1, r, x, y);
		tr[u] = std::max(tr[ls(u)], tr[rs(u)]);
	}
}
int query(int u, int l, int r, int x, int y) {
	if(x <= l && r <= y) return tr[u];
	int mid = (l + r) >> 1, res = 0;
	if(x <= mid) res = std::max(res, query(ls(u), l, mid, x, y));
	if(y > mid) res = std::max(res, query(rs(u), mid + 1, r, x, y));
	return res;
}
int stk[N], top, res;
int main() {
	scanf("%d", &n);
	for(int i = 1; i <= n; i ++) scanf("%d", a + i), ord[a[i]] = i;
	stk[top = 0] = n + 1, nxt[n + 1] = n;
	for(int i = n; i; i --) {
		while(top && a[i] > a[stk[top]]) top --;
		nxt[i] = stk[top], stk[++ top] = i;
	}
	for(int i = n; i; i --)
		modify(1, 1, n, ord[i], query(1, 1, n, nxt[ord[i]], nxt[nxt[ord[i]]] - 1) + 1);
	printf("%d\n", query(1, 1, n, 1, nxt[1]));
	return 0;
}
1. Public NOIP Round #3 (Div. 1, 提高 2022-10-22 08:30:00) 题解

| A | 移除石子 |90/

点击查看代码
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define x first
#define y second
const int N = 3005;
int n;
std::pair<int, int> a[N];
int main() {
	int T;
	scanf("%d", &T);
	while(T --) {
		scanf("%d", &n);
		for(int i = 1; i <= n; i ++) scanf("%d%d", &a[i].first, &a[i].second);
		std::sort(a + 1, a + n + 1), puts("Yes");
		while(n) {
			auto X = a[n], Y = a[n - 1];
			if(X.x == Y.x) printf("%d %d %d %d\n", Y.x, Y.y, Y.x + X.y - Y.y, X.y);
			else if(Y.y <= X.y) {
				int t = std::max(X.x - Y.x, X.y - Y.y);
				printf("%d %d %d %d\n", Y.x, Y.y, Y.x + t, Y.y + t);
			} else {
				auto p = [&] () {
					int t = std::max(X.x - Y.x, Y.y - X.y);
					printf("%d %d %d %d\n", Y.x, X.y, Y.x + t, X.y + t);
				};
				if(n > 2) {
					auto Z = a[n - 2];
					if(Z.x == Y.x) {
						if(Z.y > X.y) printf("%d %d %d %d\n", Z.x, Z.y, Z.x + Y.y - Z.y, Y.y), a[n - 2] = X;
						else if(Z.y == X.y) {
							int dx = X.x - Z.x, dy = Y.y - Z.y;
							if(dx >= dy) printf("%d.5 %d %d.5 %d\n", Z.x - 1, Z.y, Z.x + dy - 1, Y.y), a[n - 2] = X;
							else if(dx < dy) printf("%d %d %d %d\n", Z.x, Z.y, X.x, Z.y + dx), a[n - 2] = Y;
						} else p();
					} else p();
				} else p();
			}
			n -= 2;
		}
	}
	return 0;
}

| B | 抓内鬼 |/20

| C | 异或序列 |/30

posted @ 2022-10-14 15:49  azzc  阅读(112)  评论(0编辑  收藏  举报