Sweets Eating(贪心+前缀和)

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
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=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.
题目分析:
这道题看到就知道一定是贪心,但是要1s之内出结果,心想模拟肯定是不行的,但内心告诉我写着玩玩吧。大体思路是这样的,枚举所有k的取值,然后将a从小到大排序,从第k大的数开始吃糖,就这样一点点模拟。下面附上超时代码:

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=2e5+10;
int a[N],x[N];
int main()
{
    int n;
    double m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    sort(a+1,a+n+1);
    for(int k=1;k<=n;k++)//糖的数量
    {
        int day=ceil(k/m*1.0);
        for(int i=1;i<=day;i++)
        {
            int l=k-m*i+1,r=k-m*(i-1);
            if(l<=0) l=1;
            for(int j=r;j>=l;j--)
            {
                x[k]+=i*a[j];
            }
        }
    }
    for(int k=1;k<=n;k++)
        cout<<x[k]<<" ";
    return 0;
}

但后来我发现了更加奇妙的写法,前缀和,即把这些糖果分成每m个一组,存入一个数组里。
拿样例中第一个为例说明一下:
**sum[1]=a[1]
sum[2]=a[2];
sum[3]=sum[1]+a[3]=a[1]+a[3]
sum[4]=sum[2]+a[4]=a[2]+a[4]

一直写下去,当你要求k=3时设结果为ans,则最后结果ans=a[3]+a[2]+a[1]*2=sum[1]+sum[2]+sum[3]
是不是很神奇,我觉得遇到新鲜的做法不能着急理解它的内涵,要循序渐进,先理解它的大概,说不定哪天再遇到或者平常脑海里划过的时候就顿悟了呢。
代码:

#include<iostream>
#include<algorithm>
using namespace std;
const int N=2e5+10;
long long a[N],sum[N];
int main()
{
    int n,m;
    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<0) sum[i]=a[i];
        else sum[i]=sum[i-m]+a[i];
    }
    long long ans=0;
    for(int k=1;k<=n;k++)
    {
        ans+=sum[k];
        cout<<ans<<" ";
    }
    return 0;
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值