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
Output
Input
Output
代码:
思路:注意每个人只能接受别人的画一次。先用快排,然后用冒泡排序算出有多少人可以幸福,既a[j]>a[i].注意这里需要用一个函数记录每个人是否只接受了一次画。可以用结构体记录。
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].注意这里需要用一个函数记录每个人是否只接受了一次画。可以用结构体记录。
相关文章推荐
- A folder failed to be renamed or moved
- Block属性声明strong和retain竟然不一样
- int main(int argc,char* argv[])详解
- leetcode.336. Palindrome Pairs
- ORA-00054:资源正忙,但指定以 NOWAIT 方式获取资源,或者超时失效
- innodb Lock wait timeout exceeded;
- document.domain 跨域实现
- [书目20160526]Brain Rules 让大脑自由:释放天赋的12条定律
- gmail smtp 535 5.7.8无法登陆解决
- 13周练 D - Beautiful Paintings 【升数列】
- 游戏AI--决策(1)
- 机器人与人类“同床共枕“科技能让人类提升安全感?
- Sorting Railway Cars
- [leetcode] 172. Factorial Trailing Zeroes 解题报告
- 219. Contains Duplicate II
- Training Set, Validation Set 和Testing Set之间的差别及关系
- linux 系统监控、诊断工具之 IO wait
- on io scheduling again
- Codeforces Round #353 (Div. 2) B.Restoring Painting
- 服务器磁盘raid0,raid1,raid10,raid5,raid50,raid6,raid60 分析与案例