Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Round 1, count 1 space starting from position 0: _ 1 _ _ _
- Round 2, count 2 spaces starting from position 1: _ 1 _ _ 2
- Round 3, count 3 spaces starting from position 2: 3 1 _ _ 2
- Round 4, count 4 spaces starting from position 3: 3 1 4 _ 2
- Round 5, count 5 spaces starting from position 4: 3 1 4 5 2
- memset( ans, 255, sizeof(ans) );
- while ( cur <= n )
- {
- int i, cnt;
- int left = n - cur + 1; // count how many spaces left
- left = cur % left + 1; // this line is critical, mod to save time!
- for ( i = pos, cnt = 0; ; ++i ) // simulate the process
- {
- if ( i > n ) i = 1;
- if ( ans[i] == -1 ) ++cnt;
- if ( cnt == left ) break;
- }
- ans[i] = cur;
- pos = i;
- ++cur;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement