1. 程式人生 > 實用技巧 >codeforces 1253C Sweets Eating 貪心

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;
}