codeforces 651B Beautiful Paintings (想法)

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
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.


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

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

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

#include <cstdio>
#include <algorithm>
using namespace std;
int cnt[1005], n, tmp, ma;

int main()
{
	scanf("%d", &n);
	for(int i = 0; i < n; i++)
	{
		scanf("%d", &tmp);
		ma = max(ma, ++ cnt[tmp]);
	}
	printf("%d\n", n - ma);
}

证明:
首先根据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
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值