fork download
  1. import java.util.*;
  2. import java.util.stream.*;
  3. class IntPermutation implements Iterable<int[]> {
  4. private final int[] a;
  5. public IntPermutation(int[] a) {
  6. this.a = a.clone();
  7. }
  8. public IntPermutation(int n) {
  9. this.a = new int[n];
  10. for (int i = 0; i < n; i++) a[i] = i;
  11. }
  12. public Iterator<int[]> iterator() {
  13. return iterator_shared();
  14. }
  15. public Iterator<int[]> iterator_isolated() {
  16. return a == null ? Collections.emptyIterator() : new Iterator<int[]>() {
  17. private int[] a = IntPermutation.this.a.clone();
  18. public boolean hasNext() {return a != null;}
  19. public int[] next() {
  20. if (!hasNext()) throw new NoSuchElementException();
  21. int[] b = a.clone();
  22. a = next_or_null(a);
  23. return b;
  24. }
  25. };
  26. }
  27. public Iterator<int[]> iterator_shared() {
  28. return a == null ? Collections.emptyIterator() : new Iterator<int[]>() {
  29. private int[] a = IntPermutation.this.a.clone();
  30. private int[] b = a;
  31. public boolean hasNext() {
  32. if (a != null && b == null) b = a = next_or_null(a);
  33. return a != null;
  34. }
  35. public int[] next() {
  36. if (!hasNext()) throw new NoSuchElementException();
  37. b = null;
  38. return a;
  39. }
  40. };
  41. }
  42. public Stream<int[]> stream() {
  43. return StreamSupport.stream(spliterator(), false);
  44. }
  45. private static int[] swap(int i, int j, int[] a) {
  46. int t = a[i];a[i] = a[j];a[j] = t;
  47. return a;
  48. }
  49. private static int[] rev(int b, int e, int[] a) {
  50. for (e--; b < e; b++, e--) a = swap(b, e, a);
  51. return a;
  52. }
  53. private static int[] next_or_null(int[] a) {
  54. int e = a.length;
  55. for (int i = e - 2, j = e - 1; 0 <= i; i--, j--)
  56. if (a[i] < a[j])
  57. for (int k = e - 1; i < k; k--)
  58. if (a[i] < a[k])
  59. return rev(j, e, swap(i, k, a));
  60. return null;
  61. }
  62. }
  63. class Ideone {
  64. private static int[] f(int[] a, int n) {
  65. var it = new IntPermutation(a).iterator();
  66. if (0 < n) for (int i = 1; it.hasNext(); i++) {
  67. var b = it.next();
  68. if (i == n) return b;
  69. }
  70. return null;
  71. }
  72. public static void main(String[] args) {
  73. System.out.println(Arrays.toString(f(new int[] {1,2,3,4,5,6,7,8,9}, 1)));
  74. System.out.println(Arrays.toString(f(new int[] {1,2,3,4,5,6,7,8,9}, 2)));
  75. System.out.println(Arrays.toString(f(new int[] {1,2,3,4,5,6,7,8,9}, 3)));
  76. System.out.println(Arrays.toString(f(new int[] {1,2,3,4,5,6,7,8,9}, 123456)));
  77. System.out.println(Arrays.toString(f(new int[] {1,2,3,4,5,6,7,8,9}, 234567)));
  78. System.out.println(Arrays.toString(f(new int[] {1,2,3,4,5,6,7,8,9}, 362880)));
  79. System.out.println(Arrays.toString(f(new int[] {1,1,1,2,2,2,3,3,3,4,4,4}, 1)));
  80. System.out.println(Arrays.toString(f(new int[] {1,1,1,2,2,2,3,3,3,4,4,4}, 2)));
  81. System.out.println(Arrays.toString(f(new int[] {1,1,1,2,2,2,3,3,3,4,4,4}, 3)));
  82. System.out.println(Arrays.toString(f(new int[] {1,1,1,2,2,2,3,3,3,4,4,4}, 123456)));
  83. System.out.println(Arrays.toString(f(new int[] {1,1,1,2,2,2,3,3,3,4,4,4}, 234567)));
  84. System.out.println(Arrays.toString(f(new int[] {1,1,1,2,2,2,3,3,3,4,4,4}, 369600)));
  85. }
  86. }
  87.  
Success #stdin #stdout 0.14s 57268KB
stdin
Standard input is empty
stdout
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 9, 8]
[1, 2, 3, 4, 5, 6, 8, 7, 9]
[4, 1, 6, 5, 8, 9, 7, 3, 2]
[6, 8, 4, 7, 5, 3, 2, 1, 9]
[9, 8, 7, 6, 5, 4, 3, 2, 1]
[1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]
[1, 1, 1, 2, 2, 2, 3, 3, 4, 3, 4, 4]
[1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 3, 4]
[2, 2, 2, 3, 3, 1, 4, 3, 4, 1, 1, 4]
[3, 2, 4, 4, 2, 4, 3, 3, 1, 1, 1, 2]
[4, 4, 4, 3, 3, 3, 2, 2, 2, 1, 1, 1]