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. 关于 jdbc 的错误

  2. js产生对象的3种基本方式(工厂模式,构造函数模式,原型模式)

    1.工厂模式 function a(name){ var b = new object(); b.name = name; b.say = function(){ alert(this.name); ...

  3. apt 查询软件

    apt-cache search percona-server apt list percona-server-server-5.6

  4. EMC校招笔试题目

    ------------------------------------------------- 1,7×(1/7) = 1是什么率? 乘法运算满足结合律,交换律和分配率.这个题目用的应该是交换律. ...

  5. cdoj31-饭卡(card) (01背包)

    http://acm.uestc.edu.cn/#/problem/show/31 饭卡(card) Time Limit: 3000/1000MS (Java/Others)     Memory ...

  6. Spark分布式计算执行模型

    引言 相对Hadoop, Spark在处理需要迭代运算的机器学习训练等任务上有着很大性能提升,同时提供了批处理.实时数据处理.机器学习以及图算法等一站式的服务,因此最近大家一起来学习Spark,特别是 ...

  7. centOS系统安装MySQL教程

    如何卸载CentOS系统自带MySQL 1.1. 查找以前是否装有MySQL 命令:rpm -qa|grep -i mysql 可以看到如下图的所示:(图片来自互联网,仅做参考使用) 说明系统自带: ...

  8. Codeforces 76D 位运算

    题意:给你两个数x 和 y, x = a + b, y = a XOR b,问有没有合法的a和b满足这个等式? 思路:有恒等式: a + b = ((a & b) << 1) + ...

  9. SpringBoot28 RabbitMQ知识点、Docker下载RabbitMQ、SpringBoot整合RabbtiMQ

    1 RabbitMQ知识点 1.1 整体架构图 消息生产者将消息投递到exchange中,exchange会以某种路由机制将生产者投递的消息路由到queue中,消息消费者再从queue中获取消息进行消 ...

  10. AJAX(XMLHttpRequest)进行跨域请求方法详解

    AJAX(XMLHttpRequest)进行跨域请求方法详解(三) 2010年01月11日 08:48:00 阅读数:24213 注意:以下代码请在Firefox 3.5.Chrome 3.0.Saf ...