zoukankan      html  css  js  c++  java
  • Less or Equal CodeForces

    You are given a sequence of integers of length nn and integer number kk. You should print any integer number xx in the range of [1;109][1;109] (i.e. 1x1091≤x≤109) such that exactly kk elements of given sequence are less than or equal to xx.

    Note that the sequence can contain equal elements.

    If there is no such xx, print "-1" (without quotes).

    Input

    The first line of the input contains integer numbers nn and kk (1n21051≤n≤2⋅105, 0kn0≤k≤n). The second line of the input contains nn integer numbers a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109) — the sequence itself.

    Output

    Print any integer number xx from range [1;109][1;109] such that exactly kk elements of given sequence is less or equal to xx.

    If there is no such xx, print "-1" (without quotes).

    Examples

    Input
    7 4
    3 7 5 1 10 3 20
    Output
    6
    Input
    7 2
    3 7 5 1 10 3 20
    Output
    -1

    Note

    In the first example 55 is also a valid answer because the elements with indices [1,3,4,6][1,3,4,6] is less than or equal to 55 and obviously less than or equal to 66.

    In the second example you cannot choose any number that only 22 elements of the given sequence will be less than or equal to this number because 33 elements of the given sequence will be also less than or equal to this number.

    题意:

    给你一个含有N个数组的数组,给你一个整数k,0<=k<=n 

    让你输出一个数x,使之在这N个数中,有严格的k个数小于等于x。x的范围是1~1e9

    思路:

    排序后,如果a[k]!=a[k+1] 

    输出a[k]就行了。

    注意 下这几个坑点:

    x的范围:1~1e9,

    如果k是0,那么需要特判a[1] 是否是1

    如果k为N,输出a[n]即可。

    细节注意好就不会出错,。

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <vector>
    #include <iomanip>
    #define ALL(x) (x).begin(), (x).end()
    #define rt return
    #define dll(x) scanf("%I64d",&x)
    #define xll(x) printf("%I64d
    ",x)
    #define sz(a) int(a.size())
    #define all(a) a.begin(), a.end()
    #define rep(i,x,n) for(int i=x;i<n;i++)
    #define repd(i,x,n) for(int i=x;i<=n;i++)
    #define pii pair<int,int>
    #define pll pair<long long ,long long>
    #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
    #define MS0(X) memset((X), 0, sizeof((X)))
    #define MSC0(X) memset((X), '', sizeof((X)))
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define eps 1e-6
    #define gg(x) getInt(&x)
    #define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
    using namespace std;
    typedef long long ll;
    ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
    ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
    inline void getInt(int* p);
    const int maxn=1000010;
    const int inf=0x3f3f3f3f;
    /*** TEMPLATE CODE * * STARTS HERE ***/
    
    ll n;
    ll a[maxn];
    int main()
    {
        //freopen("D:\common_text\code_stream\in.txt","r",stdin);
        //freopen("D:\common_text\code_stream\out.txt","w",stdout);
        gbtb;
        cin>>n;
        ll k;
        cin>>k;
        repd(i,1,n)
        {
            cin>>a[i];
        }
        sort(a+1,a+1+n);
        if(k==0)
        {
            if(a[1]==1)
            {
                cout<<-1<<endl;
            }else
            {
                cout<<1<<endl;
            }
        }else
        {
            if(a[k]!=a[k+1])
            {
                if(a[k]<=1000000000)
                    cout<<a[k]<<endl;
                else
                {
                    cout<<-1<<endl;
                }
            }else
            {
                cout<<-1<<endl;
            }
        }
        
        
        return 0;
    }
    
    inline void getInt(int* p) {
        char ch;
        do {
            ch = getchar();
        } while (ch == ' ' || ch == '
    ');
        if (ch == '-') {
            *p = -(getchar() - '0');
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 - ch + '0';
            }
        }
        else {
            *p = ch - '0';
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 + ch - '0';
            }
        }
    }

    本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
  • 相关阅读:
    JAVA-初步认识-第五章-数组-常见操作-最值2
    JAVA-初步认识-第五章-数组-常见操作-最值
    JAVA-初步认识-第五章-数组-常见操作-遍历
    JAVA-初步认识-第五章-数组-第二种定义格式
    JAVA-初步认识-第四章-数组-常见问题
    JAVA-初步认识-第四章-内存图解
    日期加1程序
    发生了COMException 异常来自 HRESULT:0x80040228
    设置窗体的可见性无效
    DotNetBar RibbonControl 控件动态添加项
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/10666742.html
Copyright © 2011-2022 走看看