1. 程式人生 > >【Codeforces 639A】Bear and Displayed Friends

【Codeforces 639A】Bear and Displayed Friends

i++ 題解 pla pre href ace define 維護 false

【鏈接】 我是鏈接,點我呀:)
【題意】

【題解】


時刻維護一下前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;
}

【Codeforces 639A】Bear and Displayed Friends