fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8.  
  9. public class Main {
  10. public static void main(String[] args) {
  11.  
  12. Scanner sc = new Scanner(System.in);
  13.  
  14. int n = sc.nextInt();
  15. int m = sc.nextInt();
  16.  
  17. int[] arr = new int[n + 1];
  18. String[] oper = new String[m + 1];
  19.  
  20. // read array
  21. for (int i = 1; i <= n; i++) {
  22. arr[i] = sc.nextInt();
  23. }
  24.  
  25. // read operations
  26. for (int i = 1; i <= m; i++) {
  27. oper[i] = sc.next();
  28. }
  29.  
  30. // sets to store indices
  31. TreeSet<Integer> set0 = new TreeSet<>();
  32. TreeSet<Integer> set1 = new TreeSet<>();
  33.  
  34. // initialize sets
  35. for (int i = 1; i <= n; i++) {
  36. if (arr[i] == 0) {
  37. set0.add(i);
  38. } else {
  39. set1.add(i);
  40. }
  41. }
  42.  
  43. // process operations
  44. for (int i = 1; i <= m; i++) {
  45.  
  46. String s = oper[i];
  47.  
  48. // single character operation
  49. if (s.length() == 1) {
  50. if (s.charAt(0) == 'L') {
  51. int y = set0.first(); // smallest index with 0
  52. arr[y] = 1;
  53. set0.remove(y);
  54. set1.add(y);
  55. }
  56. }
  57. else {
  58. int y = s.charAt(1) - '0';
  59. arr[y] = 0;
  60. set1.remove(y);
  61. set0.add(y);
  62. }
  63. }
  64.  
  65. // output final array
  66. for (int i = 1; i <= n; i++) {
  67. System.out.print(arr[i] + " ");
  68. }
  69. }
  70. }
  71.  
Success #stdin #stdout 0.15s 59064KB
stdin
5 4
1 0 1 0 1
L C2 L C4
stdout
1 1 1 0 1