HihoCoder 1631 2017ICPC北京签到题

这题不能简单的计算

应该用堆来模拟

代码:

#include<bits/stdc++.h>
using namespace std;
int A[200];
bool cmp(pair<int,int> a,pair<int,int> b){
	if(a.first==b.first)return a.second<b.second;
	else return a.first<b.first;
}
int main(){
	int m,n,k;
	ios::sync_with_stdio(0);
	while(cin>>m>>n>>k){
		for(int i=1;i<=n;++i)cin>>A[i];
		priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > Q;
		int ans1=m,ans2=0;
		sort(A+1,A+1+n);
		for(int i=1;i<=n;++i){
			if(ans1>0){
				Q.push(make_pair(A[i],A[i])),ans1--,ans2++;
			}
		}
		while(Q.size()&&Q.top().first<=k){
			if(Q.top().first==k){
				--ans2;
				Q.pop();
				continue;
			}
			if(ans1>0){
				--ans1;
				Q.push(make_pair(Q.top().first+Q.top().second,Q.top().second));
				Q.pop();
			}else{
				--ans2;
				Q.pop();
			}
		}
		cout<<ans1<<" "<<ans2<<endl;
	}
}

猜你喜欢

转载自blog.csdn.net/Gipsy_Danger/article/details/82312700