比赛 |
20161114 |
评测结果 |
WAAWWAWWWW |
题目名称 |
社长的qwa |
最终得分 |
30 |
用户昵称 |
jinqiu |
运行时间 |
1.071 s |
代码语言 |
C++ |
内存使用 |
2.60 MiB |
提交时间 |
2016-11-14 10:32:36 |
显示代码纯文本
#include<iostream>
#include<cstdio>
#include<algorithm>
#define LL long long
using namespace std;
const int maxn = 1e5 + 10;
int n, k;
int a[maxn];
int mul[maxn];
LL line[maxn];
LL pre[maxn];
LL ans = 1e18;
int read();
int main() {
freopen("qwa.in", "r", stdin);
freopen("qwa.out", "w", stdout);
int i, j;
n = read(); k = read();
for(i = 1; i <= k - 1 - 2; i++)
mul[i] = (k - 2 - i)*i;
for(i = 1; i <= n; i++)
a[i] = read();
sort(a + 1, a + n + 1);
for(i = 2; i <= n; i++) {
line[i - 1] = a[i] - a[i - 1];
pre[i - 1] = pre[i - 2] + line[i - 1];
}
if(k == 2) {
for(i = 1; i <= n - 1; i++)
ans = min(ans, line[i]);
cout << ans << "\n";
return 0;
}
if(k == 3) {
for(i = 2; i <= n - 1; i++)
ans = min(line[i] + line[i - 1], ans);
cout << (ans << 1) << "\n";
return 0;
}
for(i = 1; i + k - 1 <= n; i++) {
LL tmp = (pre[i + k - 2] - pre[i - 1])*(k - 1);
int cnt = 1;
for(j = i + 1; j <= i + k - 3; j++)
tmp += line[i]*mul[cnt++];
ans = min(tmp, ans);
}
cout << ans << "\n";
return 0;
}
int read() {
char s = getchar();
int f = 1, t = 0;
while(s < '0' || s > '9') {
if(s == '-')
f = -1;
s = getchar();
}
while(s >= '0' && s <= '9') {
t = (t << 3) + (t << 1) + s - '0';
s = getchar();
}
return t*f;
}