Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. Round 1, count 1 space starting from position 0: _ 1 _ _ _
  2. Round 2, count 2 spaces starting from position 1: _ 1 _ _ 2
  3. Round 3, count 3 spaces starting from position 2: 3 1 _ _ 2
  4. Round 4, count 4 spaces starting from position 3: 3 1 4 _ 2
  5. Round 5, count 5 spaces starting from position 4: 3 1 4 5 2
  6.  
  7. memset( ans, 255, sizeof(ans) );
  8. while ( cur <= n )
  9. {
  10. int i, cnt;
  11. int left = n - cur + 1; // count how many spaces left
  12. left = cur % left + 1; // this line is critical, mod to save time!
  13.  
  14. for ( i = pos, cnt = 0; ; ++i ) // simulate the process
  15. {
  16. if ( i > n ) i = 1;
  17. if ( ans[i] == -1 ) ++cnt;
  18. if ( cnt == left ) break;
  19. }
  20.  
  21. ans[i] = cur;
  22. pos = i;
  23.  
  24. ++cur;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement