Codeforces Round #497 (Div. 2) C - Reorder the Array(贪心)

原题链接:http://codeforces.com/contest/1008/problem/C

题意: 题意是给了n个数,可以任意去排序,排序后的这个位置上的数要大于排序前的这个位置上的数,问最多有多少个这样的数。

我们可以让最大数挪到第二大的位置上,让第二大的数挪到第三大的位置上……以次往后退就可以求出最多有几个这样的数。


#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 1e5+10;
int a[N],b[N];

int main() {
    int n, sum = 0;
    int i,j = 0;
    scanf("%d",&n);
    for(i=0; i<n; i++) {
        scanf("%d",&a[i]);
    }
    sort(a,a+n);
    for(i=n-1; i>=0; i--,j++) {
        b[j] = a[i];
    }
    j = 0;
    for(int i=n-1; i>=0; i--) {
        if(a[i] < b[j]) {
            sum ++;
            j ++;
        }
    }

    printf("%d\n",sum);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_16554583/article/details/81043983