C - Maximum of Maximums of Minimums

You are given an array a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get?

Definitions of subsegment and array splitting are given in notes.

Input

The first line contains two integers n and k (1 ≤ k ≤ n ≤  105) — the size of the array a and the number of subsegments you have to split the array to.

The second line contains n integers a1,  a2,  ...,  an ( - 109  ≤  ai ≤  109).

Output

Print single integer — the maximum possible integer you can get if you split the array into k non-empty subsegments and take maximum of minimums on the subsegments.

Example

Input
5 2
1 2 3 4 5
Output
5
Input
5 1
-4 -5 -3 -2 -1
Output
-5

Note

A subsegment [l,  r] (l ≤ r) of array a is the sequence al,  al + 1,  ...,  ar.

Splitting of array a of n elements into k subsegments [l1, r1], [l2, r2], ..., [lk, rk] (l1 = 1, rk = nli = ri - 1 + 1 for all i > 1) is k sequences (al1, ..., ar1), ..., (alk, ..., ark).

In the first example you should split the array into subsegments [1, 4] and [5, 5] that results in sequences (1, 2, 3, 4) and (5). The minimums are min(1, 2, 3, 4) = 1 and min(5) = 5. The resulting maximum is max(1, 5) = 5. It is obvious that you can't reach greater result.

In the second example the only option you have is to split the array into one subsegment [1, 5], that results in one sequence ( - 4,  - 5,  - 3,  - 2,  - 1). The only minimum is min( - 4,  - 5,  - 3,  - 2,  - 1) =  - 5. The resulting maximum is  - 5

水题,关键是搞清楚题意,还有当k==2时,为什么是max(a[0], a[n-1]),为什么a[0],和a[n-1]一定是序列里的最小值????

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream> using namespace std; int main()
{
int n,k;
int i,j;
int ans;
int mmax,mmin;
int a[];
mmax = -1e9-;
mmin = 1e9+;
scanf("%d %d",&n, &k);
for(i = ; i < n; i++)
{
scanf("%d",&a[i]);
}
if(k == )
{
for(i = ; i < n; i++)
{
mmin = min(a[i], mmin);
}
printf("%d\n",mmin);
}
else if(k == )
{
ans = max(a[], a[n-]);
printf("%d\n",ans);
}
else if(k >= )
{
for(i = ; i < n; i++)
{
mmax = max(a[i], mmax);
}
printf("%d\n",mmax);
}
return ;
}

C - Maximum of Maximums of Minimums(数学)的更多相关文章

  1. codeforces Round #440 B Maximum of Maximums of Minimums【思维/找规律】

    B. Maximum of Maximums of Minimums time limit per test 1 second memory limit per test 256 megabytes ...

  2. Codeforces 872B:Maximum of Maximums of Minimums(思维)

    B. Maximum of Maximums of Minimums You are given an array a1, a2, ..., an consisting of n integers, ...

  3. 【Codeforces Round #440 (Div. 2) B】Maximum of Maximums of Minimums

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] k=1的时候就是最小值, k=2的时候,暴力枚举分割点. k=3的时候,最大值肯定能被"独立出来",则直接输出最 ...

  4. Codeforces Round #440 (Div. 2)【A、B、C、E】

    Codeforces Round #440 (Div. 2) codeforces 870 A. Search for Pretty Integers(水题) 题意:给两个数组,求一个最小的数包含两个 ...

  5. Codeforces Contest 870 前三题KEY

    A. Search for Pretty Integers: 题目传送门 题目大意:给定N和M个数,从前一个数列和后一个数列中各取一个数,求最小值,相同算一位数. 一道水题,读入A.B数组后枚举i.j ...

  6. Codeforces Round #440 (Div. 2) A,B,C

    A. Search for Pretty Integers time limit per test 1 second memory limit per test 256 megabytes input ...

  7. Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2)

    A. Search for Pretty Integers 题目链接:http://codeforces.com/contest/872/problem/A 题目意思:题目很简单,找到一个数,组成这个 ...

  8. ACM-ICPC (10/15) Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2)

    A. Search for Pretty Integers You are given two lists of non-zero digits. Let's call an integer pret ...

  9. MySQL 5.6 Reference Manual-14.6 InnoDB Table Management

    14.6 InnoDB Table Management 14.6.1 Creating InnoDB Tables 14.6.2 Moving or Copying InnoDB Tables to ...

随机推荐

  1. Bug of VS2015+WDK

    1>  Signability test failed.1>  1>  Errors:1>  22.9.7: DriverVer set to incorrect date ( ...

  2. DDA算法

    [DDA算法] Digital Differential Analyzer,DDA算法是一种线段扫描转换算法.(线段光栅化算法) DDA算法优缺点: 1.消除了直线方程中的乘法计算,而在x.y方向使用 ...

  3. css实现栅格的方法

    1. 方法一 1.1. 效果 2. 方法二 2.1. 效果 3. 代码 3.1. Html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 T ...

  4. 前端 webpack

    前端 webpack http://www.cnblogs.com/lvdabao/

  5. len=in.read(b,0,len)和len=in.read(b)的区别

    byte[] byte = new byte[1024]; int len =0 ; while((len=in.read(b))!=-1){ out.write(b,0,len); } read函数 ...

  6. Windows服务器常用的性能计数器

    Windows常用性能计数器总结 基础监控: 1.SQL Server Buffer: Buffer Cache Hit Ratio 这是一个很重要查看内存是否不足的参数.SQL Server Buf ...

  7. EZOJ #258

    传送门 分析 我们考虑一个点有多少中情况可以被删除 我们发现只有删除它自己和删祖先共$dep_i$中 所以每个点的答案就是$\frac{1}{dep_i}$ 代码 #include<iostre ...

  8. Storm的StreamID使用样例(版本1.0.2)

    随手尝试了一下StreamID的的用法.留个笔记. ==数据样例== { "Address": "小桥镇小桥中学对面", "CityCode" ...

  9. bootstrap实现去点列表、内联列表、水平定义列表

    内联列表:通过添加类名“.list-inline”来实现内联列表,简单点说就是把垂直列表换成水平列表,而且去掉项目符号(编号),保持水平显示. 去点列表:通过给无序列表添加一个类名“.list-uns ...

  10. spring mvc 集成hibernate步骤

    今天从头把hibernate集成进入springMVC框架中,把过程记录下来. 1.首先要在监听器配置文件中加入hibernate支持,如下: <?xml version="1.0&q ...