跳到正文

题解

题解:P15232 浣熊的快车道

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

文章信息

收录
2026年2月9日
更新
2026年2月9日
篇幅
约 1 分钟
题目
P15232

版本

  1. v1当前

Solution

简单贪心题。

肯定考虑优先便宜,发现每辆车和每个车道的编号都是无关紧要的,因此可以先排个序。由于要花费最少,所以考虑尽可能多车上最便宜的车道,所以每辆车的限制从大到小依次尝试上目前没满且最便宜的车道,一满就换下一个车道。

如果没有全部塞下输出 −1-1。

code
C++
#include <bits/stdc++.h>
using namespace std;
int n,m,a[100005],b[100005],cnt[100005],idx=1;
long long ans;
int main(){
	cin>>n>>m;
	for(int i = 1;i<=n;i++) cin>>a[i];
	sort(a+1,a+1+n);
	for(int i = 1;i<=m;i++) cin>>b[i];
	sort(b+1,b+1+m,greater<int>());
	for(int i = 1;i<=m;i++){
		if(cnt[idx]+1<=b[i]) ans+=a[idx];
		else idx++,ans+=a[idx];
		cnt[idx]++;
		if(idx>n) return cout<<-1,0;
	}
	cout<<ans;
	return 0;
}
原文