Advertisement
SergeyBochkov

Untitled

Feb 7th, 2023 (edited)
773
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. @SpringBootApplication
  5. public class YandexTestApplication {
  6.  
  7.  
  8.     public static void main(String[] args) {
  9.         SpringApplication.run(YandexTestApplication.class, args);
  10.         int N;
  11.         int K;
  12.         int steps = 0;
  13.         BufferedReader reader = null;
  14.         try {
  15.             reader = new BufferedReader(new FileReader("C:\\Users\\serge\\IdeaProjects\\YandexTest\\src\\main\\resources\\input.txt"));
  16.         } catch (FileNotFoundException e) {
  17.             throw new RuntimeException(e);
  18.         }
  19.         String file = null;
  20.         try {
  21.             file = reader.readLine();
  22.             N = Integer.parseInt(file);
  23.             List<Integer> list = new ArrayList<>();
  24.             List<Integer> listval = new ArrayList<>();
  25.             List<Integer> ref = new ArrayList<>();
  26.             file = reader.readLine();
  27.             String[] nums = file.split(" ");
  28.             for (String num : nums) {
  29.                 list.add(Integer.parseInt(num));
  30.             }
  31.             file = reader.readLine();
  32.             nums = file.split(" ");
  33.             for (int i = 0; i < N; i++) {
  34.                 listval.add(Integer.parseInt(nums[i]));
  35.             }
  36.             file = reader.readLine();
  37.             K = Integer.parseInt(file);
  38.             file = reader.readLine();
  39.             nums = file.split(" ");
  40.             for (int i = 0; i < K; i++) {
  41.                 ref.add(Integer.parseInt(nums[i]));
  42.             }
  43.             int temp, temp2;
  44.             for (int i = 0; i < K - 1; i++) {
  45.                 temp = ref.get(i);
  46.                 temp2 = ref.get(i + 1);
  47.                 if (listval.get(list.indexOf(temp)) != listval.get(list.indexOf(temp2))) {
  48.                     steps++;
  49.                 }
  50.             }
  51.             try (FileWriter writer = new FileWriter("C:\\Users\\serge\\IdeaProjects\\YandexTest\\src\\main\\resources\\output.txt")) {
  52.                 writer.write(String.valueOf(steps));
  53.                 writer.flush();
  54.             } catch (IOException e) {
  55.                 throw new RuntimeException(e);
  56.             }
  57.         } catch (IOException e) {
  58.             throw new RuntimeException(e);
  59.         }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement