Advertisement
since85stas

Untitled

Mar 2nd, 2021
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.68 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.StringTokenizer;
  3.  
  4. public class TwoBikes {
  5.  
  6.     public static void main(String[] args) throws IOException {
  7.         BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
  8.  
  9.         StringTokenizer tokenizerStr = new StringTokenizer(reader.readLine());
  10.         int n = Integer.parseInt(tokenizerStr.nextToken());
  11.  
  12.         int[] money = new int[n];
  13.         tokenizerStr = new StringTokenizer(reader.readLine());
  14.         for (int i = 0; i < n; i++) {
  15.             money[i] = Integer.parseInt(tokenizerStr.nextToken());
  16.         }
  17.  
  18.         tokenizerStr = new StringTokenizer(reader.readLine());
  19.         int s = Integer.parseInt(tokenizerStr.nextToken());
  20.  
  21.         buyBicicles(money, s);
  22.     }
  23.  
  24.     private static void buyBicicles(int[] money, int pr) {
  25.  
  26. //        pr = 5;
  27.  
  28.         int res = binaryRecurs(money, pr, 0, money.length);
  29.         System.out.print(res+1 + " ");
  30.  
  31.         res = binaryRecurs(money, pr*2, 0, money.length);
  32.         System.out.print(res+1);
  33.     }
  34.  
  35.     private static int binaryRecurs(int[] money, int goal, int leftI, int rightI) {
  36.         if (rightI == leftI) {
  37.             if (rightI < money.length && money[leftI] >=goal) {
  38.                 return leftI;
  39.             } else {
  40.                 return -2;
  41.             }
  42.         } else if (rightI < leftI) {
  43.             return -2;
  44.         }
  45.         int mid = (rightI + leftI)/2;
  46.         if (goal <= money[mid]) {
  47.             return binaryRecurs(money, goal, leftI, mid);
  48.         } else if (goal > money[mid]) {
  49.             return binaryRecurs(money, goal, mid + 1, rightI);
  50.         }
  51.         return 0;
  52.     }
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement