Less or Equal CodeForces - 977C (sort+细节)
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. 1≤x≤1091≤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 (1≤n≤2⋅1051≤n≤2⋅105, 0≤k≤n0≤k≤n). The second line of the input contains nn integer numbers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤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
7 4
3 7 5 1 10 3 20
6
7 2
3 7 5 1 10 3 20
-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\n",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), '\0', 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=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
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,,n)
{
cin>>a[i];
}
sort(a+,a++n);
if(k==)
{
if(a[]==)
{
cout<<-<<endl;
}else
{
cout<<<<endl;
}
}else
{
if(a[k]!=a[k+])
{
if(a[k]<=)
cout<<a[k]<<endl;
else
{
cout<<-<<endl;
}
}else
{
cout<<-<<endl;
}
} return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
Less or Equal CodeForces - 977C (sort+细节)的更多相关文章
- F - Make It Equal CodeForces - 1065C
题目大意:有n座塔,塔高h[i],每次给定高度H对他们进行削切,要求每次削掉的所有格子数不能超过k个,输出最少削几次才能使所有塔的高度相同. 思路一:差分+贪心 对于每一个高度h,用一个数组让1~h的 ...
- 【HackerRank】 The Full Counting Sort
In this challenge you need to print the data that accompanies each integer in a list. In addition, i ...
- CF #552 div3
A - Restoring Three Numbers CodeForces - 1154A Polycarp has guessed three positive integers aa, bb a ...
- Flink - Juggling with Bits and Bytes
http://www.36dsj.com/archives/33650 http://flink.apache.org/news/2015/05/11/Juggling-with-Bits-and-B ...
- java - day004 - 数组排序,插入,冒泡
// 判断string 是否相等不能用 == 使用 equal 方法 Arrays.sort(数组); 数组排序算法 对基本类型. 优化的快速排序算法 对引用类型, 优化的合并排序算法
- MySQL 8.0.2: Introducing Window Functions
July 18, 2017MySQL, SQLDag Wanvik MySQL 8.0.2 introduces SQL window functions, or analytic functions ...
- 51Nod 1272最大距离 (树状数组维护前缀最小值)
题目链接 最大距离 其实主流解法应该是单调栈……我用了树状数组. #include <bits/stdc++.h> using namespace std; #define rep(i, ...
- Educational Codeforces Round 2 B. Queries about less or equal elements 水题
B. Queries about less or equal elements Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforc ...
- Codeforces Round #486 (Div. 3)-B. Substrings Sort
B. Substrings Sort time limit per test 1 second memory limit per test 256 megabytes input standard i ...
随机推荐
- 自动化测试基础篇--Selenium select下拉框
摘自https://www.cnblogs.com/sanzangTst/p/7681523.html 一.什么是下拉框 下拉框是多项选择项,选择其中一种,类似下面(以百度搜索设置为例) 源代码如下所 ...
- SQL Server的实例恢复解析
同Oracle一样,SQL Server在非一致性关闭的时候也会进行实例恢复(Instance Recovery),本文根据stack overflow的文章介绍一些SQL Server实例恢复的知识 ...
- win10监听剪切板变化
一.第一步导入api #region [DllImport("user32.dll")] public static extern bool AddClipboardFormatL ...
- Unity的AssetDatabase路径格式
开发环境 windows 7 Unity 5.3 及更高版本 前言 使用AssetDatabase.Load或AnimatorController.CreateAnimatorControllerAt ...
- liunx搭建DHCP服务器以及DHCP中继服务器
liunx搭建DHCP服务器以及DHCP中继服务器 一.实验拓扑 二.实验条件 虚拟机取消VMnet1和VMnet8的dhcp动态获取ip地址,以免影响实验 DHCPserver 网关以及DHCP中继 ...
- python3+xlwt 读取txt信息并写入到excel中
# coding = utf-8 import os import xlwt import re def readTxt_toExcel(valueList, Pathlist): workbook ...
- python 实现网页 自动登录
完整代码: 1 from apscheduler.schedulers.blocking import BlockingScheduler 2 from selenium import webdriv ...
- js对WebApi请求的基本操作
在WebAPI对外提供的,大概有4种接口,get,post,delete,put,现在,我就简单的来说一下js请求webApi的方式和大概的作用: get:在webApi中,get方法通常是用来获取数 ...
- Java循环语句怎么用?经典排序算法见真知
Java中循环语句的使用,莫过于在排序算法中使用得最为经典. 排序算法非常的多,不过大体可以分为两种: 一种是比较排序,主要有:冒泡排序,选择排序,插入排序,归并排序,堆排序,快速排序等. 另一种是非 ...
- 【算法】LeetCode算法题-Longest Common Prefix
这是悦乐书的第146次更新,第148篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第5题(顺位题号是14),给定一个随机的字符串数组,查找这些字符串元素的公共前缀字符串, ...