B

题目链接: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;
}

 上面是本人ac的代码,然后刚刚看了一下别人的博客,发现总是会有大神有更快的算法

每次的答案就是:总个数-出现次数最多的数的个数

那么,为什么是这样呢?  本人比较菜,想了许久,看了别人的博客说法,看不懂,然后自己按着公式来推,发现是可以推出来的

比如样例是1 1 1  2 2 3 3 3 3 ,那么出现次数最多的就四次,也就是4个三,第二多的是三次,三个2,最少的四两次,两个2,排法如下:

最多的是3 3 3 3,插入第二多的就变成了1 3 1 3 1 3 3,然后最少的2,不管2是大于还是小于都可以插进去,只是插前面或者后面的区别罢了,这里是插在1的

后面变为1 2 3 1 2 3 1 3 3,这就是答案了。

看代码

#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,maxx=0,t;
    int a[1100];
    memset(a,0,sizeof(a));
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>t;
        a[t]++;
        maxx=max(maxx,a[t]);
    }
    cout<<n-maxx<<endl;
    return 0;
}
当初的梦想实现了吗,事到如今只好放弃吗~
原文地址:https://www.cnblogs.com/caijiaming/p/9340308.html