当前位置: 首页 > news >正文

红黑树(set,map)算法题

1.英语作文

P2786 英语1(eng1)- 英语作文 - 洛谷

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<map>

using namespace std;
typedef long long LL;

int n, p;
LL sum = 0;
map<string, int> mp;//高级词汇,含金量

bool check(char s)
{
	if (s >= '0' && s <= '9' || s >= 'a' && s <= 'z' || s >= 'A' && s <= 'Z')
	{
		return true;
	}
	else
		return false;
}

int main()
{
	cin >> n >> p;
	for (int i = 1; i <= n; i++)
	{
		string st; int x; cin >> st >> x;
		mp[st] = x;//自动创建一个key为st的插入到mp中
	}

	char s;
	string res = "";
	while (scanf("%c", &s) != EOF)//这里的EOF表示结束符,这里使用scanf的话可以把空格视作一个分隔符,而cin则会自动跳过
	{
		if (check(s))res += s;
		else
		{
			sum = (sum + mp[res]) % p;
			res = "";
		}
	}
	cout << sum << endl;
	return 0;
}

 while (scanf("%c", &s) != EOF)//这里的EOF表示结束符,这里使用scanf的话可以把空格视作一个分隔符,而cin则会自动跳过空格

mp[t]  = x;开辟一个key值为t的空间,映射x

 2.​​​​​​P2234 [HNOI2002] 营业额统计 - 洛谷

#include<iostream>
#include<set>
#include<math.h>

using namespace std;
int n,sum = 0;
set<int> st;
int INF = 1e7;//极大值

int main()
{
	cin >> n;
	int x; cin >> x;
	sum += x;
	st.insert(x);
	st.insert(INF); st.insert(-INF);

	for (int i = 2; i <= n; i++)
	{
		int y; cin >> y;
		auto r = st.lower_bound(y);//返回的是一个迭代器
		auto l = r;
		l--;
		if (*r == y)continue;
		else
		{
			sum += min(abs(*r - y), abs(y - *l));
		}
		st.insert(y);
	}
	cout << sum << endl;
	return 0;
}

左右护法:鉴于绝对值取小,那么只要采用左右护法可以避免,取迭代空 

3.记录详情 - 洛谷 | 计算机科学教育新生态

#include<iostream>
#include<set>
#include<math.h>
using namespace std;

typedef long long LL;
const LL INF = 1e10;

LL op;
set<LL> st;

int main()
{
	cin >> op;

	//左右护法
	st.insert(-INF);
	st.insert(INF);
	while (op--)
	{
		int x, y;
		cin >> x;
		if (x == 1)
		{
			cin >> y;
			if (st.count(y))
			{
				cout << "Already Exist" << endl;
			}else
			st.insert(y);
		}
		else
		{
			cin >> y;
			auto r = st.lower_bound(y);
			auto l = r;
			l--;
			if (abs(*r - y) >= abs(y - *l))
			{
				cout << *l << endl;
				st.erase(l);
			}
			else
			{
				if (st.size()==2)
				{
					cout << "Empty" << endl;
				}
				else
				{
					cout << *r << endl;
					st.erase(r);
				}
			}
		}
	}
	return 0;
}

相关文章:

  • 简易 Shell 实现指南
  • 第二章:14.1 倾斜数据集的误差指标
  • 性格测评小程序04题库管理
  • 动态规划算法
  • 探索ELK 的魅力
  • 原型模式详解(Java)
  • Nginx location 和 proxy_pass 配置详解
  • [Python 数据科学] Python 的良好编程规范与 logging 的使用
  • 如何下载AndroidStudio的依赖的 jar,arr文件到本地
  • 网络通信的基石:深入理解 TCP/IP 协议栈与 TCP/UDP 协议
  • Linux命令 ----操作命令
  • 2.14寒假
  • Baklib知识中台构建企业智能运营核心架构
  • 基于 openEuler 构建 LVS-DR 群集
  • 《玩转AI大模型:从入门到创新实践》(9)第七章 AI职业地图:从使用者到创造者
  • k8s集群搭建参考(by lqw)
  • DeepSeek处理自有业务的案例:让AI给你写一份小众编辑器(EverEdit)的语法着色文件
  • 给你一个三维numpy,形状为n*120*140,这里n<30,在其第一位维度n上扩展,将其扩展到30,扩展的部分全部设置为零
  • LLM:BERT or BART 之BART
  • 机器学习之AAE(Adversarial Autoencoder,对抗自编码器)生成数据样本
  • 企业网站改版seo/杭州seo网络公司
  • 怎么做网站编程/济南百度公司
  • 网站资源做缓存/百度提交网站入口
  • 网站定制/全网营销推广怎么做
  • 视频网站建设方案/做引流推广的平台600
  • 公安局网站备案/百度关键词推广方案