题目链接:

http://codeforces.com/contest/658/problem/B

题意:

给定元素编号及亲密度,每次插入一个元素,并按亲密度从大到小排序。给定若干操作,回答每次询问的元素是否排在前k个。

分析:

先附上我的垃圾做法:

之前没怎么用过set,比赛的时候第一反应就是利用优先级队列,每次将元素直接插入队列中,由于k最大为6,所以遍历队列的前k个,然后看是否有查询的元素。

代码:

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn = 1500005;
int t[maxn];
vector<int>v;
int main (void)
{
int n, k ,q;
scanf("%d%d%d", &n, &k, &q);
for(int i = 1; i <= n; i++){
scanf("%d", &t[i]);
}
int num, a;
priority_queue<int, vector<int>, less<int> >que;
for(int i = 0; i < q; i++){
scanf("%d%d", &num, &a);
if(num == 1){
que.push(t[a]);
}else{
int flag = 0, cnt = 0;
while(cnt < k && !que.empty() ){
int tmp = que.top();
if(tmp == t[a]){
printf("YES\n");
flag = 1;
break;
}
que.pop();
cnt++;
v.push_back(tmp);
}
for(int i = 0 ; i < v.size(); i++)
que.push(v[i]);
v.clear();
if(!flag) printf("NO\n");
}
}

其实用set可以非常快的解决:

#include <cstdio>
#include<iostream>
#include<set>
using namespace std;
set<int>s;
const int maxn = 150005;
int t[maxn]; int main (void)
{
int n, k, q; cin>>n>>k>>q;
for(int i = 1; i <= n; i++){
cin>>t[i];
}
int type, a;
for(int i = 0; i < q; i++){
cin>>type>>a;
if(type == 1){
s.insert(t[a]);
if(s.size() > k) s.erase(s.begin());
}else{
if(!s.count(t[a])) cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
}
return 0;
}

Codeforces 658B Bear and Displayed Friends【set】的更多相关文章

  1. Educational Codeforces Round 35 A. Nearest Minimums【预处理】

    [题目链接]: Educational Codeforces Round 35 (Rated for Div. 2) A. Nearest Minimums time limit per test 2 ...

  2. Codeforces 526D - Om Nom and Necklace 【KMP】

    ZeptoLab Code Rush 2015 D. Om Nom and Necklace [题意] 给出一个字符串s,判断其各个前缀是否是 ABABA…ABA的形式(A和B都可以为空,且A有Q+1 ...

  3. Educational Codeforces Round 31 A. Book Reading【暴力】

    A. Book Reading time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  4. Codeforces Round #445 A. ACM ICPC【暴力】

    A. ACM ICPC time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  5. Codeforces 653A Bear and Three Balls【水题】

    题目链接: http://codeforces.com/problemset/problem/653/A 题意: 给定序列,找是否存在连续的三个数. 分析: 排序~去重~直接判断~~ 代码: #inc ...

  6. Codeforces Round #160 (Div. 1) 题解【ABCD】

    Codeforces Round #160 (Div. 1) A - Maxim and Discounts 题意 给你n个折扣,m个物品,每个折扣都可以使用无限次,每次你使用第i个折扣的时候,你必须 ...

  7. Codeforces Round #383 (Div. 2) 题解【ABCDE】

    Codeforces Round #383 (Div. 2) A. Arpa's hard exam and Mehrdad's naive cheat 题意 求1378^n mod 10 题解 直接 ...

  8. Codeforces Round #271 (Div. 2)题解【ABCDEF】

    Codeforces Round #271 (Div. 2) A - Keyboard 题意 给你一个字符串,问你这个字符串在键盘的位置往左边挪一位,或者往右边挪一位字符,这个字符串是什么样子 题解 ...

  9. Codeforces Round #177 (Div. 1) 题解【ABCD】

    Codeforces Round #177 (Div. 1) A. Polo the Penguin and Strings 题意 让你构造一个长度为n的串,且里面恰好包含k个不同字符,让你构造的字符 ...

随机推荐

  1. element ui select组件和table做分页完整功能和二级联动效果

    <template> <div class="index_box"> <div class="search_box"> &l ...

  2. TebsorFlow低阶API(五)—— 保存和恢复

    简介 tf.train.Saver 类提供了保存和恢复模型的方法.通过 tf.saved_model.simple_save 函数可以轻松地保存适合投入使用的模型.Estimator会自动保存和恢复 ...

  3. 关于js中的then(盗)

    then()相关的东西包括但不限于:promise,thien.js 虽然还没彻底搞清楚这些个玩意儿,但是  现在知道了  then()是干嘛的了 最主要的,是解决了异步方法立刻返回的问题  这个特性 ...

  4. 32位和64位系统下 int、char、long、double所占的内存

    32位和64位系统下 int.char.long.double所占内存

  5. Spring Data Redis入门示例:字符串操作(六)

    Spring Data Redis对字符串的操作,封装在了ValueOperations和BoundValueOperations中,在集成好了SPD之后,在需要的地方引入: // 注入模板操作实例 ...

  6. php微信公众号开发之快递查询

    [文章来源:脚本之家   文章地址:https://www.jb51.net/article/149205.htm] 本文实例为大家分享了php微信公众号开发之快递查询的具体代码,供大家参考,具体内容 ...

  7. for、while循环

    for循环 # for 循环后面可以对Iterable或者Iterator进行遍历 # "abc"和[1,2,3]为可迭代对象,range(4)为迭代器 for i in &quo ...

  8. logging ,re 模块

    一,复习 # random: random() randint() choice() sample() # 序列化:对象需要持久化存储或传送 对象 => 字符串 # json: 用于传输 # - ...

  9. 实验楼Python学习记录_挑战字符串操作

    自我学习记录 Python3 挑战实验 -- 字符串操作 目标 在/home/shiyanlou/Code创建一个 名为 FindDigits.py 的Python 脚本,请读取一串字符串并且把其中所 ...

  10. .NET中的缓存实现

    软件开发中最常用的模式之一是缓存,这是一个简单但非常有效的概念,想法是重用操作结果,执行繁重的操作时,我们会将结果保存在缓存容器中,下次我们需要该结果时,我们将从缓存容器中取出它,而不是再次执行繁重的 ...