You are given an array of integers. Vasya can permute (change order) its integers. He wants to do it so that as many as possible integers will become on a place where a smaller integer used to stand. Help Vasya find the maximal number of such integers.

For instance, if we are given an array [10,20,30,40][10,20,30,40], we can permute it so that it becomes [20,40,10,30][20,40,10,30]. Then on the first and the second positions the integers became larger (20>1020>10, 40>2040>20) and did not on the third and the fourth, so for this permutation, the number that Vasya wants to maximize equals 22. Read the note for the first example, there is one more demonstrative test case.

Help Vasya to permute integers in such way that the number of positions in a new array, where integers are greater than in the original one, is maximal.

Input

The first line contains a single integer nn (1≤n≤1051≤n≤105) — the length of the array.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the elements of the array.

Output

Print a single integer — the maximal number of the array's elements which after a permutation will stand on the position where a smaller element stood in the initial array.

Examples

Input
7
10 1 1 1 5 5 3
Output
4
Input
5
1 1 1 1 1
Output
0

Note

In the first sample, one of the best permutations is [1,5,5,3,10,1,1][1,5,5,3,10,1,1]. On the positions from second to fifth the elements became larger, so the answer for this permutation is 4.

In the second sample, there is no way to increase any element with a permutation, so the answer is 0.

给你一段序列,经过重新排序后,问最多有几个位置上的数比原位置上的数大了。

类似与田忌赛马,但是我并不会操作。

原理是这样的:

  假设给定的序列是  1 1 2 3  5 5 5 7

  那么,符合题意的方法是,抽出两个子序列:1 2 3 5 7 和  1 5 ;或者是  1 3 5 7 和 1 2 5 。

  即找长串,找若干条,直到所给序列中的数都被选过,或无法再选。

方法有了,但是暴力实现的话会超时或者RT,不知道怎么实现,直到发现了网友们的一个极其巧妙的解法,但是那种算法的原理不太理解。。。

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0);
const double e=exp();
const int N = ; int con[];
int num[];
bool cmp(int a,int b)
{
return a<b;
}
int main()
{
int n,i,p,j;
int flag,ans=;
scanf("%d",&n);
for(i=;i<=n;i++)
{
scanf("%d",&con[i]);
}
sort(con+,con+n+,cmp);
int head=;
for(i=;i<=n;i++) //巧妙的方法开始了
{
if(con[i]>con[head])
{
head++;
ans++;
}
}
printf("%d\n",ans);
return ;
}

CF 1008C Reorder the Array的更多相关文章

  1. A. Reorder the Array

    You are given an array of integers. Vasya can permute (change order) its integers. He wants to do it ...

  2. CF1007A Reorder the Array 题解

    To CF 这道题是排序贪心,将原序列排序后统计答案即可. 但是直接统计会超时,因为排序后具有单调性,所以可以进行一点优化,这样,便可以通过此题. 而这道题的优化在于单调性,因为 \(a[i+1]\) ...

  3. CF 295A Greg and Array (两次建树,区间更新,单点查询)

    #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm& ...

  4. cf D. Levko and Array

    http://codeforces.com/contest/361/problem/D 用二分搜索相邻两个数的差的绝对值,然后用dp记录数改变的次数.dp[i]表示在i之前改变的次数,如果|a[i]- ...

  5. cf C. Levko and Array Recovery

    http://codeforces.com/contest/361/problem/C 这道题倒着一次,然后正着一次,在正着的一次的时候判断合不合法就可以. #include <cstdio&g ...

  6. [CF 295A]Grag and Array[差分数列]

    题意: 有数列a[ ]; 操作op[ ] = { l, r, d }; 询问q[ ] = { x, y }; 操作表示对a的[ l, r ] 区间上每个数增加d; 询问表示执行[ x, y ]之间的o ...

  7. Codeforces Round #497 (Div. 2) C. Reorder the Array

    Bryce1010模板 http://codeforces.com/contest/1008/problems #include <bits/stdc++.h> using namespa ...

  8. CodeForces-1007A Reorder the Array 贪心 田忌赛马

    题目链接:https://cn.vjudge.net/problem/CodeForces-1007A 题意 给个数组,元素的位置可以任意调换 问调换后的元素比此位置上的原元素大的元素个数最大多少 思 ...

  9. [CF 718C] Sasha and Array

    传送门 Solution 用线段树维护矩阵 第一个操作相当于区间乘 第二个操作相当于区间求和 Code  #include<bits/stdc++.h> #define ll long l ...

随机推荐

  1. dstat 监控时,无颜色显示

    linux:Centos 6.6 dstat:0.7.0 注意,有这个提醒:Color support is disabled, python-curses is not installed good ...

  2. 数据库事务的四大特性以及4种事务的隔离级别-以及对应的5种JDBC事务隔离级别

    本篇讲诉数据库中事务的四大特性(ACID),并且将会详细地说明事务的隔离级别. 如果一个数据库声称支持事务的操作,那么该数据库必须要具备以下四个特性: ⑴ 原子性(Atomicity) 原子性是指事务 ...

  3. Java并发编程中的设计模式解析(一)

    Java并发编程,除了被用于各种Web应用.分布式系统和大数据系统,构成高并发系统的核心基础外,其本身也蕴含着大量的设计模式思想在里面.这一系列文章主要是结合Java源码,对并发编程中使用到的.实现的 ...

  4. java 表单验证

    java 表单验证 1.思路:通过表单选择器,表单属性过滤器提取每个表单提交的值,进行验证 2.实现:javascript通过 onSubmit()事件,判断,返回值false不提交,返回true提交 ...

  5. 使用jQuery在javascript中自定义事件

    js中的自定义事件有attachEvent,addEventListener等等好多种,往往受困于浏览器兼容,而且代码写起来也相当麻烦.jQuery为我们解决了这个问题,几行代码就可以很好的实现事件的 ...

  6. [NOIP2011]玛雅游戏

    闲的没事干,出来写一下早两天刷的一道搜索题NOIP2011玛雅游戏,其实这道题还是比较水的,虽然看起来可能有点复杂. 方法很简单粗暴,直接根据规则模拟就行. 话不多说直接上代码(关键操作在注释中有提到 ...

  7. Ants on tree

    Description 从前有一个策略游戏, 叫做 蚂蚁上树 游戏中有一棵 nn 个节点, 以 11 为根的有根树 初始始每个节点都为空, 游戏系统会进行两种操作 : 1 x , 表示往 xx 节点放 ...

  8. IO编程(1)-文件读写

    文件读写 读写文件是最常见的IO操作.Python内置了读写文件的函数,用法和C是兼容的. 读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接 ...

  9. Android 程序架构: MVC、MVP、MVVM、Unidirectional、Clean...

    摘选自:GUI 应用程序架构的十年变迁:MVC.MVP.MVVM.Unidirectional.Cleanhttps://zhuanlan.zhihu.com/p/26799645 MV* in An ...

  10. 解题:ZJOI 2015 幻想乡战略游戏

    题面 神**所有点都爆int,我还以为我写出什么大锅了,不开long long见祖宗... 动态点分治利用点分树树高不超过log的性质,我们对每个点维护一个子树和,一个点分树子树和,一个点分树上父亲的 ...