codeforces-Reorder the Array

点击打开 题目链接
A. Reorder the Array
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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>1040>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 (1n1051≤n≤105) — the length of the array.

The second line contains nn integers a1,a2,,ana1,a2,…,an (1ai1091≤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
Copy
7
10 1 1 1 5 5 3
output
Copy
4
input
Copy
5
1 1 1 1 1
output
Copy
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.

感觉这道题描述有点不人性化,大概意思就是:给你一组数据,然后你可以随意调换这组数,然后假设改变后这组数据,比原来这组数对应位置的数大的有n个,让你求n得最大值。
这基本就是个水题吧。我得思路是先把数存到两个数组里面,都拍一下序。

然后用第一个第二个数组和第一个数组逐个比较,看看最多有多少个大于第一个数组,类似于两个有序数列的排序算法。

import java.io.*;
import java.util.*;
public class Main {
	public static void main(String[] args) throws IOException {
		
		StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));  
		in.nextToken();
		int n=(int) in.nval;
		int[] arr=new int[n];
		int[]arr2=new int[n];
		for(int i=0;i<n;i++){
		in.nextToken();
		int a=(int) in.nval;
		arr[i]=a;
		arr2[i]=a;
		}
		Arrays.sort(arr);
		Arrays.sort(arr2);
		int i=0,j=0;//i是arr的下标,j是arr2的下标。
		while(j!=n){
			int a=arr2[j];
			if(a>arr[i]){i++;}//如果arr2的j大于arr的i,就与arr的下一个元素比较
			j++;
		}
		System.out.println(i);//最后i的只就是最终答案
	}
	}

猜你喜欢

转载自blog.csdn.net/king8611/article/details/81042255
今日推荐