D2

看起来比较麻烦,但实际上,就是排序,然后分成大数组和小数组,然后交叉一下就好了.....这样我们可以最大化组合数为 ( n − 1 ) / 2

 就是说会有相等的...加个特判就好(还是把小的插大的里,就是如果有相等的就不能加ans的数)

#include <iostream>
#include <stack>
#include <vector>
#include <math.h>
#include <string>
#include <algorithm>
#include <unordered_map>
#include <map>
#include<set>
#include <cstring>
using namespace std;
const int maxn=1e5+3;
int main() {
    int n;
    int a[maxn];
    int b[maxn];
    int ans = 0;
    vector<int> v;

    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    sort(a + 1, a + n + 1);
//    for (int j = 1,k = 1; k<=((n%2)?n+1:n) ; ++j,k+=2) {
//        b[k]=a[j+n/2];//第一位是个大的
//        b[k+1]=a[j];
//    }
    if (n % 2) {
        for (int i = 1, j = i + n / 2; i <= n / 2; i++, j++) {
            v.push_back(a[j]);
            v.push_back(a[i]);
        }
        v.push_back(a[n]);
    } else {
        for (int i = 1, j = i + n / 2; i <= n / 2; i++, j++) {
            v.push_back(a[j]);
            v.push_back(a[i]);
        }

    }


    for (int l = 1; l < n; ++l) {
        if (v[l - 1] > v[l] && v[l + 1] > v[l])
            ans++;
    }
    cout << ans << endl;
    for (vector<int>::iterator k = v.begin(); k != v.end(); ++k) {
        cout << *k;
        if (k != v.end()-1)
            cout << " ";
    }
    return 0;
}
为了自己,和那些爱你的人
原文地址:https://www.cnblogs.com/zhmlzhml/p/13726147.html