MicrosoftOA #63
Skyscraper
Estimated EasyJudge-ready
Topics (AI-labelled)Greedy
Assign each building a positive integer height B[i] not exceeding its limit A[i], such that all heights are distinct and their sum is maximized. A solution is guaranteed to exist, and any optimal assignment is accepted.
This site uses a standard input/output protocol.
Input
The first line contains N (1..50000), and the second line contains N values A[i] (1..10⁹); a valid assignment of distinct positive integers is guaranteed to exist.
Output
Output N positive integer heights B, keeping the buildings in their original input order.
Example 1
Input:
3
1 2 3
Output:
1 2 3
Example 2
Input:
5
9 4 3 7 7
Output:
9 4 3 6 7
Example 3
Input:
5
2 5 4 5 5
Output:
1 2 3 4 5
Example notes
Example 1: every building can take its limit, for a total of 6. Example 2: the two limits of 7 become 7 and 6, and the others are 9, 4, 3, for a total of 29; the assignments of the two buildings with limit 7 can be swapped. Example 3: only the five distinct heights 1..5 are available, for a total of 15; for example, 1, 2, 3, 4, 5 is valid.
Editorial
Loading solution…