[luoguP2073] 送花(set)

本文介绍了一个使用 C++ 标准库 set 结构的示例程序,该程序通过自定义比较运算符来实现对节点结构体的排序,并演示了如何插入、删除节点以及计算节点坐标总和。

传送门

 

set

 

#include <set>
#include <cstdio>
#include <iostream>
#define LL long long

using namespace std;

struct node
{
	LL x, y;
	node(LL x = 0, LL y = 0) : x(x), y(y) {}
	inline bool operator < (const node & gg) const
	{
		return y < gg.y;
	}
};

LL ans1, ans2;
set <node> s;
set <node> :: iterator it;

inline int read()
{
	int x = 0, f = 1;
	char ch = getchar();
	for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
	for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
	return x * f;
}

int main()
{
	int x, y, z;
	while(1)
	{
		z = read();
		if(z == -1) break;
		if(z == 1)
		{
			x = read();
			y = read();
			if(s.find(node(x, y)) != s.end()) continue;
			ans1 += x;
			ans2 += y;
			s.insert(node(x, y));
		}
		if(z == 3)
		{
			if(!s.size()) continue;
			it = s.begin();
			ans1 -= (*it).x;
			ans2 -= (*it).y;
			s.erase(it);
		}
		if(z == 2)
		{
			if(!s.size()) continue;
			it = s.end();
			it--;
			ans1 -= (*it).x;
			ans2 -= (*it).y;
			s.erase(it);
		}
	}
	printf("%lld %lld\n", ans1, ans2);
	return 0;
}

  

转载于:https://www.cnblogs.com/zhenghaotian/p/7410299.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值