- Practice
- Data Structures
- Queues
- Truck Tour
- Discussions

# Truck Tour

# Truck Tour

nickdu + 0 comments I'm also confused why this is under the queue section. I solved it without a queue, stack, list, etc. Just keep track of how much fuel is in the tank. Once it goes below zero then reset the fuel in the tank to zero and set the pump to the next pump.

#include <iostream> using namespace std; int main() { unsigned int n; cin >> n; long long tank = 0; unsigned int pump = 0; for (unsigned int i = 0; i < n; ++i) { unsigned int liters; unsigned int kilometers; cin >> liters >> kilometers; tank += (long long) (unsigned long long) liters - (long long) (unsigned long long) kilometers; if (tank < 0) { pump = i + 1; tank = 0; } } cout << pump << endl; }

a_anofriichuk + 0 comments Queue-based solutuon, just in case...

struct gasStation { int gas; int next; }; int main() { int N; cin >> N; queue <struct gasStation> route; for (int i = 0; i < N; i++) { struct gasStation st; cin >> st.gas >> st.next; route.push(st); } int start = 0, passed = 0, gas = 0; while (passed < N) { struct gasStation st = route.front(); gas += st.gas; route.pop(); if (gas >= st.next) { passed += 1; gas -= st.next; } else { start += passed + 1; passed = 0; gas = 0; } route.push(st); } cout << start << endl; return 0; }

Gladdyu + 0 comments This was the absolute easiest problem 'Difficult' problem on this site so far. Possible in linear time and you don't need any queues whatsoever.

**Spoiler alert:**Integration and global minima.

vinay_star_lord + 0 comments Easiest approach:

N=input()

sumi=0;

maxi=0;

j=0;

for i in range(N):

`a,b=raw_input().split() sumi+=int(a)-int(b) if(sumi<0): sumi=0 j=i+1`

print(j)

driftwood + 0 comments ### C++ USERS

**This problem can be completed in O(N) (linear) time complexity using**`std::queue<type>`

;*if you've not utilized*`queue`

when solving the problem, you've not been successful (mind the category that the problem is in...)**HINTS:***- You require only the standard constructor class of*`std::queue<type>`

*- O(N) is possible; you must only iterate through the*`queue<type>`

once*- Mind the size of input variables; set*`type`

accordingly*-*`queue<type>`

's`type`

must make use of`std::pair<type,type>`

Sort 176 Discussions, By:

Please Login in order to post a comment