Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.PrintStream;
- import java.util.*;
- public class a {
- public static void main(String[] args) throws FileNotFoundException {
- Scanner scn = new Scanner(new File("paint.in"));
- int N = scn.nextInt();
- int K = scn.nextInt();
- TreeMap<Integer,Integer> positions = new TreeMap<>();
- int curr_pos = 0;
- char dir = 'X';
- for(int i=0;i<N;i++){
- int dist = scn.nextInt();
- char new_dir = scn.next().charAt(0);
- if(new_dir == 'R') {
- positions.putIfAbsent(curr_pos, 0);
- positions.replace(curr_pos, positions.get(curr_pos) + 1);
- curr_pos += dist;
- positions.putIfAbsent(curr_pos, 0);
- positions.replace(curr_pos, positions.get(curr_pos) - 1);
- } else {
- curr_pos -= dist;
- positions.putIfAbsent(curr_pos, 0);
- positions.replace(curr_pos, positions.get(curr_pos) + 1);
- curr_pos += dist;
- positions.putIfAbsent(curr_pos, 0);
- positions.replace(curr_pos, positions.get(curr_pos) - 1);
- curr_pos -= dist;
- }
- }
- int ans = 0;
- int curr = 0;
- int prev = 0;
- for(int pos : positions.keySet()) {
- if(curr >= K) {
- ans += pos - prev;
- }
- curr += positions.get(pos);
- prev = pos;
- }
- PrintStream ps = new PrintStream("paint.out");
- ps.println(ans);
- ps.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement