Codeforces1008 C Reorder the Array 贪心

https://cn.vjudge.net/contest/240521#problem/D

题意:一个数列,改变顺序,新的顺序中每有一个位置的新元素大于原来元素就加一分,求通过改变顺序能得到的最大分数。

题解:贪心,肯定是选取比该元素大的元素中最小的去放。本来是打算用并查集优化的,结果发现直接用一个指针指着就行了……

#include<iostream>
#include<cstdio>
#include<algorithm>
const int maxn = int(1e5) + 7;
int a[maxn], n, r = 1, ans;
int main() {
    std::cin >> n;
    for (int i = 1; i <= n; i++) std::cin >> a[i];
    std::sort(a + 1, a + 1 + n);
    for (int i = 1; i <= n; i++) {
        while (r <= n && a[r] <= a[i]) r++;
        if (r <= n) ans++, r++;
    }
    std::cout << ans << std::endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/nwpu2017300135/article/details/81175324