• 题意:有一个长度为\(n\)的序列,要求在\([1,10^9]\)中找一个\(x\),使得序列中恰好\(k\)个数满足\(\le x\).如果找不到\(x\),输出\(-1\).

  • 题解:先对这个序列排个序,然后首先要注意\(k=0\)的情况

    如果\(k=0\)并且序列中含有\(1\),那么我们无论如何都找不到比\(1\)小的数,输出\(-1\),如果不含\(1\),那么只要输出\(a[1]-1\)即可.

    如果\(k\ne 0\),那么我们要找前\(k\)个小的数(连续相等的数也算),所以我需要用桶来记录每个数出现的次数,然后遍历序列,累加每个数出现的次数,如果\(sum=k\),那么当前这个数就是我们要找的,如果\(sum>k\)的话,那么我们无论如何都找不到\(x\)(因为\(sum\)记录的是\(\le x\)的数目).

  • 代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <stack>
    #include <queue>
    #include <vector>
    #include <map>
    #include <set>
    #include <unordered_set>
    #include <unordered_map>
    #define ll long long
    #define fi first
    #define se second
    #define pb push_back
    #define me memset
    const int N = 1e6 + 10;
    const int mod = 1e9 + 7;
    using namespace std;
    typedef pair<int,int> PII;
    typedef pair<long,long> PLL; int n,k;
    int a[N];
    map<int,int> mp;
    int main() {
    ios::sync_with_stdio(false);cin.tie(0);
    cin>>n>>k;
    for(int i=1;i<=n;++i){
    cin>>a[i];
    mp[a[i]]++;
    } sort(a+1,a+1+n);
    if(k==0){
    if(mp[1]) puts("-1");
    else printf("%d\n",a[1]-1);
    return 0;
    }
    int cnt=0;
    for(auto w:mp){
    cnt+=w.se;
    if(cnt==k){
    printf("%d\n",w.fi);
    return 0;
    }
    if(cnt>k){
    puts("-1");
    return 0;
    }
    }
    puts("-1"); return 0;
    }

Codeforces Round #479 (Div. 3) C. Less or Equal (排序,贪心)的更多相关文章

  1. Codeforces Round #479 (Div. 3) C. Less or Equal

    题目地址:http://codeforces.com/contest/977/problem/C 题解:给一串数组,是否找到一个数x,找到k个数字<=x,找到输出x,不能输出-1.例如第二组,要 ...

  2. Codeforces Round #479 (Div. 3) A. Wrong Subtraction

    题目网址:http://codeforces.com/contest/977/problem/A 题解:给你一个数n,进行k次变换,从末尾开始-1,512变成511,511变成510,510会把0消掉 ...

  3. Codeforces Round #479 (Div. 3) F. Consecutive Subsequence (简单dp)

    题目:https://codeforces.com/problemset/problem/977/F 题意:一个序列,求最长单调递增子序列,但是有一个要求是中间差值都是1 思路:dp,O(n)复杂度, ...

  4. Codeforces Round #479 (Div. 3)解题报告

    题目链接: http://codeforces.com/contest/977 A. Wrong Subtraction 题意 给定一个数x,求n次操作输出.操作规则:10的倍数则除10,否则减1 直 ...

  5. Codeforces Round #479 (Div. 3) B. Two-gram

    原题代码:http://codeforces.com/contest/977/problem/B 题解:有n个字符组成的字符串,输出出现次数两个字符组合.例如第二组样例ZZ出现了两次. 方法:比较无脑 ...

  6. Codeforces Round #479 (Div. 3) 题解 977A 977B 977C 977D 977E 977F

    A. Wrong Subtraction 题目大意:   定义一种运算,让你去模拟 题解:   模拟 #include <iostream> #include <cstdio> ...

  7. Codeforces Round #479 (Div. 3)题解

    CF首次推出div3给我这种辣鸡做,当然得写份博客纪念下 A. Wrong Subtraction time limit per test 1 second memory limit per test ...

  8. Codeforces Round #479 (Div. 3)

    手速场2333,这群人贼牛逼!手速贼快!   A. Wrong Subtraction time limit per test 1 second memory limit per test 256 m ...

  9. E. Cyclic Components (DFS)(Codeforces Round #479 (Div. 3))

    #include <bits/stdc++.h> using namespace std; *1e5+; vector<int>p[maxn]; vector<int&g ...

随机推荐

  1. select 里面带的值居然是估算的?

    mysql> set profiling=1;Query OK, 0 rows affected, 1 warning (0.07 sec) mysql> select count(1) ...

  2. binlog-do-db

    如果只是对一个数据库设置,其实没有效果的,其他数据还是会记录binlog的 binlog-ignore-db =database b  binlog日志里面将不会记录database b的所有相关的操 ...

  3. 不要把file,process或者super权限授予管理员以外的账号

    file权限的主要作用是通过select ....into outfile 写到服务器上具有写权限的目录下,作为文本格式存放,具有权限的目录也就是启动mysql时的用户权限目录.(没有理解) 可以将有 ...

  4. 摆脱 996——GitHub 热点速览 v.21.03

    作者:HelloGitHub-小鱼干 Twitter 有位程序员总结了本周的 GitHub 中文程序员的看点:国内程序员日常--考公务员.996.抢茅台.刷算法.整健康码.在本期热点速览里,小鱼干收录 ...

  5. 【Linux】dlopen failed: /lib/lsiRAID.so: cannot open shared object file: No such file or directory

    遇到这个问题,首先第一反应,是看其他的服务器中是否有这个库文件,如果有的话直接cp过来一份就行 但是检查发现,其他的系统中也不存在lsiRAID.so这个库文件,很神奇.. 但是看日志持续报错,查看s ...

  6. 优先队列priority_queue排序

    优先队列默认大顶堆,即堆顶元素是最大值 改成小顶堆时: priority_queue<int,vector<int>, greater<int> > Q;//注意最 ...

  7. MAVEN编译NIFI源码

    场景: 由于项目需求,需要借用NIFI进行二次开发,因此需要将NIFI源码进行修改,然后编译,办公环境无外网. 步骤: (1)   找一台可以上网(外网)的机器,安装java环境和maven环境,安装 ...

  8. M8 E147 不可能为条目*确立账户

    今天用BAPI做发票校验, BAPI_INCOMINGINVOICE_CREATE这个函数使用都正常,可是突然就无法做发票检验了 报了个错误,"不可能为条目BOXT TR 确立账户" ...

  9. Error Code: 2006 - MySQL 5.7 server has gone away

    使用 Navicat 执行 sql 脚本失败 出现 Error Code: 2006 - MySQL server has gone away 原因 当MySQL客户端或mysqld服务器收到大于ma ...

  10. Java 从Character和char的区别来学习自动拆箱装箱

    本文结构 1.Character和char 的区别: 2.自动拆箱装箱 1.Character和char 的区别: Character是类,char基本数据类型. 在java中有三个类负责对字符的操作 ...