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

· · 题解

Link

简单数学题。

因为 b_i\in [-k,k],所以 c_i\in[a_i-k, a_i+k],因为数组 c 全相等,所以数组 c 合法值集合就是

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

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

::::success[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;
}

::::