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个不同字符,让你构造的字符 ...
随机推荐
- Verilog设计中的锁存器
问题: 什么是锁存器? 什么时候出现锁存器? 锁存器对电路有什么影响? 如何在FPGA设计中避免锁存器? 在FPGA设计中应该避免锁存器.实际上,锁存器与D触发器实现的逻辑功能基本相同,都有暂存数据的 ...
- Java练习题02
问题: 编程求一个整数数组的最大值.最小值.平均值和所有元素的和. 代码: public class Page99{ public static void main(String args[] ...
- 基于 python 的接口测试框架
项目背景 公司内部的软件采用B/S架构,管理实验室数据,实现数据的存储和分析统计.大部分是数据的增删改查,由于还在开发阶段,所以UI界面的变化非常快,之前尝试过用python+selenium进行UI ...
- 原创:shell两个整数的比较 思想版
思想是学的 代码创作是自己的 很喜欢前几行的逻辑严谨 #!/bin/bash#判断两个整数的大小read -p "请输入两个整数a b :" a b #或者使用a=$1[ -z & ...
- systemtap执行过程中报probe timer.profile registration error
probe timer.profile registration error 今天在执行火焰图的过程中,代码报错,probe timer.profile registration error 经过查询 ...
- Android(java)学习笔记167:横竖屏切换时Activity的生命周期
1.横竖屏切换的生命周期 默认情况下横竖屏切换,先销毁再创建 2.有的时候,默认情况下的横竖屏切换(先销毁再创建),对应用户体验是不好的,比如是手机游戏横竖屏切换对游戏体验非常不好,下面两种方 ...
- [Redis] 基于redis的分布式锁
前言分布式锁一般有三种实现方式:1. 数据库乐观锁:2. 基于Redis的分布式锁:3. 基于ZooKeeper的分布式锁.本篇博客将介绍第二种方式,基于Redis实现分布式锁. 可靠性首先,为了确保 ...
- python导包一不小心就入坑(常用解决办法)
常见导包报错: - ImportError:No module named - SystemError: Parent module '' not loaded, cannot perform rel ...
- Android流行界面结构——Fragment通过ViewPager(带指示器)嵌套Fragment结构的创建方法详解
原创文章,转载请注明出处http://www.cnblogs.com/baipengzhan/p/6287213.html 当前Android流行界面结构的一种——Fragment通过ViewPage ...
- Linux的硬件时间、校正Linux系统时间及系统时间调用流程
第一部分: 一)概述: 事实上在Linux中有两个时钟系统,分别是系统时间和硬件时间 UTC是协调世界时(Universal Time Coordinated)英文缩写,它比北京时间早8个小时. ...