C++ HackerRank|Truck Tour

来源:互联网 发布:booking.it 编辑:程序博客网 时间:2024/06/15 07:08

  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;}


0 0
原创粉丝点击