CodeForces - 977 C. Less or Equal

C. Less or Equal

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

input

Copy

7 4
3 7 5 1 10 3 20

output

Copy

6

input

Copy

7 2
3 7 5 1 10 3 20

output

Copy

-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个数,叫你求出x,满足有k个数小于等于x。

分析:

水坑啊,就四点,k可以为0,1=<x<=1e9,a[i]>=1,x=-1表示没有情况(-1已经不是数了)。

举例说明把,

3 0

2 2 3

则x=1满足情况,这时候不能x=-1,x的范围

3 0

1 1 3

这时候,只能输出-1

代码实现:

#include<cstdio>  
#include<cstring>  
#include<cstdlib>  
#include<cctype>  
#include<cmath>  
#include<iostream>  
#include<sstream>  
#include<iterator>  
#include<algorithm>  
#include<string>  
#include<vector>  
#include<set>  
#include<map>  
#include<stack>  
#include<deque>  
#include<queue>  
#include<list>  
using namespace std;  
const double eps = 1e-8;  
typedef long long LL;  
typedef unsigned long long ULL;  
const int INF = 0x3f3f3f3f;  
const int INT_M_INF = 0x7f7f7f7f;  
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;  
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;  
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};  
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};  
const int MOD = 1e9 + 7;  
const double pi = acos(-1.0);  
const int MAXN=5010;  
const int MAXM=100010;
using namespace std;
int  a[200005];
int main()
{
   
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
	{
		for(int i=1;i<=n;i++)
			scanf("%d",&a[i]);
		sort(a+1,a+n+1);
		if(m==0) 
		{
			if(a[1]>1)
				cout<<1<<endl;
			else
				cout<<"-1"<<endl;
			continue;
		}
		int flag=0;
		
			if(a[m+1]==a[m])
			{flag=1;  }
				
		
		if(flag==1)
			cout<<"-1"<<endl;
		else
			cout<<a[m]<<endl;
		
	}
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值