Link: https: //ac.nowcoder.com/acm/contest/1080/B
Source: Cattle-off network
There tokitsukaze number n, the order need to plug them into a hash table, the hash table position is 0 to n-1.
Insert rule is:
the beginning of the hash table is empty.
For a number of x, the hash table, if (x mod n) position is empty, put on x (x mod n) position. If it is not empty, from (x mod n) to the right to find the start of an empty position of the insert. If up to n-1 is not empty, start from position 0 Continue right to find the first empty position is inserted.
Since the hash table a total of n space, the number n needs to be inserted, so that each number can be inserted.
After tokitsukaze now want to know this number n sequentially inserted hash table, the hash table in each position corresponding to each of which the number.
Input Description:
The first line contains a positive integer n (1≤n≤10 ^ 6).
The second line comprises a non-negative integer n x (0≤x≤10 ^ 9), these numbers are sequentially inserted from left to right hash table.
Description Output:
output a line number n, denotes the i-th position in the hash table corresponding to the number i. (0≤i≤n-1)
Example 1
input
Copy
. 4
. 1 2. 6. 5
Output
Copy
5126
Description
When inserting 1, 1 mod 4 = 1, is empty, an inserted position.
When the insert 2, 2 mod 4 = 2, is empty, is inserted in the 2 position.
Insertion 6, 6 mod 4 = 2, is not empty, to find the next empty location is 3, 3 inserted position.
Insert 5, 5 mod 4 = 1, is not empty, to find the next free position is 0, the insertion position 0.
Example 2
Input
Copy
. 4
. 3. 7 0. 11
output
Copy
07113
Description
When inserted 3, 3 mod 4 = 3, is empty, is inserted in position 3.
When inserting 0, 0 mod 4 = 0, is empty at position 0 is inserted.
The insertion 7, 7 mod 4 = 3, is not empty, to find the next empty location 1, the insertion position 1.
Insert 11, 11 mod 4 = 3, is not empty, to find the next empty location is 2, inserted in position 2.
Meaning of the questions: For a number x, if (x mod n) position is empty, put on x (x mod n) position, if there are elements of the position, the back one so ( when n have not found it to return to the start position 0)
#include <iostream>
#include <set>
using namespace std;
const int N = 1000005;
int res[N];
int main()
{
ios::sync_with_stdio(false);
set<int>s;
int n;
cin >> n;
for(int i=0;i<n;i++)///先用set将每个位置插入
s.insert(i);
for(int i=0;i<n;i++)
{
int x;
cin >> x;
int r=x%n;
auto it=s.lower_bound(r);///二分寻找第一个大于等于x%m的位置
if(it==s.end())如果不存在 则将其放在set的第一个位置
it=s.begin();
res[*it]=x;///记录答案
s.erase(it);///将用过的位置抹去
}
for(int i=0;i<n;i++)
{
cout << res[i] << " ";///输出
}
cout << endl;
return 0;
}
Was writing this time should expect to find that half position, but did not think if it is to start from scratch how to proceed.
Disjoint-set Niubi ...
#include <iostream>
using namespace std;
const int N = 1e6+5;
int fa[N],res[N];
int findx(int x)
{
return fa[x]==x?fa[x]:fa[x]=findx(fa[x]);
}
int main()
{
ios::sync_with_stdio(false);
int n;
cin >> n;
for(int i=0;i<n;i++)///初始化
fa[i]=i;
for(int i=0;i<n;i++)
{
int x;
cin >> x;
int pos=findx(x%n);///寻找
res[pos]=x;
fa[pos]=(pos+1)%n///更新使用过的位置使其后移一位
}
for(int i=0;i<n;i++)
cout << res[i] << " ";
cout << endl;
return 0;
}