Codeforces Round #440 (Div. 2)B. Maximum of Maximums of Minimums
来源:互联网 发布:为什么要学c语言 编辑:程序博客网 时间:2024/06/08 13:52
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 thek 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 andk (1 ≤ k ≤ n ≤ 105) — the size of the arraya 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 intok non-empty subsegments and take maximum of minimums on the subsegments.
5 21 2 3 4 5
5
5 1-4 -5 -3 -2 -1
-5
A subsegment [l, r] (l ≤ r) of arraya 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[N];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
- Codeforces Round #440 (Div. 2) B. Maximum of Maximums of Minimums
- Codeforces Round #440 div2 B Maximum of Maximums of Minimums
- Codeforces Round #440 B. Maximum of Maximums of Minimums
- Codeforces Round #440 B. Maximum of Maximums of Minimums-【思维】
- #440 Div.2 B. Maximum of Maximums of Minimums 水
- B. Maximum of Maximums of Minimums --codeforces
- Codeforces 872 B Maximum of Maximums of Minimums
- CodeForces 872B Maximum of Maximums of Minimums
- CF 440 div2 B.Maximum of Maximums of Minimums
- Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2)B. Maximum of Maximums of
- Maximum of Maximums of Minimums
- codeforce Maximum of Maximums of Minimums(区间划分 求区间最小值最大值问题)
- Codeforces Round #188 (Div. 2) B. Strings of Power
- Codeforces Round #310 (Div. 2) B. Case of Fake Numbers
- Codeforces Round #342 (Div. 2)--B. War of the Corporations
- Codeforces Round #342 (Div. 2)-B. War of the Corporations
- 【Leetcode】【python】Simplify Path
- windows核心编程---用内核对象进行线程同步
- Jquery表单验证
- AngularJs用户管理
- Xcode9学习笔记5
- Codeforces Round #440 (Div. 2)B. Maximum of Maximums of Minimums
- 073 定积分基本公式牛顿-莱布尼茨公式证明
- Codeforces Round #440 (Div. 1) A. Maximum splitting
- Cocos 中的自动绑定
- 函数一致性导数的定义
- Matlab中image、imagesc和imshow函数用法解析
- Cocos 中的脚本绑定
- 洛谷 P1083 借教室
- 读Zepto源码之Stack模块