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.
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).
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.
5 2 1 2 3 4 5
5
5 1 -4 -5 -3 -2 -1
-5
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 = n, li = 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;
}