B. Maximum of Maximums of Minimums
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples
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 ksequences (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个区间里的最小值。

【分析】:观察,发现最大化的值和k有关。

【代码】:

#include<bits/stdc++.h>

using namespace std;

int n,k;
const int maxn = 1e5+;
int a[maxn];
#define inf 0x3f3f3f3f
int main()
{
int mm,ma;
memset(a,,sizeof(a));
while(cin>>n>>k)
{
mm=inf;
ma=-inf;
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
mm=min(mm,a[i]);
ma=max(ma,a[i]);
}
if(k==)
{
cout<<mm<<endl;
return ;
}
if(k>)//分割大于2时,最大化策略为总把最大值划为单独,就可以最大化结果刚好为最大值
{
cout<<ma<<endl;
return ;
}
if(k==)//分割为2个区间时,想要最大化,策略1 5 9 7 2/9 5 1 7 8可以试试,无论在哪里隔板,肯定符合两端的最大值。
{
cout<<max(a[],a[n-])<<endl;
}
}
return ;
}

codeforces Round #440 B Maximum of Maximums of Minimums【思维/找规律】的更多相关文章

  1. codeforces Round #440 C Maximum splitting【数学/素数与合数/思维/贪心】

    C. Maximum splitting time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  2. Codeforces Round #260 (Div. 2) A , B , C 标记,找规律 , dp

    A. Laptops time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  3. Codeforces Round #493 (Div. 1) B. Roman Digits 打表找规律

    题意: 我们在研究罗马数字.罗马数字只有4个字符,I,V,X,L分别代表1,5,10,100.一个罗马数字的值为该数字包含的字符代表数字的和,而与字符的顺序无关.例如XXXV=35,IXI=12. 现 ...

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

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

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

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

  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. 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 ...

  8. 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, ...

  9. Codeforces Round #365 (Div. 2) C - Chris and Road 二分找切点

    // Codeforces Round #365 (Div. 2) // C - Chris and Road 二分找切点 // 题意:给你一个凸边行,凸边行有个初始的速度往左走,人有最大速度,可以停 ...

随机推荐

  1. Hibernate配置文件说明

    <property name="hbm2ddl.auto">create</property> create - 启动Hibernate前先删除表,重新创建 ...

  2. 当xml结构很深时候 可以通过父节点删除子元素

    当xml结构很深时候 可以通过父节点删除子元素

  3. [NOIP2017]列队 线段树

    ---题面--- 题解: 之前写的splay,,,然而一直没调出来,我感觉是某个细节想错了,,然而已经重构4次代码不想再写splay了.于是今天尝试了线段树的解法. 首先因为每次出列之后的变化都是将当 ...

  4. 【NOIP模拟赛】beautiful 乱搞(平衡树)+ST

    biubiu~~~ 我用平衡树处理的这道题,然而这种方法还是要看评测姬..... 正解是乱搞....就是枚举每一位数作为中位数,比他小的看做-1比他大的看做1,那么我们从一开始就有了一个绵延的山,我们 ...

  5. Maven如何打包本地依赖包

    有的jar包,在maven中心库里面是没有的,那么,如何在项目中使用呢? 假设我们需要使用:apache-ant-zip-2.3.jar 将该jar包,放在项目的lib目录,例如: 在pom.xml里 ...

  6. Codeforces Round #524 (Div. 2) C. Masha and two friends

    C. Masha and two friends 题目链接:https://codeforc.es/contest/1080/problem/C 题意: 给出一个黑白相间的n*m的矩阵,现在先对一个子 ...

  7. inflate

    LayoutInflater是用 来找res/layout/下的xml布局文件,并且实例化 https://www.cnblogs.com/savagemorgan/p/3865831.html

  8. ansible 部署jdk

    playbook 剧本如下 [root@sz_fy_virt_encrypt_33_239 x]# cat jdk.yml - hosts: web remote_user: opsadmin bec ...

  9. 更改win10和mint双系统默认启动顺序

    更改win7 & Linuxmint双系统安装后更改默认启动顺序 1.打开一个term,编辑/etc/default/grub,即sudo nano /etc/default/grub,把se ...

  10. 百度vue服务端渲染(ssr)有感

    前端各种框架工具层次不穷,日新月异,越学越混乱了快 知乎上看到了一段回复,豁然开朗的感觉. Web 2.0时代最大的思想革命本质不是前后端分离,而是把网页当作独立的应用程序(app).前后端分离只是实 ...