0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-09-16 20:25:24 +00:00

P1956 Sum

https://www.luogu.com.cn/record/96691668
This commit is contained in:
Baoshuo Ren 2022-12-06 17:02:21 +08:00
parent 7b40b909b2
commit 59ca4fbd30
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

43
Luogu/P1956/P1956.cpp Normal file
View File

@ -0,0 +1,43 @@
#include <iostream>
#include <limits>
#include <set>
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<long long>::max();
std::set<long long> 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;
}