跳到正文

题解

题解:P17711 Red Face 选数字(choose)

Qwarth约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年10月16日
收录
2026年10月16日
更新
2026年11月14日
篇幅
约 2 分钟
题目
P17711

版本

  1. v1当前

Link

简单数学题。

因为 bi∈[−k,k]b_i\in [-k,k],所以 ci∈[ai−k,ai+k]c_i\in[a_i-k, a_i+k],因为数组 cc 全相等,所以数组 cc 合法值集合就是

⋂i=1n[ai−k,ai+k]\bigcap_{i=1}^{n} [a_i-k,a_i+k]

一个合法值对应一种方案,输出集合大小即可。

AC 代码
C++
// Problem: P17711 Red Face 选数字(choose)
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P17711
// Memory Limit: 512 MB
// Time Limit: 1000 ms

#include <iostream>
#include <algorithm>

namespace Qwarth {
void main() {
	int n, k, min = -2e9, max = 2e9;
	std::cin >> n >> k;
	for (int i = 1, x; i <= n; ++i) {
		std::cin >> x;
		min = std::max(min, x - k);
		max = std::min(max, x + k);
	}
	std::cout << std::max(0, max - min + 1) << '\n';
}
}  // namespace Qwarth

int main() {
	// freopen(".in", "r", stdin);
	// freopen(".out", "w", stdout);
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	int _T = 1;
	// std::cin >> _T;
	while (_T--) {
		Qwarth::main();
	}
	return 0;
}
原文
抓取于
  1. Angels_of_Delusion

    ?!%%%%%%/bx /bx /bx /bx \Qwarth/\Qwarth/\Qwarth/\Qwarth/\Qwarth/\Qwarth/