Maximum of Maximums of Minimums
Topic link: CodeForces - 872Bquestion meaning, given an array of length n, divide it into consecutive k subsets, what is the maximum and minimum value of the k subsets? (find the minimum value of each subset, and then find the maximum value among the k minimum values);
when k==1, only the original sequence, output the minimum value;
when k==2, output a[0] , the maximum value in a[n-1];
when k>2, the maximum value is output;
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <queue> #include <vector> using namespace std; const int maxn = 1e5+10; const int inf=0x3f3f3f3f; int a[maxn]; int main(){ int n, k, maxn = -inf, minn = inf; scanf("%d%d", &n, &k); for(int i=0; i<n; i++){ scanf("%d", &a[i]); maxn=max(maxn, a[i]); minn = min (minn, a [i]); } if(k==1) printf("%d\n", minn); else if(k==2) printf("%d\n", max(a[0], a[n-1])); else printf("%d\n", maxn); return 0; }