tinycodinglm / cf /cf-1007-A.jsonl
winapiadmin's picture
Upload folder using huggingface_hub
1f3a244 verified
|
Raw History Blame Contribute Delete
3.02 kB
opencode trace
System You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code.

You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code.

User

Problem: Reorder the Array (Rating: 1300)
Tags: combinatorics, data structures, math, sortings, two pointers

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]$, we can permute it so that it becomes $[20, 40, 10, 30]$. Then on the first and the second positions the integers became larger ($20>10$, $40>20$) and did not on the third and the fourth, so for this permutation, the number that Vasya wants to maximize equals $2$. 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 $n$ ($1 \leq n \leq 10^5$) — the length of the array.

The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) — 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

Assistant

The answer is $$$n$$$ minus maximal number of equal elements.
Let the maximal number of equals be $$$x$$$. Let's proove that $$$n-x$$$ is reachable. It's clear that for every permutation of the array the answer will be the same, so let's sort the array in non-decreasing order. Now we should just make a left shift on $$$x$$$. After it the $$$n-x$$$ right elements will move to a position of a smaller element.
Now let's proove that the answer is no more than $$$n-x$$$. Let's consider some permutation. It's known that every permutation breaks into cycles. Let's look at two occurences of the same number in the same cycle. Then there is at least one number between them which will move on a postion of a non-smaller element. Even if it the same occurence and even if the length of the cycle is $$$1$$$, we can say that for every occurence of this number there is at least one number which moves on a postion of a non-smaller one. So if some number occurs $$$x$$$ times, there are at least $$$x$$$ bad positions and therefore no more than $$$n-x$$$ good positions.
To count the number of equals you can, for instance, use std::map.