codeforces 651B Beautiful Paintings (想法)
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.
5
20 30 10 50 40
4
4
200 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減去出現次數最多的數的出現次數就是答案
#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,根據這樣的歸納可以證得結論的正確性
相關文章
- 【數位dp】Beautiful numbers CodeForces - 55D
- Codeforces Round #231 (Div. 2) B Very Beautiful Number(數字遞推)
- Beautiful Soup學習
- BUUCTF:Beautiful_SideIDE
- Beautiful Soup 學習手冊
- Python Beautiful Soup簡介Python
- 增加想法
- xpath beautiful pyquery三種解析庫
- 淺析Beautiful Soup庫和Lxml庫XML
- [題解]CF55D Beautiful Numbers
- CF Div. 3 C Beautiful Triple PairsAI
- 初學者的想法
- Beautiful Soup庫的使用(學習筆記)筆記
- You are beautiful voted the 'Most Irritating Song Ever'.
- vue開發小想法Vue
- 使用 Beautiful Soup 在 Python 中抓取網頁Python網頁
- Python Beautiful Soup+requests實現爬蟲Python爬蟲
- Python網頁抓取工具Beautiful Soup面面觀!Python網頁
- Beautiful Soup在爬蟲中的基本使用語法爬蟲
- Python爬蟲學習(11):Beautiful Soup的使用Python爬蟲
- 題解:CF1264D Beautiful Bracket SequenceRacket
- 一點 Vue.observable 想法Vue
- 小想法有大智慧
- IT人你的想法呢?薦
- 關於WebWind一點想法Web
- Codeforces
- 關於Java的一點想法Java
- 圖靈改版之我的想法圖靈
- 關於美食網站的想法!網站
- Router-view 背後的想法View
- 迴歸の一些想法
- python爬蟲之Beautiful Soup基礎知識+例項Python爬蟲
- 一起學爬蟲——使用Beautiful Soup爬取網頁爬蟲網頁
- JB的Python之旅-爬蟲篇--urllib和Beautiful SoupPython爬蟲
- [LeetCode] 2070. Most Beautiful Item for Each QueryLeetCode
- codeforces Mafia
- codeforces Winner
- Laravel的mysql資料分離想法LaravelMySql