CF639A Bear and Displayed Friends - 题解

题目传送门

题意

维护一个队列,每次可以加入一个数到队尾,问这个数是不是比这个队列前面的一项大。

思路


可以使用关联式容器 setset 有自动排序和给出数组长度的功能,很好用。

具体用法:OI-WIKI

这里讲一下本题涉及到的 set 知识点。

  • 插入操作:insert(x) 当容器中没有等价元素的时候,将元素 x 插入到 set 中。

例子:


    set<std::string> myset; 
    string str = "jasonshen_";
    myset.insert(str);
  • 访问队列首个元素:队列名.begin()

  • 删除操作

  1. erase(x) 删除值为 x 的 所有元素,返回删除元素的个数。

  2. erase(pos) 删除迭代器为 pos 的元素,要求迭代器必须合法

代码

#include<bits/stdc++.h>
using namespace std;
set<int> s;
int id[150001];
int main() {
	int n,k,q;
	cin>>n>>k>>q;
	for(int i=1; i<=n; ++i) {
		cin>>id[i];
	}
	for(int i=1; i<=q; ++i) {
		int x,d;
		cin>>x>>d;
		if(x==1) {
			s.insert(id[d]);
			if(s.size()>k) s.erase(s.begin());
		} else {
			if(!s.count(id[d])) cout<<"NO"<<endl;
			else cout<<"YES"<<endl;
		}
	}
}
posted @ 2023-10-04 08:56  _Unnamed  阅读(6)  评论(0)    收藏  举报