题目链接
C. Sweets Eating
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Tsumugi brought n delicious sweets to the Light Music Club. They are numbered from 1 to n, where the i-th sweet has a sugar concentration described by an integer ai.
Yui loves sweets, but she can eat at most m sweets each day for health reasons.
Days are 1-indexed (numbered 1,2,3,…). Eating the sweet i at the d-th day will cause a sugar penalty of (d⋅ai), as sweets become more sugary with time. A sweet can be eaten at most once.
The total sugar penalty will be the sum of the individual penalties of each sweet eaten.
Suppose that Yui chooses exactly k sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get?
Since Yui is an undecided girl, she wants you to answer this question for every value of k between 1 and n.
Input
The first line contains two integers n and m (1≤m≤n≤200 000).
The second line contains n integers a1,a2,…,an (1≤ai≤200 000).
Output
You have to output n integers x1,x2,…,xn on a single line, separed by spaces, where xk is the minimum total sugar penalty Yui can get if she eats exactly k sweets.
Examples
inputCopy
9 2
6 19 3 4 4 2 6 7 8
outputCopy
2 5 11 18 30 43 62 83 121
inputCopy
1 1
7
outputCopy
7
Note
Let’s analyze the answer for k=5 in the first example. Here is one of the possible ways to eat 5 sweets that minimize total sugar penalty:
Day 1: sweets 1 and 4
Day 2: sweets 5 and 3
Day 3 : sweet 6
Total penalty is 1⋅a1+1⋅a4+2⋅a5+2⋅a3+3⋅a6=6+4+8+6+6=30. We can prove that it’s the minimum total sugar penalty Yui can achieve if she eats 5 sweets, hence x5=30.
毫无疑问贪心大法
先排个序,k个中,大的放在前面吃
超时代码
前缀和 ;
#include<iostream>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<map>
#include<set>
#include<queue>
using namespace std;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
const int inf=0x3f3f3f3f;
const ll mod=1e9+7;
const int maxn=2e5+5;
ll a[maxn],b[maxn],sum[maxn];
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int n,m;
while(cin>>n>>m){
mem(sum);mem(b);
for(int i=1;i<=n;i++) cin>>a[i];
sort(a+1,a+n+1);
for(int i=1;i<=n;i++) sum[i]=sum[i-1]+a[i];
for(int i=1;i<=n;i++){
if(i<=m) b[i]=sum[i];
else{
int t=i/m,k=i%m;
int day=t+(k==0?0:1);
for(int j=1;j<day;j++){
b[i]+=j*(sum[i-(j-1)*m]-sum[i-j*m]);
}
if(k!=0) b[i]+=day*sum[k];
else b[i]+=day*sum[i-(day-1)*m];
}
}
for(int i=1;i<n;i++){
cout<<b[i]<<' ';
}
cout<<b[n]<<endl;
}
}
AC代码
#include<iostream>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<map>
#include<set>
#include<queue>
using namespace std;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
const int inf=0x3f3f3f3f;
const ll mod=1e9+7;
const int maxn=2e5+5;
ll a[maxn],sum[maxn];
int n,m;
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
}
sort(a+1,a+n+1);
for(int i=1;i<=n;i++){
if(i<m){
sum[i]=a[i];
}
else{
sum[i]=a[i]+sum[i-m];
}
}
ll ans=0;
for(int i=1;i<n;i++){
ans+=sum[i];
cout<<ans<<' ';
}
ans+=sum[n];
cout<<ans<<endl;
}
小白表示第一次遇到这种解法感觉好妙…
其实就是相当于把t(要吃的糖个数)分成以m为单位的若干组.
把每组都叠起来(可以想象成二维数组,列宽就是m,高无限制)
小的在下,大的在上(要先对数组排序)
sum[i]到i为止表示i%m列的和,比如说m=4,那么sum[1]=a[1],
sum[5]=a[1]+a[5];sum[9]=a[1]+a[5]+a[9];
sum[1]+sum[5]+sum[9]=3a[1]+2a[5]+a[9];
(假设k是9,应该能自行脑补出来了吧?)
a9
a5 a6 a7 a8
a1 a2 a3 a4
大概就是这个样子s[i]就是 a[i]一直往下加,加到最低层;
如果把sum[i]累加起来的话,不难发现最底层的(也就是最小的)被加了最多次,就起到了乘法的作用,最上面的最大的,乘的次数最少.