Codeforces Round #440 (Div. 2)B. Maximum of Maximums of Minimums
2017-10-16 07:20
381 查看
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
Output
Input
Output
Note
A subsegment [l, r] (l ≤ r) of array
a is the sequence al, al + 1, ..., ar.
题目意思:
给你两个数字n和k 把这n个数字分成k段 让这k段中的k个最小值中的最大值最大
思路:
当k==1的时候我们直接输出最小值就好了
当k>2的时候我们可以单独把最大值隔开 然后能找到的最大值就一定是原序列的最大值本身
当k==2的时候 我用了一个前缀最小值数组和一个后缀最小值数组来维护前n项中的最小值和后n项中的最小值
本来这个操作需要一个for来遍历的 但是后来发现这个array[0] and array[n - 1] 一定是前缀最小值数组和后缀最小值数组中最大的值
所以只需要比较这两个数字的大小输出大的就好~~
比赛的时候我这道题莫名TL4次 就一个for循环 我至今不知道为什么~~~
#include <cstdio>
#include <cstring>
#include <algorithm>
const int inf = 0x7fffffff;
const int N = 100005;
using namespace std;
int array
;
int main()
{
int n , k;
scanf("%d%d",&n,&k);
int maxx = -inf;
int minn = inf;
int max_id = 0;
for (int i = 0 ; i < n ; i++) {
scanf("%d",&array[i]);
if(array[i] > maxx) {
maxx = array[i];
max_id = i;
}
minn = min(minn , array[i]);
}
if(k == 1) {
printf("%d\n",minn);
} else if (k > 2) {
printf("%d\n",maxx);
}
else {
printf("%d\n",max(array[n-1],array[0]));
}
}
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.
题目意思:
给你两个数字n和k 把这n个数字分成k段 让这k段中的k个最小值中的最大值最大
思路:
当k==1的时候我们直接输出最小值就好了
当k>2的时候我们可以单独把最大值隔开 然后能找到的最大值就一定是原序列的最大值本身
当k==2的时候 我用了一个前缀最小值数组和一个后缀最小值数组来维护前n项中的最小值和后n项中的最小值
本来这个操作需要一个for来遍历的 但是后来发现这个array[0] and array[n - 1] 一定是前缀最小值数组和后缀最小值数组中最大的值
所以只需要比较这两个数字的大小输出大的就好~~
比赛的时候我这道题莫名TL4次 就一个for循环 我至今不知道为什么~~~
#include <cstdio>
#include <cstring>
#include <algorithm>
const int inf = 0x7fffffff;
const int N = 100005;
using namespace std;
int array
;
int main()
{
int n , k;
scanf("%d%d",&n,&k);
int maxx = -inf;
int minn = inf;
int max_id = 0;
for (int i = 0 ; i < n ; i++) {
scanf("%d",&array[i]);
if(array[i] > maxx) {
maxx = array[i];
max_id = i;
}
minn = min(minn , array[i]);
}
if(k == 1) {
printf("%d\n",minn);
} else if (k > 2) {
printf("%d\n",maxx);
}
else {
printf("%d\n",max(array[n-1],array[0]));
}
}
相关文章推荐
- Codeforces Round #440 (Div. 2) B. Maximum of Maximums of Minimums
- 【Codeforces Round #440 (Div. 2) B】Maximum of Maximums of Minimums
- CF 440 div2 B.Maximum of Maximums of Minimums
- Maximum of Maximums of Minimums
- 【Codeforces Round #440 (Div. 2) B】Maximum of Maximums of Minimums
- Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2)B. Maximum of Maximums of
- codeforce Maximum of Maximums of Minimums(区间划分 求区间最小值最大值问题)
- LeetCode Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- LeetCode---Maximum Depth of Binary Tree
- Length of LOB data (78862) to be replicated exceeds configured maximum 65536
- Codeforces Round #428 (Div. 2) E - Mother of Dragons
- 79:Maximum Depth of Binary Tree
- LeetCode | Maximum Depth of Binary Tree
- tomcat consider increasing the maximum size of the cache java.lang.IncompatibleClassChangeError: Imp
- Maximum Depth of Binary Tree--二叉树的深度
- BW:Maximum number of selections exceeded for InfoPackage ZPAK_4JXT7KVKHH6NMZRR64D4GHUDO
- [LeetCode] Maximum Depth of Binary Tree
- Codeforces Round #258 (Div. 2) C. Predict Outcome of the Game
- [LeetCode] Maximum Depth of Binary Tree