codeforces 651B Beautiful Paintings
来源:互联网 发布:qq炫舞mac版 编辑:程序博客网 时间:2024/05/16 08:03
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.
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.
Print one integer — the maximum possible number of neighbouring pairs, such thatai + 1 > ai, after the optimal rearrangement.
520 30 10 50 40
4
4200 100 100 200
2
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,根据这样的归纳可以证得结论的正确性。
- codeforces--651B Beautiful Paintings
- codeforces 651B Beautiful Paintings
- CodeForces 651B Beautiful Paintings
- codeforces 651B Beautiful Paintings
- CodeForces 651B Beautiful Paintings
- 【CodeForces】651B - Beautiful Paintings
- 【CodeForces】[651B]Beautiful Paintings
- CodeForces 651B Beautiful Paintings
- CodeForces 651B Beautiful Paintings
- Codeforces 651B Beautiful Paintings
- codeforces 651B Beautiful Paintings (想法)
- codeforces 651B Beautiful Paintings【优先队列】
- Codeforces 651B Beautiful Paintings【思维】
- codeforces 651B Beautiful Paintings(贪心)
- Codeforces 651B Beautiful Paintings【贪心】
- 【CodeForces 651B】Beautiful Paintings 排序+贪心
- 【codeforces 651B - Beautiful Paintings 】 greedy(贪心)
- codeforces 651B Beautiful Paintings (思路题)
- 传值方式
- Notification简述
- ssm 框架集成memcached 缓存服务器
- kindle电子书横竖屏切换方法
- $("#id :input")和$("#id input")的区别
- codeforces 651B Beautiful Paintings
- myeclipse创建web工程无响应
- 第7周 C语言程序设计(新2版) 练习1-14 打印输出各个字符出现频度的直方图(输出结果有问题)
- C++11正则表达式 ps:多线程
- Python threading operation
- Math类四个常用方法辨析,floor、ceil、round、rint
- spring中scope作用域
- viewpager 小技巧
- CSS3-贝塞尔曲线