Codeforces 658B Bear and Displayed Friends【set】
题目链接:
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】的更多相关文章
- Educational Codeforces Round 35 A. Nearest Minimums【预处理】
[题目链接]: Educational Codeforces Round 35 (Rated for Div. 2) A. Nearest Minimums time limit per test 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 ...
- 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 ...
- 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 ...
- Codeforces 653A Bear and Three Balls【水题】
题目链接: http://codeforces.com/problemset/problem/653/A 题意: 给定序列,找是否存在连续的三个数. 分析: 排序~去重~直接判断~~ 代码: #inc ...
- Codeforces Round #160 (Div. 1) 题解【ABCD】
Codeforces Round #160 (Div. 1) A - Maxim and Discounts 题意 给你n个折扣,m个物品,每个折扣都可以使用无限次,每次你使用第i个折扣的时候,你必须 ...
- 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 题解 直接 ...
- Codeforces Round #271 (Div. 2)题解【ABCDEF】
Codeforces Round #271 (Div. 2) A - Keyboard 题意 给你一个字符串,问你这个字符串在键盘的位置往左边挪一位,或者往右边挪一位字符,这个字符串是什么样子 题解 ...
- Codeforces Round #177 (Div. 1) 题解【ABCD】
Codeforces Round #177 (Div. 1) A. Polo the Penguin and Strings 题意 让你构造一个长度为n的串,且里面恰好包含k个不同字符,让你构造的字符 ...
随机推荐
- RecyclerView 缓存机制学习笔记1
盗用别人图片 获取VIew的方法的流程 最先调用 其次调用 这个方法调用会先去缓存 这个是是否有动画,有动画就去里面取. 如果取不到就接着调用 如果在没有继续调用 都取不到就去实例化 调用的次数取决于 ...
- Xilinx器件原语
原语,其英文名为primitive,是FPGA厂商针对其器件特征开发的一系列常用模块的名称.原语是FPGA芯片中基本元件,代表FPGA中实际拥有的硬件逻辑单元,如LUT,D触发器,RAM等.相当于软件 ...
- Outlook 数据文件(.pst 和 .ost)简介
使用 Microsoft Outlook 时,电子邮件.日历.任务和其他项目保存在邮件服务器或计算机上,或者同时保存在这两个位置.如果 Outlook 项目保存在计算机上,则它们保存在 Outlook ...
- Linux-RedHat7.2 使用CentOS源
-- 查看yum rpm -qa |grep yum --卸载yum rpm -qa | grep yum | xargs rpm -e --nodeps --拷贝centos系统yum工具安装程序到 ...
- python程序的编辑和运行、变量
第一个python程序 python是解释型弱类型高级语言 常见的python解释器CPython.IPython.pypy.JPython.IronPython 方法一.python程序可以写在命令 ...
- Tomcat启动报错 ERROR org.apache.struts2.dispatcher.Dispatcher - Dispatcher initialization failed
背景: 在进行Spring Struts2 Hibernate 即SSH整合的过程中遇到了这个错误! 原因分析: Bean已经被加载了,不能重复加载 原来是Jar包重复了! 情形一: Tomcat ...
- JavaSE-31 Java正则表达式
概述 正则表达式是一个强大的字符串处理工具,可以实现对字符串的查找.提取.分割.替换等操作. String类的几个方法需要依赖正则表达式的支持. 方法 方法说明 boolean matches(Str ...
- spring boot 在idea中实现热部署
1)在pom中直接引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifac ...
- 弹跳加载动画特效Bouncing loader
一款非常常用的css 加载动画,这款CSS3 Loading动画主要由几个小球通过规律的上下跳动,渐隐渐显而成,效果十分生动.流畅.兼容IE8以上,尤其适合在移动端中使用,基本代替了图片实现加载的效果 ...
- Spring Data Redis入门示例:程序配置(五)
单机配置 redis.properties配置 #redis的服务器地址 redis.host=127.0.0.1 #redis的服务端口 redis.port=6379 #客户端超时时间单位是毫秒 ...