CodeForces - 870B - Maximum of Maximums of Minimums

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 = 1rk = 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.


题意我弄了好久才懂,因为不知道他的这个该怎么具体的分那几个小数组,有什么要求,看见下面的note.Google翻译了还是没怎么懂,就百度了这道题在别人那里得到了大概,,,大致就是给你一个序列,你可以分为几组,随意分吧,找出各组最小值,再从最小值中找出最大值,求最好情况时的这个最大值,第一个数肯定在第一组,最后一个数肯定在最后一组

既然都随意,那么最好情况的话,越大越好,所以其实序列能被分为1部分,2部分或   大于或等于3部分,当k=1是就是找序列中的最小值,k=2时,有两种情况一种是第一个数自己一组那就第一个数就是最小值中的最大值,第二种情况最后一个数一组,那么最小值中的最大值就可能是最后一个数,所以只要求两个端点处的较大的数,k>=3(可以把最大值自己分成一组)时就是求序列中的最大值

#include<stdio.h>
int main(void)
{
    long long int n,k,i,ans,min,max,x;
    scanf("%lld%lld\n",&n,&k);
    max=-1000000009;
    min=1000000009;

    if (k==1)
        {
            for(i=1;i<=n;i++)
              {
                scanf("%lld",&x);
                if (x<min) min=x;
              }
            ans=min;
        }

    else if (k>=3)
            {
              for(i=1;i<=n;i++)
                 {
                    scanf("%lld",&x);
                    if (x>max) max=x;
                 }
            ans=max;
        }

    else
        {
          scanf("%lld",&max);
          for (i=2;i<=n;i++)  scanf("%lld",&x);
          if (max>x) ans=max;
          else ans=x;
        }
    printf("%lld",ans);
    return 0;
 }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值