From ef580a25a74d1ce717611209c363130495e9fc4e Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Fri, 2 Sep 2022 07:45:59 +0800 Subject: [PATCH] =?UTF-8?q?P3957=20[NOIP2017=20=E6=99=AE=E5=8F=8A=E7=BB=84?= =?UTF-8?q?]=20=E8=B7=B3=E6=88=BF=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/85667205 --- Luogu/P3957/P3957.cpp | 74 ++++++++++++++++++++++++++++++++++++ Luogu/P3957/data/P3957_1.in | 3 ++ Luogu/P3957/data/P3957_1.out | 3 ++ 3 files changed, 80 insertions(+) create mode 100644 Luogu/P3957/P3957.cpp create mode 100644 Luogu/P3957/data/P3957_1.in create mode 100644 Luogu/P3957/data/P3957_1.out diff --git a/Luogu/P3957/P3957.cpp b/Luogu/P3957/P3957.cpp new file mode 100644 index 00000000..b516803c --- /dev/null +++ b/Luogu/P3957/P3957.cpp @@ -0,0 +1,74 @@ +#include +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 5e5 + 5; +const long long INF = 0x3f3f3f3f3f3f3f3f; + +int n, d; +long long k, f[N], sum; +std::pair a[N]; + +bool check(long long l, long long r) { + std::deque q; + + for (int i = 1, j = 0; i <= n; i++) { + f[i] = -INF; + + while (j < i && l <= a[i].first - a[j].first) { + if (f[j] != -INF) { + while (!q.empty() && f[q.back()] <= f[j]) q.pop_back(); + q.push_back(j); + } + + j++; + } + + while (!q.empty() && a[i].first - a[q.front()].first > r) { + q.pop_front(); + } + + if (!q.empty()) { + f[i] = std::max(f[i], f[q.front()] + a[i].second); + } + + if (f[i] >= k) return true; + } + + return false; +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> d >> k; + + for (int i = 1; i <= n; i++) { + cin >> a[i].first >> a[i].second; + + sum += a[i].second; + } + + long long l = 0, r = INF; + + while (l < r) { + long long mid = l + r >> 1; + + if (check(std::max(d - mid, 0ll), d + mid)) { + r = mid; + } else { + l = mid + 1; + } + } + + cout << (r == INF ? -1 : r) << endl; + + return 0; +} diff --git a/Luogu/P3957/data/P3957_1.in b/Luogu/P3957/data/P3957_1.in new file mode 100644 index 00000000..6439527b --- /dev/null +++ b/Luogu/P3957/data/P3957_1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2cb334050d8d82af75fce691a6d4e2f942cb9343bc0f2f3e35bd3995b9561e4f +size 79 diff --git a/Luogu/P3957/data/P3957_1.out b/Luogu/P3957/data/P3957_1.out new file mode 100644 index 00000000..92f8b043 --- /dev/null +++ b/Luogu/P3957/data/P3957_1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fe028cccfcfd1aadf0cae5cdadc9fdb1e93988c41b242b575cb45a3dd4b0c24c +size 3