fork download
  1. import java.io.*;
  2. import java.util.*;
  3. import java.math.*;
  4.  
  5. import static java.lang.Math.*;
  6. import static java.lang.Character.*;
  7. import static java.util.Arrays.*;
  8. import static java.util.Collections.*;
  9. import static java.math.BigInteger.*;
  10.  
  11. class A {
  12. static final char PROB = A.class.getSimpleName().charAt(0);
  13. static final boolean PRAC = false;
  14.  
  15. static final int INF = 1 << 20;
  16. static final int[] di = { -1, 0, 0, 1 };
  17. static final int[] dj = { 0, -1, 1, 0 };
  18. static Scanner sc = new Scanner(System.in);
  19.  
  20. final int N;
  21. final char[] R;
  22. final int[] P;
  23.  
  24. public A() {
  25. N = sc.nextInt();
  26. R = new char[N];
  27. P = new int[N];
  28. for(int i = 0; i < N; i++) {
  29. R[i] = sc.next().charAt(0);
  30. P[i] = sc.nextInt();
  31. }
  32. }
  33.  
  34. public String solve() {
  35. int ans = 0;
  36. for(int i = 0, ot = 0, bt = 0, op = 1, bp = 1; i < N; i++)
  37. switch(R[i]) {
  38. case 'O' :
  39. ans = ot = max(ot + abs(P[i] - op), bt) + 1;
  40. op = P[i];
  41. break;
  42. case 'B' :
  43. ans = bt = max(bt + abs(P[i] - bp), ot) + 1;
  44. bp = P[i];
  45. break;
  46. }
  47. return "" + ans;
  48. }
  49.  
  50. public static void main(String... args) {
  51. //large();
  52. int T = Integer.parseInt(sc.nextLine());
  53. for (int t = 1; t <= T; t++) {
  54. System.err.printf("Case #%s%n", t);
  55. System.out.printf("Case #%s: %s%n", t, new A().solve());
  56. }
  57. System.err.println("done.");
  58. }
  59.  
  60. public static void small() {
  61. String in = PROB + "-small" + (PRAC ? "-practice" : "-attempt" + 0) + ".in";
  62. String out = PROB + "-small.out";
  63. try {
  64. if (!PRAC)
  65. for (int i = 1; new File(PROB + "-small" + "-attempt" + i + ".in").exists(); i++)
  66. in = PROB + "-small" + "-attempt" + i + ".in";
  67. System.setOut(new PrintStream(out));
  68. } catch (Exception e) {
  69. e.printStackTrace();
  70. System.exit(0);
  71. }
  72. sc = new Scanner(System.in);
  73. }
  74.  
  75. public static void large() {
  76. String in = PROB + "-large" + (PRAC ? "-practice" : "") + ".in";
  77. String out = PROB + "-large.out";
  78. try {
  79. System.setOut(new PrintStream(out));
  80. } catch (Exception e) {
  81. e.printStackTrace();
  82. System.exit(0);
  83. }
  84. sc = new Scanner(System.in);
  85. }
  86.  
  87. private static void debug(Object... os) {
  88. System.err.println(deepToString(os));
  89. }
  90. }
  91.  
Success #stdin #stdout 0.08s 213888KB
stdin
3
4 O 2 B 1 B 2 O 4
3 O 5 O 8 B 100
2 B 2 B 1
stdout
Case #1: 6
Case #2: 100
Case #3: 4