Sweets Eating

Tsumugi brought nn delicious sweets to the Light Music Club. They are numbered from 11 to nn, where the ii-th sweet has a sugar concentration described by an integer aiai.
Yui loves sweets, but she can eat at most mm sweets each day for health reasons.
Days are 11-indexed (numbered 1,2,3,…1,2,3,…). Eating the sweet ii at the dd-th day will cause a sugar penalty of (d⋅ai)(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 kk 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 kk between 11 and nn.

Input
The first line contains two integers nn and mm (1≤m≤n≤200 0001≤m≤n≤200 000).
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤200 0001≤ai≤200 000).
Output
You have to output nn integers x1,x2,…,xnx1,x2,…,xn on a single line, separed by spaces, where xkxk is the minimum total sugar penalty Yui can get if she eats exactly kk sweets.
Examples:
Input

9 2
6 19 3 4 4 2 6 7 8

Output

2 5 11 18 30 43 62 83 121

Input

1 1
7

Output

7

Note
Let’s analyze the answer for k=5k=5 in the first example. Here is one of the possible ways to eat 55 sweets that minimize total sugar penalty:

Day 11: sweets 11 and 44
Day 22: sweets 55 and 33
Day 33 : sweet 66
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 55 sweets, hence x5=30x5=30.

题意:
有n颗糖果,每天吃m颗,吃第i颗糖的甜度值是a[i]且第d天吃的第i颗糖的甜度值是a[i]*d,求吃k(1<=k<=n)颗糖的最小甜度。
题目分析:
这道题是一道典型的用前缀和求解的题目。因为甜度值会随着天数成倍增加,所以甜度值大的要先吃。要求出吃1-n颗糖的最小甜度值,先从小到大排序,再进行前缀和运算。
然后简单的dp:
当k小于m时,糖可以一天吃完,所以直接输出a[k]的前缀和即可。
当k大于m时,糖要分几天吃完,所以a[k]要加上a[k-m],表示吃k颗糖的最小甜度值(原a[k]表示从小到大排序后前k颗糖的甜度和,a[k-m]表示前一天吃糖获取的甜度值,两者相加等价于第k-m+1颗糖加到第k颗糖是第一天吃的,所以都乘了1;第k-2m+1颗糖到第k-m颗糖是第二天吃的,所以都乘了2…以此类推)。

代码如下:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <algorithm>
#include <iomanip>
#define LL long long
using namespace std;
const int N=2e5+5;
LL a[N];
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    cin>>a[i];
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++)  //前缀和
    a[i]+=a[i-1];
    for(int k=1;k<=n;k++)
    {                      //求出吃k颗糖的最小甜度,并输出
        if(k>m) a[k]+=a[k-m];
        cout<<a[k]<<" ";
    }
    return 0;
}
发布了16 篇原创文章 · 获赞 4 · 访问量 901

猜你喜欢

转载自blog.csdn.net/li_wen_zhuo/article/details/104777498
今日推荐