跳到正文

题解

题解:B4104 [CSP-X2024 山东] 购物

easy42约 1 分钟
原文
文章信息与版本

文章信息

发布
2024年12月25日
收录
2025年12月12日
更新
2026年11月9日
篇幅
约 1 分钟
获赞
4
题目
B4104

版本

  1. v1当前

贪心。

非常明显,优惠券要用最大的商品才合算。

每次要取最大值,可以采用堆。

看一下是否更合算,如果用优惠券依然不合算,直接不用优惠券就好了。

代码:

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n,m,w,a[100005],cnt;
priority_queue<int>q;//堆
signed main(){
	cin>>n>>m>>w;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		q.push(a[i]);
	}
	while(1){
		int ans=0;
		if(q.size()<m){//细节,不加会报错。
			while(!q.empty()) ans+=q.top(),q.pop();
			cnt+=min(ans,w);
			break;
		}
		for(int i=1;i<=m;i++){
			ans+=q.top();
			q.pop();
		}
		if(ans>w){
			cnt+=w;
		}
		else{
			cnt+=ans;
			while(!q.empty()) cnt+=q.top(),q.pop();
			break;//不用了。
		}
	}
	cout<<cnt;
	return 0;
}
原文