2022.5.5 AcWing每日一题

签到 细节处理

答案只可能在 n - 2 个中间节点和 n - 1 个中间节点的中点之间,所以直接枚举这些结果是否条件即可。
但是需要考虑到中点可能是小数,注意 int 和 double 类型的转换。

#include <bits/stdc++.h>
using namespace std;

// 签到
// 不允许在端点处进行折叠
// 至少要保证选取位置的左右两个最近断定能对上
// 所以,答案一定在 n-2 个中间节点和 n-1 个节点中点之间。

const int N = 1e4 + 10;

int n, l;
vector<double> node;
int st[N];
int ans = 0;
vector<double> check_area;

bool check(double x) {
//	cout << "X:" << x << endl;
	for (int i = 0; i < node.size(); i++) {
		if (2 * x < node[i])
			break;
		if (2 * x > node[i] + l)
			continue;
		if (!st[(int)(2 * x - node[i])]) {
			return false;
		}
	}
	return true;
}

int main() {
	scanf("%d %d", &n, &l);

	for (int i = 1; i <= n; i++) {
		double tmp;
		scanf("%lf", &tmp);
		node.push_back(tmp);
		st[(int)tmp] = 1;
	}

	sort(node.begin(), node.end());

	for (int i = 0; i < node.size(); i++) {
		if (i != 0 && i != node.size() - 1) {
			check_area.push_back(node[i]);
		}
		if (i != 0) {
			check_area.push_back((node[i] + node[i - 1]) / 2.0);
		}
	}

//	for (int i = 0; i < check_area.size(); i++) {
//		cout << check_area[i] << endl;
//	}

	for (int i = 0; i < check_area.size(); i++) {
		if (check(check_area[i])) {
			ans++;
		}
	}

	printf("%d\n", ans);

	return 0;
}
posted @ 2022-05-05 14:19  superPG  阅读(21)  评论(0编辑  收藏  举报