From d100f0e79fbc80a8e9045294bb364d6bc88db9b3 Mon Sep 17 00:00:00 2001 From: Baoshuo Ren Date: Thu, 2 Dec 2021 13:34:52 +0800 Subject: [PATCH] =?UTF-8?q?P1314=20[NOIP2011=20=E6=8F=90=E9=AB=98=E7=BB=84?= =?UTF-8?q?]=20=E8=81=AA=E6=98=8E=E7=9A=84=E8=B4=A8=E7=9B=91=E5=91=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit R64081915 --- Luogu/P1314/P1314.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 Luogu/P1314/P1314.cpp diff --git a/Luogu/P1314/P1314.cpp b/Luogu/P1314/P1314.cpp new file mode 100644 index 00000000..de65e98a --- /dev/null +++ b/Luogu/P1314/P1314.cpp @@ -0,0 +1,56 @@ +#include + +using std::cin; +using std::cout; +using std::endl; + +int n, m, l, r, w[200005], v[200005]; +long long s, y, sum, ans, fn[200005], fv[200005]; +std::pair q[200005]; + +bool check(int x) { + y = sum = 0; + memset(fn, 0x00, sizeof(fn)); + memset(fv, 0x00, sizeof(fv)); + for (int i = 1; i <= n; i++) { + if (w[i] >= x) { + fn[i] = fn[i - 1] + 1; + fv[i] = fv[i - 1] + v[i]; + } else { + fn[i] = fn[i - 1]; + fv[i] = fv[i - 1]; + } + } + for (int i = 1; i <= m; i++) { + y += (fn[q[i].second] - fn[q[i].first - 1]) * (fv[q[i].second] - fv[q[i].first - 1]); + } + sum = std::abs(y - s); + return y > s; +} + +int main() { + ans = std::numeric_limits::max(); + l = std::numeric_limits::max(); + r = std::numeric_limits::min(); + cin >> n >> m >> s; + for (int i = 1; i <= n; i++) { + cin >> w[i] >> v[i]; + l = std::min(l, w[i]); + r = std::max(r, w[i]); + } + for (int i = 1; i <= m; i++) { + cin >> q[i].first >> q[i].second; + } + l -= 1, r += 2; + while (l <= r) { + int mid = l + r >> 1; + if (check(mid)) { + l = mid + 1; + } else { + r = mid - 1; + } + ans = std::min(ans, sum); + } + cout << ans << endl; + return 0; +}