Codeforces Round #600 (Div. 2) C.Sweets Eating

Codeforces Round #600 (Div. 2) C.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.

题意:给出n个糖果,每天最多可以吃m颗,分别求出吃1-n颗糖果获得的最小甜度值。甜度值等于第i颗啊a[i]*第几天。

思路:根据贪心思想,每颗糖果的甜度越小应该越晚吃,需要吃i颗糖果时,将所有糖果甜度值排序取最小的前i个,甜度越大吃的越早。因为每天可以吃m颗糖果,所有吃i颗时的甜度值等于i-1颗时加上排序后取余m等于i%m的糖果。

代码:

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <cstring>

using namespace std;
typedef long long ll;
const int maxx=1e6+7;
ll a[maxx],sum[maxx],ans[maxx];//数值大于int所以用long long
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%I64d",&a[i]);
    }
    sort(a+1,a+n+1);//排序
    for(int i=1;i<=n;i++)
    {
        sum[i%m]+=a[i];
        ans[i]=ans[i-1]+sum[i%m];//i颗时等于i-1时加上在此之前取余等于i%m的
    }
    for(int i=1;i<n;i++)
    {
        printf("%I64d ",ans[i]);
    }
    printf("%I64d\n",ans[n]);
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值