codeforces 658B B. Bear and Displayed Friends(优先队列)
题目链接:
2 seconds
256 megabytes
standard input
standard output
Limak is a little polar bear. He loves connecting with other bears via social networks. He has n friends and his relation with the i-th of them is described by a unique integer ti. The bigger this value is, the better the friendship is. No two friends have the same value ti.
Spring is starting and the Winter sleep is over for bears. Limak has just woken up and logged in. All his friends still sleep and thus none of them is online. Some (maybe all) of them will appear online in the next hours, one at a time.
The system displays friends who are online. On the screen there is space to display at most k friends. If there are more than k friends online then the system displays only k best of them — those with biggest ti.
Your task is to handle queries of two types:
- "1 id" — Friend id becomes online. It's guaranteed that he wasn't online before.
- "2 id" — Check whether friend id is displayed by the system. Print "YES" or "NO" in a separate line.
Are you able to help Limak and answer all queries of the second type?
The first line contains three integers n, k and q (1 ≤ n, q ≤ 150 000, 1 ≤ k ≤ min(6, n)) — the number of friends, the maximum number of displayed online friends and the number of queries, respectively.
The second line contains n integers t1, t2, ..., tn (1 ≤ ti ≤ 109) where ti describes how good is Limak's relation with the i-th friend.
The i-th of the following q lines contains two integers typei and idi (1 ≤ typei ≤ 2, 1 ≤ idi ≤ n) — the i-th query. If typei = 1 then a friend idi becomes online. If typei = 2 then you should check whether a friend idi is displayed.
It's guaranteed that no two queries of the first type will have the same idi becuase one friend can't become online twice. Also, it's guaranteed that at least one query will be of the second type (typei = 2) so the output won't be empty.
For each query of the second type print one line with the answer — "YES" (without quotes) if the given friend is displayed and "NO" (without quotes) otherwise.
4 2 8
300 950 500 200
1 3
2 4
2 3
1 1
1 2
2 1
2 2
2 3
NO
YES
NO
YES
YES
6 3 9
50 20 51 17 99 24
1 3
1 4
1 5
1 2
2 4
2 2
1 1
2 4
2 3
NO
YES
NO
YES
In the first sample, Limak has 4 friends who all sleep initially. At first, the system displays nobody because nobody is online. There are the following 8 queries:
- "1 3" — Friend 3 becomes online.
- "2 4" — We should check if friend 4 is displayed. He isn't even online and thus we print "NO".
- "2 3" — We should check if friend 3 is displayed. Right now he is the only friend online and the system displays him. We should print "YES".
- "1 1" — Friend 1 becomes online. The system now displays both friend 1 and friend 3.
- "1 2" — Friend 2 becomes online. There are 3 friends online now but we were given k = 2 so only two friends can be displayed. Limak has worse relation with friend 1 than with other two online friends (t1 < t2, t3) so friend 1 won't be displayed
- "2 1" — Print "NO".
- "2 2" — Print "YES".
- "2 3" — Print "YES".
题意&&思路:
1操作加入,2是询问,插入由于是有一定容量的,所以插入值大的话还要把最小的弹出去,用优先队列维护最小值,用flag数组记录是否在队列中,用num记录队列的大小;
AC代码:
/*
2014300227 658B - 18 GNU C++11 Accepted 93 ms 3456 KB
*/ #include <bits/stdc++.h>
using namespace std;
const int N=;
int n,k,q,a,b;
int t[N],flag[N];
struct node
{
friend bool operator< (node n1, node n2)
{
return n1.priority>n2.priority;
}
int priority;
int cnt;
};
priority_queue<node>qu;
int main()
{
int num=;
memset(flag,,sizeof(flag));
scanf("%d%d%d",&n,&k,&q);
for(int i=;i<=n;i++)
{
scanf("%d",&t[i]);
}
for(int i=;i<q;i++)
{
scanf("%d%d",&a,&b);
if(a==)
{
if(flag[b])printf("YES\n");
else printf("NO\n");
}
else
{
if(num>)
{
if(num<k)
{
node y;
y.cnt=b;
y.priority=t[b];
qu.push(y);
flag[b]=;
num++;
}
else
{
node x=qu.top();
if(t[b]>x.priority)
{
flag[x.cnt]=;
qu.pop();
node y;
y.cnt=b;
y.priority=t[b];
qu.push(y);
flag[b]=;
}
}
}
else
{
node y;
y.cnt=b;
y.priority=t[b];
qu.push(y);
flag[b]=;
num++;
}
}
} return ;
}
codeforces 658B B. Bear and Displayed Friends(优先队列)的更多相关文章
- 【Codeforces 639A】Bear and Displayed Friends
[链接] 我是链接,点我呀:) [题意] [题解] 时刻维护一下前K大的数字就好. 因为k<=6 然后数字不会减少只会增加. 因此只要维护一个大小为k的数组就ok. 保证这个数组是有序的. 写个 ...
- VK Cup 2016 - Round 1 (Div. 2 Edition) B. Bear and Displayed Friends 树状数组
B. Bear and Displayed Friends 题目连接: http://www.codeforces.com/contest/658/problem/B Description Lima ...
- Codeforces 658B Bear and Displayed Friends【set】
题目链接: http://codeforces.com/contest/658/problem/B 题意: 给定元素编号及亲密度,每次插入一个元素,并按亲密度从大到小排序.给定若干操作,回答每次询问的 ...
- codeforces Codeforces Round #318 div2 A. Bear and Elections 【优先队列】
A. Bear and Elections time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Codeforces Round #318 (Div. 2) A Bear and Elections (优先队列模拟,水题)
优先队列模拟一下就好. #include<bits/stdc++.h> using namespace std; priority_queue<int>q; int main( ...
- 【32.89%】【codeforces 574D】Bear and Blocks
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- codeforces 828 C. String Reconstruction(思维+优先队列)
题目链接:http://codeforces.com/contest/828/problem/C 题解:有点意思的题目,可用优先队列解决一下具体看代码理解.或者用并查集或者用线段树都行. #inclu ...
- codeforces 680C C. Bear and Prime 100(数论)
题目链接: C. Bear and Prime 100 time limit per test 1 second memory limit per test 256 megabytes input s ...
- codeforces 680B B. Bear and Finding Criminals(水题)
题目链接: B. Bear and Finding Criminals //#include <bits/stdc++.h> #include <vector> #includ ...
随机推荐
- window.opener
window.opener 实际上就是通过window.open打开的窗体的父窗体. 比如在父窗体parentForm里面 通过 window.open("subForm.html" ...
- Linux在中国正在走向没落
在中国,Linux正在走向没落,一片萧条景象. 在这样的大背景下.居然有人愿意接手中科红旗,令人佩服! 在中国,没有一个关于国际Linux的官方刊物(或站点)反映国际Linux运动的真实声音.Linu ...
- CentOS开启FTP及配置用户
vsftpd作为FTP服务器,在Linux系统中是非常常用的.下面我们介绍如何在centos系统上安装vsftp. 什么是vsftpd vsftpd是一款在Linux发行版中最受推崇的FTP服务器程序 ...
- ubuntu16.04 Cmake学习二
本节主要总结编译程序的时候使用了第三方库的情况,以调用开源opencv-2.4.9为例子,具体安装详见http://www.cnblogs.com/xsfmg/p/5900420.html. 工程文件 ...
- substring,subsequence,charAt执行效率的不同
package com.java.tencent; public class T_2_longestPalindrome { public String test1(String s){ long s ...
- TOML简介 (转)
TOML的由来 配置文件的使用由来已久,从.ini.XML.JSON.YAML再到TOML,语言的表达能力越来越强,同时书写便捷性也在不断提升. TOML是前GitHub CEO, Tom Prest ...
- LRU java实现
实现LRU缓存,用到了一个链表和 HashMap, HashMap保证了get/set的时间复杂度是O(1), 链表用来记录 最近最少使用的元素,以便用来淘汰. package lru; /** * ...
- 【Atheros】如何在驱动中禁用ACK
上一篇文章讲了如何禁用载波侦听(CSMA)和退避(BACKOFF)的方法,这一篇介绍如何禁用ACK. 禁用ACK主要分为三部分: 1. 在发送端设置不等待ACK回来就继续发送: 2. 在接收端设置收到 ...
- 概率dp HDU 4405
Aeroplane chess Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Sub ...
- Layout布局位置
- - GUILayout 这个本身就是用于自动布局的. 不用给定位置的,这也是它与GUI的区别所在.通常默认开始的位置是屏幕的左上角. 当然你也可以限定开始自动布局的位置.用 GUILayout.B ...