题目链接:

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. Android开发实现高德地图定位

    1.获取Key 参考官方文档:http://lbs.amap.com/api/android-location-sdk/guide/create-project/get-key 对于签名文件的获取建议 ...

  2. 【HEVC简介】ALF-Adative Loop Filter

    由于HEVC在HM4.0之后,就把ALF去掉,所以ALF的介绍是基于AVS2. <HEVC标准介绍.HEVC帧间预测论文笔记>系列博客,目录见:http://www.cnblogs.com ...

  3. prevent to do sth 与 prevent sb (from) doing 用法

    prevent to do sth 如: Do not water in before making a turn every time 9 days, make wilting of its bra ...

  4. 你的项目刚刚启动?是时候考虑Globalization了!

    今天继续由SAP成都研究院非典型程序猿, 菜园子小哥王聪给大家带来分享. 关于这个很长的定语的由来,请参考这篇文章,里面有王聪的背景介绍,包括他种菜的特长:当我用UI5诊断工具时我用些什么. 秋天到了 ...

  5. ubuntu install zabbix

    ubuntu install zabbix reference1 reference2 some ERRORS raise during install process, may it help. z ...

  6. elasticsearch 中文 term & completion suggester

    Term suggester 创建索引 curl -XPUT 'http://172.16.125.136:9200/term?pretty'创建 mapping curl -XPOST http:/ ...

  7. 我的java web之路(JSP基本语法)

    1.JSP注释 1.1输出注释 语法格式  <!--comment [<%= expression %>] --> <body> This is my JSP pa ...

  8. SolrCloud 概念

    原文链接 https://www.w3cschool.cn/solr_doc 当您的集合对于一个节点来说太大时,您可以通过创建多个分片将其分解并分段存储. 碎片是集合的逻辑分区,包含集合中的文档的子集 ...

  9. 刁肥宅详解中缀表达式求值问题:C++实现顺序/链栈解决

    1. 表达式的种类 如何将表达式翻译成能够正确求值的指令序列,是语言处理程序要解决的基本问题,作为栈的应用事例,下面介绍表达式的求值过程. 任何一个表达式都是由操作数(亦称运算对象).操作符(亦称运算 ...

  10. sql使用row_number()查询标记行号

    背景: 在分页功能中,记录需分页显示,需要row_number()函数标记行号. 数据表: 排序之前数据表显示: sql语句: select ROW_NUMBER() over(order by id ...