codeforces 1253C Sweets Eating 贪心

题目链接:https://codeforces.com/problemset/problem/1253/C

贪心
按甜度排序,选出前 k 颗糖,先吃甜度大的
维护前缀和,每隔 m 颗糖果添加一次前缀和,维护按天数递增的效果

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<stack>
#include<queue>
using namespace std;
typedef long long ll;

const int maxn = 200010;

int n,m;
ll a[maxn],sum[maxn],pre[maxn];

ll read(){ ll s=0,f=1; char ch=getchar(); while(ch<'0' || ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0' && ch<='9'){ s=s*10+ch-'0'; ch=getchar(); } return s*f; }

int main(){
	n = read(), m = read();
	for(int i=1;i<=n;++i) a[i] = read();
	
	sort(a+1,a+1+n);
	for(int i=1;i<=n;++i) sum[i] = sum[i-1] + a[i];
	
	for(int i=1;i<=n;++i){
		int l = max(i-m,0);
		pre[i] = pre[l] + sum[i];
	} 
	
	for(int k=1;k<=n;++k){
		int pos=k,re=k,cnt=1;
		printf("%lld ",pre[k]);
	}printf("\n");
	
	return 0;
}
posted @ 2020-10-15 11:51  Tartarus_li  阅读(100)  评论(0编辑  收藏  举报