bzoj 4781: [Usaco2017 Open]Paired Up

版权声明:本文为博主原创文章,转载请联系博主。 https://blog.csdn.net/everlasting_20141622/article/details/77696661

题目链接


每次把最大的牛与最小的牛合并,最终结果一定是最大值最小的

所以我们用优先队列维护一个大根堆,再维护一个小根堆,每次合并更新下答案就好


靠,排个序就好,我是智障,不改了


代码:

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cstdio>

using namespace std;

struct node1{
	int sum,val,num;
	friend bool operator < (node1 a,node1 b){
		return a.val<b.val;
	}
};

struct node2{
	int sum,val,num;
	friend bool operator < (node2 a,node2 b){
		return a.val>b.val;
	}
};

int n;
priority_queue<node1>q1;
priority_queue<node2>q2;
int ans=0;

int main(){
	scanf("%d",&n);
	node1 t1;
	node2 t2;
	for(int i=1; i<=n; i++){
		scanf("%d%d",&t1.sum,&t1.val);
		t2.sum=t1.sum;
		t2.val=t1.val;
		t1.num=t2.num=i;
		q1.push(t1);
		q2.push(t2);
	}
	t1.sum=t2.sum=0;
	t1.num=0;
	t2.num=1;
	while(t1.num!=t2.num){
		if(t1.sum==0)t1=q1.top(),q1.pop();
		if(t2.sum==0)t2=q2.top(),q2.pop();
		if(t1.num==t2.num){
			ans=max(ans,t1.val*2);
			break;
		}
		int x=min(t1.sum,t2.sum);
		ans=max(ans,t1.val+t2.val);
		t1.sum-=x;
		t2.sum-=x;
	}
	printf("%d\n",ans);
	
	return 0;
}


没有更多推荐了,返回首页