From 590a0fbd21dcf13fd5dd5ff172e056b10961f2e2 Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Mon, 15 Nov 2021 13:08:42 +0800 Subject: [PATCH] =?UTF-8?q?#152.=20=E3=80=902020.12.2=20NOIP=E6=A8=A1?= =?UTF-8?q?=E6=8B=9F=E8=B5=9B=20T1=E3=80=91=E6=9C=80=E5=B0=8F=E5=BE=97?= =?UTF-8?q?=E5=88=86=E5=92=8C(mark)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://sjzezoj.com/submission/43987 --- S2OJ/152/152.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 S2OJ/152/152.cpp diff --git a/S2OJ/152/152.cpp b/S2OJ/152/152.cpp new file mode 100644 index 00000000..5e9925ae --- /dev/null +++ b/S2OJ/152/152.cpp @@ -0,0 +1,50 @@ +#pragma GCC optimize("Ofast") + +#include + +using namespace std; + +long long n, k, a[1000005], sum, cnt, pos, ans, num, res; + +bool check(int x); + +int main() { + cin >> n >> k; + for (int i = 1; i <= n; i++) { + cin >> a[i]; + } + sort(a + 1, a + n + 1); + int l = 1; + int r = a[n] - a[1]; + while (l <= r) { + int mid = l + r >> 1; + if (check(mid)) { + r = mid - 1; + num = cnt; + pos = mid; + ans = res; + } else { + l = mid + 1; + } + } + cout << ans - (num - k) * pos << endl; + return 0; +} + +bool check(int x) { + cnt = 0; + res = 0; + sum = 0; + queue q; + for (int i = 1; i <= n; i++) { + while (!q.empty() && a[i] - a[q.front()] > x) { + sum -= a[q.front()]; + q.pop(); + } + cnt += q.size(); + res += q.size() * a[i] - sum; + q.push(i); + sum += a[i]; + } + return cnt >= k; +}