forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request matthewsamuel95#579 from anirudherabelly/master
added important queue algos
- Loading branch information
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class GfG{ | ||
public Queue<Integer> rev(Queue<Integer> q){ | ||
//add code here. | ||
//revHelperRecursive(q); | ||
revHelperIterative(q); | ||
return q; | ||
} | ||
private void revHelperIterative(Queue<Integer> q){ | ||
Stack<Integer> s=new Stack<Integer>(); | ||
while(!q.isEmpty()){ | ||
s.push(q.poll()); | ||
} | ||
while(!s.empty()){ | ||
q.offer(s.pop()); | ||
} | ||
} | ||
private void revHelperRecursive(Queue<Integer> q){ | ||
if(q.isEmpty())return; | ||
int temp=q.poll(); | ||
revHelperRecursive(q); | ||
q.offer(temp); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
class PetrolPump | ||
{ | ||
public int petrolGiven; | ||
public int distanceToNext; | ||
public PetrolPump(int PetrolGiven,int DistanceToNext) | ||
{ | ||
this.petrolGiven = PetrolGiven; | ||
this.distanceToNext = DistanceToNext; | ||
} | ||
} | ||
class Solution { | ||
static void Main(String[] args) { | ||
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */ | ||
int n = int.Parse(Console.ReadLine()); | ||
PetrolPump[] q = new PetrolPump[n]; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
int[] info = Array.ConvertAll(Console.ReadLine().Split(' '),int.Parse); | ||
q[i]=new PetrolPump(info[0],info[1]); | ||
} | ||
TruckTour(q,n); | ||
} | ||
private static void TruckTour(PetrolPump[] q, int n) | ||
{ | ||
int start = 0,end=1; | ||
int curr_petrol= q[start].petrolGiven- q[start].distanceToNext; | ||
while(curr_petrol<0 || start != end) | ||
{ | ||
while(curr_petrol<0 && start != end) | ||
{ | ||
curr_petrol -= (q[start].petrolGiven - q[start].distanceToNext); | ||
start = (start + 1) % n; | ||
if (start == 0) | ||
{ | ||
Console.WriteLine("Not Possible"); | ||
return; | ||
} | ||
} | ||
curr_petrol += q[end].petrolGiven - q[end].distanceToNext; | ||
end = (end + 1) % n; | ||
} | ||
Console.WriteLine(start); | ||
} | ||
} |