codeforces 651B Beautiful Paintings (想法)

_TCgogogo_發表於2016-03-08
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,根據這樣的歸納可以證得結論的正確性

相關文章