Advertisement
a_chn

Untitled

Sep 1st, 2024
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.64 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.PrintStream;
  4. import java.util.*;
  5.  
  6. public class a {
  7.     public static void main(String[] args) throws FileNotFoundException {
  8.         Scanner scn = new Scanner(new File("paint.in"));
  9.         int N = scn.nextInt();
  10.         int K = scn.nextInt();
  11.         TreeMap<Integer,Integer> positions = new TreeMap<>();
  12.         int curr_pos = 0;
  13.         char dir = 'X';
  14.         for(int i=0;i<N;i++){
  15.             int dist = scn.nextInt();
  16.             char new_dir = scn.next().charAt(0);
  17.             if(new_dir == 'R') {
  18.                 positions.putIfAbsent(curr_pos, 0);
  19.                 positions.replace(curr_pos, positions.get(curr_pos) + 1);
  20.                 curr_pos += dist;
  21.                 positions.putIfAbsent(curr_pos, 0);
  22.                 positions.replace(curr_pos, positions.get(curr_pos) - 1);
  23.             } else {
  24.                 curr_pos -= dist;
  25.                 positions.putIfAbsent(curr_pos, 0);
  26.                 positions.replace(curr_pos, positions.get(curr_pos) + 1);
  27.                 curr_pos += dist;
  28.                 positions.putIfAbsent(curr_pos, 0);
  29.                 positions.replace(curr_pos, positions.get(curr_pos) - 1);
  30.                 curr_pos -= dist;
  31.             }
  32.         }
  33.         int ans = 0;
  34.         int curr = 0;
  35.         int prev = 0;
  36.         for(int pos : positions.keySet()) {
  37.             if(curr >= K) {
  38.                 ans += pos - prev;
  39.             }
  40.             curr += positions.get(pos);
  41.             prev = pos;
  42.         }
  43.         PrintStream ps = new PrintStream("paint.out");
  44.         ps.println(ans);
  45.         ps.close();
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement