Topic ::
Links: https://ac.nowcoder.com/acm/contest/1080/B
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.
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.
Enter ::
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.
Output ::
Output line number n, denotes the i-th position in the hash table corresponding to the number i. (0≤i≤n-1)
Sample 1 ::
4 1 2 6 5
Sample 2 ::
4 3 0 7 11
Analysis ::
ordinary thinking must go once again found TLE; that we can optimize to write about the half, which set a good thought of container;
first pretreatment 0 ~ n-1 is inserted into the set, for doing data input processing using lower_bound function can quickly find the first empty location, and delete it; when returns end () is changed to the need s.begin (); and deletes;
herein returned iterator represents the value that is placed in position, can start to store the array;
details see :: Code
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const ll MOD=998244353; 5 const int maxn=1e6+10; 6 int ma[maxn]; 7 set<int>s; 8 int main() 9 { 10 s.clear(); 11 int n; 12 scanf("%d",&n); 13 for(int i=0;i<n;i++){ 14 s.insert(i); 15 } 16 for(int i=1;i<=n;i++){ 17 int x,k; 18 scanf("%d",&x); 19 int ans=x; 20 x%=n; 21 set<int>::iterator it; 22 it=s.lower_bound(x); 23 if(it!=s.end()){ 24 k=*it; 25 s.erase(it); 26 } 27 else{ 28 it=s.begin(); 29 k=*it; 30 s.erase(it); 31 } 32 ma[k]=ans; 33 } 34 for(int i=0;i<n;i++){ 35 if(i==0){ 36 printf("%d",ma[i]); 37 } 38 else{ 39 printf(" %d",ma[i]); 40 } 41 } 42 return 0; 43 }
I have this problem as well as solution
Similar disjoint-set (also can not say that -_-)
analysis::
First, pre-recording is 0 ~ n-1, and then find an empty position by finding fathers
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const ll MOD=998244353; 5 const int maxn=1e6+10; 6 int ma[maxn]; 7 int f[maxn]; 8 int getfather(int x) 9 { 10 return x==f[x]?x:f[x]=getfather(f[x]); 11 } 12 int main() 13 { 14 int n; 15 scanf("%d",&n); 16 for(int i=0;i<n;i++){ 17 f[i]=i; 18 } 19 for(int i=1;i<=n;i++){ 20 int x,y; 21 scanf("%d",&x); 22 y=x; 23 x=getfather(x%n); 24 ma[x]=y; 25 F [X] = (X + . 1 )% n-; // change the occupied positions and points after the first empty position 26 is } 27 for ( int I = 0 ; I <n-; I ++ ) { 28 IF (I == 0 ) { 29 the printf ( " % D " , mA [I]); 30 } 31 is the else { 32 the printf ( " % D " , mA [I]); 33 is } 34 is } 35 return 0 ; 36 }