codeforces 651B Beautiful Paintings

来源:互联网 发布:qq炫舞mac版 编辑:程序博客网 时间:2024/05/16 08:03
B. Beautiful Paintings
time limit per test: 1 second
memory limit per test: 256 megabytes

There are n pictures delivered for the new exhibition. Thei-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 ofa in any order. What is the maximum possible number of indicesi(1 ≤ i ≤ n - 1), such thatai + 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), whereai means the beauty of thei-th painting.

Output

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

Examples
Input
520 30 10 50 40
Output
4
Input
4200 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.

题目链接:http://codeforces.com/problemset/problem/651/B

题目大意:可重排原数组,使得ai+1>ai的组数最大,求最大组数

题目分析:数据很小,无脑n^2模拟即可,记个O(n)做法,n减去出现次数最多的数的出现次数就是答案

法一:

</pre><pre name="code" class="cpp">#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>using namespace std;const int maxn=1005;int cnt[maxn],maxz;int main(){    int n,a;    scanf("%d", &n);    for(int i = 0; i < n; i++)    {        cin>>a;        maxz = max(maxz, ++ cnt[a]);    }    printf("%d\n", n - maxz);}
证明:
首先根据n^2的思路,我们用出现次数最多的数字来生成这个数列,即用它尽量构造长度越长的连续子数列,这样一定是最优的,因为这样一个数字若在中间可以同时影响其前后两个数字,举个例子: 234这三个数字可以得到2个ai<ai+1的组数,而若不连续,2356,需要4个数才可以得到。根据这个构造思路我们还可以得到最后如果有剩下的数字那必然是出现次数最多的那个,假设现在出现次数最多的数字为a,a出现了cnt次,满足ai<ai+1的组数为ans=n-cnt。现在再加一个数字b进去,若a==b则按照最优构造思路将其插到连续的a里,这样组数不变相当于ans = (n + 1) - (cnt + 1) = n - cnt,若a!=b则必然可以拿出来一个a和b组成一组,即(n + 1) - cnt = n - cnt + 1 = ans + 1,根据这样的归纳可以证得结论的正确性。


0 0
原创粉丝点击