简单数学题。
因为 ,所以 ,因为数组 全相等,所以数组 合法值集合就是
一个合法值对应一种方案,输出集合大小即可。
AC 代码
// 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;
}