您的位置:首页 > 大数据 > 人工智能

CodeForces651BBeautiful Paintings

2016-05-26 15:32 429 查看
Description

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.

Sample Input

Input
5
20 30 10 50 40


Output
4


Input
4200 100 100 200


Output
2


代码:

#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
using namespace std;
struct st{
int x;
int s;
}a[1001];
bool cmp(st a ,st b)
{
return a.s<b.s;
}
int main()
{
int i,n;
while(scanf("%d",&n)!=EOF)
{
memset(a,0,sizeof(a));
for(i=1;i<=n;i++)
scanf("%d",&a[i].s);
sort(a+1,a+n+1,cmp);
int sum=0;
for(i=1;i<n;i++)
for(int j=i+1;j<=n;j++)
{
if(a[i].s<a[j].s&&a[j].x==0)
{
a[j].x=1;
sum++;
break;
}
}
printf("%d\n",sum);
}
return 0;
}
题意:每个人手里都有一副有价值的画,每个人都可以把他的画给另一个人,如果另一个人画价值比自己的高,那么他就是幸福的。问最多有多少人是幸福的?

思路:注意每个人只能接受别人的画一次。先用快排,然后用冒泡排序算出有多少人可以幸福,既a[j]>a[i].注意这里需要用一个函数记录每个人是否只接受了一次画。可以用结构体记录。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: