One very well-known internet resource site (let's call it X) has come up with a New Year adventure. Specifically, they decided to give ratings to all visitors.
There are n users on the site, for each user we know the rating value he wants to get as a New Year Present. We know that user i wants to get at least ai rating units as a present.
The X site is administered by very creative and thrifty people. On the one hand, they want to give distinct ratings and on the other hand, the total sum of the ratings in the present must be as small as possible.
Help site X cope with the challenging task of rating distribution. Find the optimal distribution.
Input
The first line contains integer n (1 ≤ n ≤ 3·105) — the number of users on the site. The next line contains integer sequence a1, a2, ..., an (1 ≤ ai ≤ 109).
OutputPrint a sequence of integers b1, b2, ..., bn. Number bi means that user i gets bi of rating as a present. The printed sequence must meet the problem conditions.
If there are multiple optimal solutions, print any of them.
Examples3 5 1 1
5 1 2
1 1000000000
1000000000
题意,把输入的数列改成没有重复的并且总和最小。
思路,最开始想到用map,比如输入1之后就有m【1】=2,那么就能解决输入相同的数的问题,但是此做法问题在于map的指向位置有可能也会重复,比如
样例
5
1 1 2 2 3,
这样1变2之后,2就不能是2了,依次类推,可用while实现类似链表一样的去找头,但是会tle,因此换方法。
把数列排好序,用pl标记上此时的a[i]+1,那么a[i+1]如果小于pl的话,就一定是a[i+1]==a[i]了,于是把a[i+1]=pl, pl ++;即把a[i+1]变成a[i]+1;
如果大于等于Pl就不用改变a[i]了,因为肯定不会和前面的重复了,只需把pl变成比a[i]大1的数就好了。
#include"iostream"
#include"algorithm"
#include"map"
using namespace std;
typedef long long ll;
struct Point
{
ll x;
int v;
}a[1000005];
int cmp(Point a,Point b)
{
return a.x<b.x;
}
int cmp2(Point a,Point b)
{
return a.v<b.v;
}
int main()
{
int n;
while(cin >> n)
{
for(int i = 0;i < n;i ++)
{
cin>>a[i].x;
a[i].v=i;
}
sort(a,a+n,cmp);
int pl = a[0].x+1;
for(int i = 1;i < n;i ++)
{
if(a[i].x < pl)
{
a[i].x=pl;
pl++;
}
else
{
pl=a[i].x+1;
}
}
sort(a,a+n,cmp2);
for(int i = 0;i < n;i ++)
{
if(i)
{
cout<<" "<<a[i].x;
}
else
{
cout<<a[i].x;
}
}
cout<<endl;
}
return 0;
}