题解:P15232 浣熊的快车道

· · 题解

P15232 题解:

主要思路:

贪心。

## 代码实现: $ans$ 记录最终结果,每有一辆车上路就累加答案。$x$ 记录当前道路上的车的数量,如果小于当前车的限制就把这辆车加到道路上。$cnt$ 记录所有上路的车的数量,如果小于 $m$ 就说明无解,输出 $-1$。 **Code:** ```cpp #include <bits/stdc++.h> #define int long long using namespace std; constexpr int N=1e5+7,M=1e3+7; int n,m; int a[N],b[N]; int sum; int32_t main(){ cin.tie(nullptr)->ios::sync_with_stdio(false); cin>>n>>m; for(int i=1;i<=n;i++){ cin>>a[i]; } for(int i=1;i<=m;i++){ cin>>b[i]; } sort(a+1,a+n+1); sort(b+1,b+m+1,greater<int>()); int ans=0; int cnt=0; for(int i=1;i<=n;i++){ int x=0; for(int j=cnt+1;j<=m;j++){ if(x<b[j]){ x++;//累加当前道路上车的数量 cnt++;//累加所有道路上车的总数 ans+=a[i];//记录最终结果 }else{ break; } } } if(cnt<m) cout<<-1;//判断是否无解 else cout<<ans; return 0; } ``````