Codeforces Round #345 (Div. 2) B. Beautiful Paintings 暴力
B. Beautiful Paintings
题目连接:
http://www.codeforces.com/contest/651/problem/B
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
5
20 30 10 50 40
Sample Output
4
Hint
题意
给你n个数,让你重新安排顺序,使得a[i+1]>a[i]这种情况最多
题解:
显然就是每次抽出一个最长子序列,然后这样摆是最优的。
数据范围只有1000,那我们就暴力抽出最长上升子序列就好了。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1200;
int vis[maxn];
int a[maxn];
vector<int> tmp;
int main()
{
int n;scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+1+n);
int ans = 0;
for(int i=1;i<=n;i++)
{
if(vis[i])continue;
tmp.push_back(a[i]);
int now = a[i];
for(int j=i+1;j<=n;j++)
{
if(vis[j])continue;
if(now>=a[j])continue;
now=a[j];
vis[j]=1;
tmp.push_back(a[j]);
}
}
for(int i=0;i<tmp.size()-1;i++)
if(tmp[i+1]>tmp[i])ans++;
cout<<ans<<endl;
}