Codeforces 872 B Maximum of Maximums of Minimums

题目地址
题意:给你一个序列,然后让你把这个序列正好分割为k个子序列,每个子序列都选出一个最小值,然后求这些最小值的最大值为多少。
思路:这题就是考细节的,我在比赛中被hack了两次,然后最后一次hack还是在比赛结束以后的重测中。言归正传,我们可以发现存在以下的三种情况:

  1. m==1,这时候因为只能选1个子序列就是原序列了,所以就是输出最小值了。
  2. m==2,这时候我们可以发现要分割为两个集合,所以我们直接判断左右就好了,num[0]和num[n-1]较大的一个。
  3. m>=3,这时候我们发现我们就可以不管最大值的位置划分出只有最大值的子序列,所以就是直接输出max。

还是那句话,细心就是王道,我之前就是忘记了m==2这种情况了。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define N 400010
#define LL __int64
#define inf 0x3f3f3f3f 0
#define lson l,mid,ans<<1
#define rson mid+1,r,ans<<1|1
#define getMid (l+r)>>1
#define movel ans<<1
#define mover ans<<1|1
const int mod = 1000000007;
using namespace std;
int num[N];
int main() {
    cin.sync_with_stdio(false);
    int n, m;
    int a, b;
    while (cin >> n >> m) {
        a = 0;
        b = 0;
        for (int i = 0; i < n; i++) {
            cin >> num[i];
            if (num[a] > num[i]) {
                a = i;
            }
            if (num[b] < num[i]) {
                b = i;
            }
        }
        if (m == 1) {
            cout << num[a] << endl;
        }
        else if (m == 2) {
                cout << max(num[0], num[n - 1]) << endl;
        }
        else {
            cout << num[b] << endl;
        }
    }
    return 0;
}

你可能感兴趣的:(codeforces,思维,ACM(已完结))