概述
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("%I64dn",ans[n]);
}
最后
以上就是谨慎黑猫为你收集整理的Codeforces Round #600 (Div. 2) C.Sweets Eating的全部内容,希望文章能够帮你解决Codeforces Round #600 (Div. 2) C.Sweets Eating所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复