How can I reorder the array in C to achieve a long-tail word?
- 内容介绍
- 文章标签
- 相关推荐
本文共计833个文字,预计阅读时间需要4分钟。
给定一个整数数组。Vasya 可以重新排列这些整数。他希望尽可能多的整数移动到之前比它们小的整数所在的位置。帮助 Vasya 找到最多可以移动的整数数量。
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
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
The second line contains
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
Input7
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
In the second sample, there is no way to increase any element with a permutation, so the answer is 0.
#include <iostream> #include <algorithm> #include <cstdio> #include <string> #include <cstring> #include <cstdlib> #include <map> #include <vector> #include <set> #include <queue> #include <stack> #include <cmath> typedef long long lli; using namespace std; const int mxn = 1e9; multiset<int> v; multiset<int> :: iterator it ; int main() { int n,b,cnt=0; cin>>n; int a[n+5]; for(int i=0; i<n; i++) { cin>>a[i]; v.insert(a[i]); } for(int i=0; i<n; i++) { it = v.lower_bound(a[i]+1); if( it!=v.end() ) { cnt++; v.erase(it); } } cout<<cnt<<endl; return 0; }
本文共计833个文字,预计阅读时间需要4分钟。
给定一个整数数组。Vasya 可以重新排列这些整数。他希望尽可能多的整数移动到之前比它们小的整数所在的位置。帮助 Vasya 找到最多可以移动的整数数量。
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
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
The second line contains
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
Input7
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
In the second sample, there is no way to increase any element with a permutation, so the answer is 0.
#include <iostream> #include <algorithm> #include <cstdio> #include <string> #include <cstring> #include <cstdlib> #include <map> #include <vector> #include <set> #include <queue> #include <stack> #include <cmath> typedef long long lli; using namespace std; const int mxn = 1e9; multiset<int> v; multiset<int> :: iterator it ; int main() { int n,b,cnt=0; cin>>n; int a[n+5]; for(int i=0; i<n; i++) { cin>>a[i]; v.insert(a[i]); } for(int i=0; i<n; i++) { it = v.lower_bound(a[i]+1); if( it!=v.end() ) { cnt++; v.erase(it); } } cout<<cnt<<endl; return 0; }

