From 59ca4fbd302dadde1309bf2eff102d914051111c Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Tue, 6 Dec 2022 17:02:21 +0800 Subject: [PATCH] P1956 Sum https://www.luogu.com.cn/record/96691668 --- Luogu/P1956/P1956.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Luogu/P1956/P1956.cpp diff --git a/Luogu/P1956/P1956.cpp b/Luogu/P1956/P1956.cpp new file mode 100644 index 00000000..c091644d --- /dev/null +++ b/Luogu/P1956/P1956.cpp @@ -0,0 +1,43 @@ +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e5 + 5; + +int n; +long long k, p, s[N], + ans = std::numeric_limits::max(); +std::set set{{0}}; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> k >> p; + + for (int i = 1; i <= n; i++) { + long long x; + + cin >> x; + + s[i] = (s[i - 1] + x) % p; + } + + for (int i = 1; i <= n; i++) { + if (s[i] < k) { + ans = std::min(ans, s[i] + p - *--set.upper_bound(s[i] - k + p)); + } else { + ans = std::min(ans, s[i] - *--set.upper_bound(s[i] - k)); + } + + set.emplace(s[i]); + } + + cout << ans << endl; + + return 0; +}