C++ HackerRank|Truck Tour

  1. Dashboard 
  2.  Data Structures 
  3.  Queues 
  4.  Truck Tour
#include <iostream>
#include <queue>
using namespace std;

int main(int argc, char const *argv[])
{
    queue<long long> q;
    int n;
    long long x, y;
    cin >> n;
    for (int i = 0; i < n; ++ i)
    {
        cin >> x >> y;
        q.push(x - y);
    }
    long long tank = 0;
    int i = 0;
    for (int count = 0; count < n; ++ count)
    {
        tank += q.front();
        q.pop();
        if (tank < 0)
        {
            // 若在 count 處油的儲量已爲負, 則從 原i 到 count 都不可能是題目要求的 smallest index
            i = count + 1;
            tank = 0;
        }
    }
    cout << i << endl;

    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章