mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-12-24 03:11:58 +00:00
#152. 【2020.12.2 NOIP模拟赛 T1】最小得分和(mark)
https://sjzezoj.com/submission/43987
This commit is contained in:
parent
9c31ba69fc
commit
590a0fbd21
50
S2OJ/152/152.cpp
Normal file
50
S2OJ/152/152.cpp
Normal file
@ -0,0 +1,50 @@
|
||||
#pragma GCC optimize("Ofast")
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
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<int> 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user