【Codeforces 639A】Bear and Displayed Friends

【链接】 我是链接,点我呀:)
【题意】

【题解】

时刻维护一下前K大的数字就好。 因为k<=6 然后数字不会减少只会增加。 因此只要维护一个大小为k的数组就ok. 保证这个数组是有序的。 写个插入排序(或者sort也可以 然后询问的话就循环k次遍历就ok

【代码】

#include <bits/stdc++.h>
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define LL long long
using namespace std;

const int N = 15e4;

int n,k,q;
int temp[10],a[N+10],cnt;

int main()
{
    //freopen("D:\rush.txt","r",stdin);
    scanf("%d%d%d",&n,&k,&q);
    rep1(i,1,n) scanf("%d",&a[i]);
    rep1(i,1,q){
        int ope,id;
        scanf("%d%d",&ope,&id);
        if (ope==1){
            int idx = -1;
            rep2(j,k,1)
                if (a[id]>temp[j])
                    idx = j;
            if (idx==-1) continue;
            rep2(j,k,idx+1) temp[j] = temp[j-1];
            temp[idx] = a[id];

        }else{
            bool fi = false;
            rep1(j,1,k)
                if(a[id]==temp[j]){
                    fi = true;
                    break;
                }
            if (fi){
                puts("YES");
            }else{
                puts("NO");
            }
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/9744909.html