B - Beautiful Paintings

题目链接:https://vjudge.net/contest/237394#problem/B

There are n pictures delivered for the new exhibition. The i-th painting has beauty ai. We know that a visitor becomes happy every time he passes from a painting to a more beautiful one.

We are allowed to arranged pictures in any order. What is the maximum possible number of times the visitor may become happy while passing all pictures from first to last? In other words, we are allowed to rearrange elements of a in any order. What is the maximum possible number of indices i (1 ≤ i ≤ n - 1), such that ai + 1 > ai.

Input

The first line of the input contains integer n (1 ≤ n ≤ 1000) — the number of painting.

The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where ai means the beauty of the i-th painting.

Output

Print one integer — the maximum possible number of neighbouring pairs, such that ai + 1 > ai, after the optimal rearrangement.

Examples
Input
5
20 30 10 50 40
Output
4
Input
4
200 100 100 200
Output
2
Note

In the first sample, the optimal order is: 10, 20, 30, 40, 50.

In the second sample, the optimal order is: 100, 200, 100, 200.

题目大意:输入n,有n个数,你可以任意排序,要求a[i+1]>a[i]的数对最多

个人思路:额。。。这题wa了三次,一直在想思路哪里有问题,其实是有问题的,后来改了思路,正确的思路是:

先把数从小到大排序,找出按完全递增顺序排列的一组数,然后排除这些数,剩余的数又按完全递增的顺序排列····,一直循环此过程,直到结束

看代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
const int maxn=1e5+10;
const ll maxa=32050;
const double x=(1.0+sqrt(5.0))/2;
#define INF 0x3f3f3f3f3f3f
int main()
{
    int n,ans=0,sum=0;
    int a[1100];
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);
    for(int i=1;i<n;i++)
    {
        if(a[i]>a[i-1])
        {
            sum++;
        }
        else
        {
            int t=upper_bound(a+i,a+n,a[i-1])-a;
           // cout<<t<[[<' ';
            if(t!=n)
            {
                swap(a[i],a[t]);
                sum++;
            }
            else
            {
                ans+=sum;
                sum=0;
                sort(a+i,a+n);
              //  for(int j=i;j<n;j++)
                //    cout<<a[j]<<' ';
                //cout<<ans<<endl;
            }
        }
    }
    ans+=sum;
    cout<<ans<<endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/caijiaming/p/9340308.html