Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. import java.io.*;
  2. import java.lang.reflect.Array;
  3. import java.util.*;
  4.  
  5. import static java.lang.Math.*;
  6.  
  7. public class Main {
  8. ArrayList<Integer>[] graph;
  9. int[] dist;
  10. boolean[] used;
  11.  
  12. void bfs(Queue<Integer> q) {
  13. while (!q.isEmpty()) {
  14. int to = q.poll();
  15. used[to] = true;
  16. for (Integer c : graph[to]) {
  17. if (!used[c]) q.add(c);
  18. dist[c] = min(dist[c], dist[to] + 1);
  19. }
  20. }
  21. }
  22.  
  23. void run() throws IOException {
  24. int n = nextInt();
  25. int[] a = new int[n];
  26. dist = new int[n];
  27. graph = new ArrayList[n];
  28. for (int i = 0; i < graph.length; i++) {
  29. graph[i] = new ArrayList<>();
  30. }
  31. for (int i = 0; i < a.length; i++) {
  32. a[i] = nextInt();
  33. }
  34. Queue<Integer> chet = new ArrayDeque<>();
  35. Queue<Integer> nechet = new ArrayDeque<>();
  36. for (int i = 0; i < a.length; i++) {
  37. if (i - a[i] > -1) graph[i - a[i]].add(i);
  38. if (i + a[i] < n) graph[i + a[i]].add(i);
  39. if (a[i] % 2 == 0) chet.add(i);
  40. else nechet.add(i);
  41. }
  42. Arrays.fill(dist, Integer.MAX_VALUE);
  43. used = new boolean[n];
  44. for (int i = 0; i < a.length; i++) {
  45. if (a[i] % 2 == 0) {
  46. dist[i] = 0;
  47. used[i] = true;
  48. }
  49. }
  50. bfs(chet);
  51. int[] ans = new int[n];
  52. for (int i = 0; i < dist.length; i++) {
  53. if (a[i] % 2 != 0) ans[i] = dist[i];
  54. }
  55. Arrays.fill(dist, Integer.MAX_VALUE);
  56. used = new boolean[n];
  57. for (int i = 0; i < a.length; i++) {
  58. if (a[i] % 2 == 1) {
  59. dist[i] = 0;
  60. used[i] = true;
  61. }
  62. }
  63. bfs(nechet);
  64. for (int i = 0; i < ans.length; i++) {
  65. if (a[i] % 2 != 1) ans[i] = dist[i];
  66. }
  67. for (int i = 0; i < ans.length; i++) {
  68. if(ans[i] == Integer.MAX_VALUE)pw.print(-1 + " ");
  69. else pw.print(ans[i] + " ");
  70. }
  71. pw.close();
  72. }
  73.  
  74. class point implements Comparable<point> {
  75. int x, y;
  76.  
  77. public point(int a, int b) {
  78. x = a;
  79. y = b;
  80. }
  81.  
  82. @Override
  83. public int compareTo(point o) {
  84. if (o.y == this.y) return Integer.compare(o.x, this.x);
  85. return -Integer.compare(o.y, this.y);
  86. }
  87.  
  88. }
  89.  
  90. long mod = (long) 1e9 + 7;
  91. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  92. //BufferedReader br = new BufferedReader(new FileReader("qual.in"));
  93.  
  94. StringTokenizer st = new StringTokenizer("");
  95. PrintWriter pw = new PrintWriter(System.out);
  96. //PrintWriter pw = new PrintWriter("qual.out");
  97.  
  98. int nextInt() throws IOException {
  99. return Integer.parseInt(next());
  100. }
  101.  
  102. String next() throws IOException {
  103. if (!st.hasMoreTokens()) {
  104. st = new StringTokenizer(br.readLine());
  105. }
  106. return st.nextToken();
  107. }
  108.  
  109. long nextLong() throws IOException {
  110. return Long.parseLong(next());
  111. }
  112.  
  113. double nextDouble() throws IOException {
  114. return Double.parseDouble(next());
  115. }
  116.  
  117. public Main() throws FileNotFoundException {
  118. }
  119.  
  120. public static void main(String[] args) throws IOException {
  121. new Main().run();
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement