Advertisement
gjorgjikirkov

PetrolPumps

May 26th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.47 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package petrol_pumps_geeks4geeks;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.io.InputStreamReader;
  11. import java.util.ArrayDeque;
  12.  
  13. /**
  14.  *
  15.  * @author 141021
  16.  */
  17.  
  18. /*
  19.  Suppose there is a circle. There are n petrol pumps on that circle. You are given two sets of data.
  20.  
  21.  1. The amount of petrol that every petrol pump has.
  22.  2. Distance from that petrol pump to the next petrol pump.
  23.  
  24.  Calculate the first point from where a truck will be able to complete the
  25.  circle (The truck will stop at each petrol pump and it has infinite capacity).
  26.  Expected time complexity is O(n). Assume for 1 litre petrol, the truck can go 1 unit of distance.
  27.  
  28.  For example, let there be 4 petrol pumps with amount of petrol and distance to
  29.  next petrol pump value pairs as {4, 6}, {6, 5}, {7, 3} and {4, 5}.
  30.  The first point from where truck can make a circular tour is 2nd petrol pump.
  31.  Output should be β€œstart = 1β€³ (index of 2nd petrol pump).
  32.  */
  33. class Pumpa {
  34.  
  35.     int litriBenzin;
  36.     int rastojanie;
  37.  
  38.     public Pumpa(int litriBenzin, int rastojanie) {
  39.         this.litriBenzin = litriBenzin;
  40.         this.rastojanie = rastojanie;
  41.     }
  42. }
  43.  
  44. public class PetrolPumps {
  45.  
  46.     public static void main(String[] args) throws IOException {
  47.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  48.         ArrayDeque<Pumpa> queue = new ArrayDeque<>();
  49.         int N = Integer.parseInt(br.readLine());
  50.  
  51.         for (int i = 0; i < N; i++) {
  52.             String[] split = br.readLine().split(" ");
  53.             queue.add(new Pumpa(Integer.parseInt(split[0]), Integer.parseInt(split[1])));
  54.         }
  55.  
  56.        int brojac = 1;
  57.         int start = 0;
  58.         for (;;) {
  59.             Pumpa p = queue.peek();
  60.             if (p.litriBenzin >= p.rastojanie) {
  61.                 ++brojac;
  62.                
  63.                 if (brojac == queue.size()) {
  64.                     break;
  65.                 }
  66.                
  67.                 queue.remove(p);
  68.                 queue.add(p);
  69.             } else {
  70.                 ++brojac;
  71.                 ++start;
  72.                 queue.remove(p);
  73.                 queue.add(p);
  74.             }
  75.         }
  76.         System.out.println("start = " + start);
  77.     }
  78. }
  79. /*
  80. INPUT:
  81. 4
  82. 4 6
  83. 6 5
  84. 7 3
  85. 4 5
  86.  
  87. OUTPUT:
  88.  
  89. start = 1
  90.  
  91. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement