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. jQ 小球碰撞检测

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. NLP & 中文分词

    NLP & 中文分词 中文分词 (Word Segmentation, WS) 指的是将汉字序列切分成词序列. 中文自然语言处理系统 https://www.ltp-cloud.com/int ...

  3. 安装Zookeeper出现Unable to start AdminServer,existing abnormally问题解决方法

    问题如下: 出现这个问题主要是由于8080端口占用,可在zoo.cfg中增加admin.serverPort=没有被占用的端口号解决问题.

  4. Cannot create file"C:\Users\LML\AppData\Local\Temp\EditorLineEnds.ttr"。另一个程序正在使用此文件,进程无法访问。

    不能二次启动,每次开机第一次都ok,出于习惯,总是想试试第二次打开软件是否正常,结果不出所料,出现了“Cannot create file"C:\Users\LML\AppData\Loca ...

  5. bzoj1036 [ZJOI2008]树的统计Count(树链剖分)

    Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. Q ...

  6. tensorflow环境下安装scikit-learn

    1. scikit-learn所依赖的环境: python(>=2.6 or >=3.3) numpy(>=1.6.1) scipy(>=0.9) 可用conda list 查 ...

  7. P2303 [SDOi2012]Longge的问题

    题目描述 Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在问题来了:给定一个整数N,你需要求出∑gcd(i, N)(1<=i <=N). 输入输出格式 输入格式: 一 ...

  8. What Kind of Friends Are You? ZOJ - 3960(ZheJiang Province Contest)

    怎么说呢...我能说我又过了一道水题? emm... 问题描述: 给定 n 个待确定名字的 Friends 和 q 个问题.已知 c 个 Friends 的名字. 对于第 i 个问题,有  个 Fri ...

  9. 一类dp的网格模型

    关于形如\(f_{i,j} = \sum_{t=1}^{|w|}\sum_{k=1}^{|v|}f_{i+w_t,j+v_k}\),其中\(w_t,v_k\)为一个定值的\(dp\)转移. 可以考虑放 ...

  10. linux内核分析 第七周读书笔记

    第七章 链接 1.链接是将各种代码和数据部分收集起来并组合成为一个单一文件的过程,这个文件可被加载到存储器并执行. 2.链接可以执行于编译时,加载时,运行时. 7.1编译器驱动程序 1.大多数编译系统 ...