与红黑树有关算法题

文章目录

  • [1. 英语作⽂(逐个字符输入)](#1. 英语作⽂(逐个字符输入))
  • [2. P2234 [HNOI2002] 营业额统计(迭代器加减,越界访问问题)](#2. P2234 [HNOI2002] 营业额统计(迭代器加减,越界访问问题))
  • [3. ⽊材仓库](#3. ⽊材仓库)

1. 英语作⽂(逐个字符输入)

https://www.luogu.com.cn/problem/P2786

将 <单词, 含⾦量> 绑定放在 map 中,然后遍历作⽂中的每⼀个字符串,找找 map 中对应单词的含⾦量,累加起来即可。

道题我反而觉得最难的操作是逐个逐个字符的正确输入

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int n,p;
LL ret;//存储含金量
map<string,int> mp;//一个存词,一个存含金量
// 判断 ch 是否合法
bool check(char ch)
{
if((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' &&
ch <= 'Z'))
{
return true;
}
else return false;
}

int main()
{
    cin>>n>>p;
    while(n--)
    {
      string s;
        int t;
      cin>>s>>t;
       mp[s]=t;
    }
    //这道题唯一难点 我觉得就是 如何一个一个读取字符,同时要过滤非法字符
    string t="";
    char ch;
    while(scanf("%c",&ch)!=EOF)
    {
        if(check(ch)) t+=ch;
        else//读取非法字符 则开始结算是否要加分并且重置t
        {
            ret=(ret+mp[t])%p;
            t="";
        }
    }
    
    cout<<ret;
    
    
}

2. P2234 [HNOI2002] 营业额统计(迭代器加减,越界访问问题)

https://www.luogu.com.cn/problem/P2234

对于每一个新来的数 (x),找出之前的数中,大于等于 (x) 的最小值 (y),以及小于等于 (x) 的最大值 (z),也就是距离 (x) 最近的一大一小两个数。那么 (y - x) 与 (x - z) 的最小值就是当天的最小波动值。

把所有的最小波动值累加起来即可。

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
const int INF=1e7+10;
set<int> st;
int main()
{
    int ret,n;
    cin>>n;
    cin>>ret;
    st.insert(ret);
    //防止迭代器越界 给个不影响结果的值!
    st.insert(-INF);
    st.insert(INF);
    for(int i=2;i<=n;i++)
    {
        int x;cin>>x;
        auto t1=st.lower_bound(x);
        auto t2=t1;
        t2--;
        ret+=min(abs(x-*t1),abs(x-*t2));
        st.insert(x);
    }
    cout<<ret;
}

3. ⽊材仓库

https://www.luogu.com.cn/problem/P5250

与上题的解法类似,⽤ set 来存储⽊材的信息,每次进货和出货操作都在 set 中进⾏。

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL INF=1e10+10;
set<LL> st;
int main()
{
    st.insert(-INF);
    st.insert(INF);
    int n;cin>>n;
    while(n--)
    {
        int x,y;
        cin>>x>>y;
        if(x==1)
        {
            if(st.count(y)==1)
            {
                cout<<"Already Exist"<<endl;
            }
            else
            {
                st.insert(y);
            }
        }
        else
        {
            if(st.size()==2)
            {
                cout<<"Empty"<<endl;
                continue;
            }
            auto t1=st.lower_bound(y);
            auto t2=t1;
            t2--;
            if(abs(y-*t1)<abs(y-*t2))
            {
                cout<<*t1<<endl;
                st.erase(t1);
            }
            else
            {
                cout<<*t2<<endl;
                st.erase(t2);
            }

        }
    }
}
相关推荐
无敌昊哥战神1 天前
【保姆级题解】力扣17. 电话号码的字母组合 (回溯算法经典入门) | Python/C/C++多语言详解
c语言·c++·python·算法·leetcode
脱氧核糖核酸__1 天前
LeetCode热题100——238.除了自身以外数组的乘积(题目+题解+答案)
数据结构·c++·算法·leetcode
再卷也是菜1 天前
算法提高篇(1)线段树(上)
数据结构·算法
py有趣1 天前
力扣热门100题之单词拆分
算法·leetcode
j_xxx404_1 天前
C++算法:哈希表(简介|两数之和|判断是否互为字符重排)
数据结构·c++·算法·leetcode·蓝桥杯·力扣·散列表
Aaron15881 天前
RFSOC+VU13P+RK3588的核心优势与应用场景分析
嵌入式硬件·算法·matlab·fpga开发·信息与通信·信号处理·基带工程
优家数科1 天前
精准预测:基于多维用水量的滤芯寿命预警算法
算法
脱氧核糖核酸__1 天前
LeetCode热题100——189.轮转数组(题解+答案+要点)
数据结构·c++·算法·leetcode
贾斯汀玛尔斯1 天前
每天学一个算法-快速排序(Quick Sort)
数据结构·算法
炽烈小老头1 天前
【每天学习一点算法 2026/04/16】逆波兰表达式求值
学习·算法