【市赛】【提高】1526 - 小X与游戏

题目描述:

输入&输出:

现在给出代码:(记得点赞哟)

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <queue>
 
typedef long long LL;
using namespace std;
 
int n;
 
struct node
{
	int x, y;
	bool operator <(const node &a)const{
		return x == a.x ? y > a.y : x < a.x;
	}
}a;
 
priority_queue<node> q;//优先队列维护最大值
 
int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
 
	scanf("%d", &n);
	for (int i = 1; i <= n; i++){
		scanf("%d %d", &a.x, &a.y);
		q.push(a);
	}
	LL ans = -(1LL << 60), sum1 = 0, sum2 = 0;//注意ans的范围和初始化的值
	int num = 0;
	while(q.size()){
		node t = q.top();
		q.pop();
		if(t.y > t.x) continue;//如果下面的牌比上面的大,不要选
		if(num & 1){//小Y选择
			sum2 += t.x;
			ans = max(ans, sum1 - sum2);//一个轮回更新下差
		}else{//小X选择
			sum1 += t.x;
		}
		if(t.y != -1) t.x = t.y, t.y = -1, q.push(t);//如果选择了a[i],那么b[i]代替a[i],b[i]设为尽量小的值(<0)
		num++;
	}
	ans = max(ans, sum1 - sum2);//有可能小Y没有选择,也要更新下值
	printf("%lld\n", ans);
 
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值