codeforces872B

给出一个有n个整数的数组 a1, a2, ..., an 和一个整数k。你被要求把这个数组分成k 个非空的子段。 然后从每个k 个子段拿出最小值,再从这些最小值中拿出最大值。求这个最大值最大能为多少?

 

Input

第一行输入两个整数 n 和 k (1 ≤ k ≤ n ≤  105) — 数组a 的大小和要求分成子段的数目

第二行包含 n 整数 a1,  a2,  ...,  an ( - 109  ≤  ai ≤  109).

Output

输出答案——在你分离的每个子段中最小值中的最大值k

Example

Input

5 2
1 2 3 4 5

Output

5

Input

5 1
-4 -5 -3 -2 -1

Output

-5

 

这个题是思维题,如果被分割的区间个数k==1,就直接输出最小值;如果被分割的个数k==2,输出首项a[1]和尾项a[n]的最大值。(注:这是一个WA点,假设区间是1 -2 7 9由于我们要使最大值最大,应该输出9,所以只能在n-1处划分。假设区间是1-2 9 7,此时无论怎样划分都不能将9弄出来,要使最大值最大,只能再次在n-1处划分)如果被分割的区间个数k>2,输出最大值。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<queue>
#include<stack>
#include<vector>
typedef long long ll;
using namespace std;
const int maxn = 250000;
const int INF = 1e9;
int n, k;
ll a[maxn];
ll mini=INF;
ll maxx=-INF;
int main()
{
	ios::sync_with_stdio(false);
	cin >> n >> k;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		if (a[i] > maxx)
			maxx = a[i];
		if (a[i] < mini)
			mini = a[i];
	}
	
	if (k == 1)
		cout << mini;
	if (k == 2)
		cout << max(a[1], a[n]);
	if (k > 2)
		cout << maxx;
	//system("pause");
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值