http://codeforces.com/problemset/problem/658/B
题意:
给定一些数据,查询朋友当前是否能够联系的上,并且输出结果。
操作有:
操作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;
}
***********************