CodeForces 658B Bear and Displayed Friends(好友在线查询)
2016-08-12 23:34
603 查看
http://codeforces.com/problemset/problem/658/B
B. Bear and Displayed Friends
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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?
Input
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 friendidi 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.
Output
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.
Examples
input
output
input
output
Note
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 好友Id: 好友上线;
操作 2 好友Id :查询能否联系的上。
键入数据提示:
朋友数量 n ,可以显示的朋友数量(亲密度优先显示)k,操作的次数 q;
第一个朋友的亲密度............第 n 个朋友的亲密度。
思路:
引入一个 onl[ ] 数组,用来记录已经在线好友的亲密度(没有两个好友是一样的),用于查询 k 范围内在线好友的亲密度即可。
具体看下代码吧。
#include<cstring>
#include<algorithm>
using namespace std;
const int MYDD = 1103+1.5e5;
int fri[MYDD];//friends
int onl[16];//online 注意:1≤k≤min(6,n)
bool cmp(int x,int y) {
return x>y;
}
int main() {
int n,k,q;
while(scanf("%d%d%d",&n,&k,&q)!=EOF) {
for(int j=1; j<=n; j++) {
scanf("%d",&fri[j]);
}
memset(onl,0,sizeof(onl));
while(q--) {
int oper,id;//opertation
scanf("%d%d",&oper,&id);
if(oper==1) {
onl[k]=fri[id];//当前在线的赋值给 onl[]数组
sort(onl,onl+1+k,cmp);//注意排序空间 *wa_bug sort(onl+1,onl+1+k,cmp);
}
if(oper==2) {
bool flag=false;//遍历在线的好友
for(int j=0; j<k; j++) {//存在该亲密度的好友
if(onl[j]==fri[id]) flag=true;
}
if(flag) puts("YES");
else puts("NO");
}
}
}
return 0;
}
后:
***********************
B. Bear and Displayed Friends
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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?
Input
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 friendidi 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.
Output
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.
Examples
input
4 2 8 300 950 500 200 1 3 2 4 2 3 1 1 1 2 2 1 2 2 2 3
output
NO YES NO YES YES
input
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
output
NO YES NO YES
Note
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 好友Id: 好友上线;
操作 2 好友Id :查询能否联系的上。
键入数据提示:
朋友数量 n ,可以显示的朋友数量(亲密度优先显示)k,操作的次数 q;
第一个朋友的亲密度............第 n 个朋友的亲密度。
思路:
引入一个 onl[ ] 数组,用来记录已经在线好友的亲密度(没有两个好友是一样的),用于查询 k 范围内在线好友的亲密度即可。
具体看下代码吧。
AC Code:
#include<cstdio>#include<cstring>
#include<algorithm>
using namespace std;
const int MYDD = 1103+1.5e5;
int fri[MYDD];//friends
int onl[16];//online 注意:1≤k≤min(6,n)
bool cmp(int x,int y) {
return x>y;
}
int main() {
int n,k,q;
while(scanf("%d%d%d",&n,&k,&q)!=EOF) {
for(int j=1; j<=n; j++) {
scanf("%d",&fri[j]);
}
memset(onl,0,sizeof(onl));
while(q--) {
int oper,id;//opertation
scanf("%d%d",&oper,&id);
if(oper==1) {
onl[k]=fri[id];//当前在线的赋值给 onl[]数组
sort(onl,onl+1+k,cmp);//注意排序空间 *wa_bug sort(onl+1,onl+1+k,cmp);
}
if(oper==2) {
bool flag=false;//遍历在线的好友
for(int j=0; j<k; j++) {//存在该亲密度的好友
if(onl[j]==fri[id]) flag=true;
}
if(flag) puts("YES");
else puts("NO");
}
}
}
return 0;
}
后:
***********************
相关文章推荐
- Codeforces - 813E - Army Creation (在线查询区间小于k的数的数量)
- flashbuilder4.5开发查询好友qq是否在线
- 不用软件直接查询QQ好友的IP地址(二) && 通过 Ping 得知对方是否联网在线(与QQ是否上线无关)
- 开发接口api在线查询
- MSDN在线文档查询
- 利用Python了解微信通信机制,实现查询有多少好友删除你!!
- SQL在线查询设计器
- 2016网易笔试-查询互为好友
- 在线准确的天气预报查询
- Codeforces 813E Army Creation 主席树(在线,求[l,r]内比x大的数的个数)
- ajax传输,后台同步,后台json传输,文件,文件夹复制删除,查询,新建,在线压缩
- Csdn's Blog可以看到作者是否在线并且可以添加好友啦。。
- MSDN在线查询
- codeforces 379F New Year Tree 在线LCA
- 查询QQ好友的IP地址(二)
- 网页中嵌套QQ代码设置在线状态无需加好友聊天
- 如何在线查询本地机的文件?
- jQuery1.7 中文API 在线查询 带用例
- [导入]金山词霸在线查询