CodeForces - 1007A (思惟+雙指針)

題意

https://vjudge.net/problem/CodeForces-1007Aios

對一個序列重排,使得新的數比原來的數大對應的位置個數最多。c++

思路

 舉個栗子,好比1 2 2 3 3 3 3 4 5,那麼對於一個數,確定是用比他大的最小的去覆蓋他,這樣後面較大的數就有更多的機會被覆蓋了。好比用2覆蓋1,但不能用5覆蓋1,由於這樣的話4就不會被覆蓋了。再考慮亂序的,好比 2 1 3 5 3 3 4 3 2,同樣的,用2覆蓋1,用3覆蓋2,和有序的序列其實並沒有區別,因此咱們直接拍個序,用雙指針遍歷便可。spa

代碼

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int N=200005;
const int mod=1e9+7;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
int a[N];
int main()
{
    std::ios::sync_with_stdio(false);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    sort(a+1,a+1+n);
    int i=1,j=1;
    for(;i<=n;i++)
    {
        if(a[i]>a[j])
        {
            j++;
        }
    }
    cout<<j-1<<endl;
    return 0;
}
相關文章
相關標籤/搜索