C. Sweets Eating cf1253(贪心)

Tsumugi brought 𝑛 delicious sweets to the Light Music Club. They are numbered from 1 to 𝑛, where the 𝑖-th sweet has a sugar concentration described by an integer 𝑎𝑖.

Yui loves sweets, but she can eat at most 𝑚 sweets each day for health reasons.

Days are 1-indexed (numbered 1,2,3,…). Eating the sweet 𝑖 at the 𝑑-th day will cause a sugar penalty of (𝑑⋅𝑎𝑖), 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 𝑘 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 𝑘 between 1 and 𝑛.

Input
The first line contains two integers 𝑛 and 𝑚 (1≤𝑚≤𝑛≤200 000).

The second line contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤200 000).

Output
You have to output 𝑛 integers 𝑥1,𝑥2,…,𝑥𝑛 on a single line, separed by spaces, where 𝑥𝑘 is the minimum total sugar penalty Yui can get if she eats exactly 𝑘 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 𝑘=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⋅𝑎1+1⋅𝑎4+2⋅𝑎5+2⋅𝑎3+3⋅𝑎6=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 𝑥5=30.

题意: 糖果每天最多吃m个,一共n个糖果。糖果第d天吃的花费是a[i] * d 问你吃k块糖果的最小花费是多少
思路: 这应该是全场最水题了吧。首先贪心策略很明显,先排个序。小的放在后面吃,大的放在前面吃。证明:假设糖果a,b 且花费a > b。假设a先吃花费更大,可以有ad + b(d+k) > a(d+k) + b*d 得到 b > a,故原假设错误。
那么每次额外加一个新的糖果时,这个糖果先吃,前面的糖果顺推。同时维护一个前缀和,每隔m个数加一次。就好了,刚好起到了天数顺推的效果。

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<stack>
#include<map>
#include <algorithm>
using namespace std;
 
typedef long long ll;
int dp[1005];
ll a[200005];
ll sum[200005],sum2[200005];
 
int main()
{
    int n,m;scanf("%d%d",&n,&m);
    for(int i = 1;i <= n;i++)
    {
        scanf("%lld",&a[i]);
        
    }
    sort(a + 1,a + 1 + n);
    for(int i = 1;i <= n;i++)
    {
        if(i > m)
            sum[i] += a[i] + sum[i - m];
        else sum[i] = a[i];
    }
    ll tmp = 0;
    for(int i = 1;i <= n;i++)
    {
        tmp += sum[i];
        printf("%lld ",tmp);
    }
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值