zoukankan      html  css  js  c++  java
  • UVA11093-Just Finish it up(思维)

    Problem UVA11093-Just Finish it up

    Accept: 1225  Submit: 5637
    Time Limit: 3000 mSec

    Problem Description

    Along a circular track, there are N gas stations, which are numbered clockwise from 1 up to N. At station i, there are pi gallons of petrol available. To race from station i to its clockwise neighbor one need qi gallons of petrol. Consider a race where a car will start the race with an empty fuel tank. Your task is to find whether the car can complete the race from any of the stations or not. If it can then mention the smallest possible station i from which the lap can be completed.

    Input

    First line of the input contains one integer T the number of test cases. Each test case will start with a line containing one integer N, which denotes the number of gas stations. In the next few lines contain 2∗N integers. First N integers denote the values of pis (petrol available at station i), subsequent N integers denote the value of qis (amount of patrol needed to go to the next station in the clockwise direction).

     Output

    For each test case, output the case number in the format “Case c:” , where c is the case number starting form 1. Then display whether it is possible to complete a lap by a car with an empty tank or not. If it is not possible to complete the lap then display “Not possible”. If possible, then display “Possible from station X”, where X is the first possible station from which the car can complete the lap.
    Constraints
    • T < 25
    • N < 100001
     

     Sample Input

    2
    5
    1 1 1 1 1
    1 1 2 1 1
    7
    1 1 1 10 1 1 1
    2 2 2 2 2 2 2
     

     Sample Output

    Case 1: Not possible

    Case 2: Possible from station 4

    题解:比较经典的问题,从1开始尝试,如果从p道p+1这一段走不到那就说明从2到p都不能走完全程,简单解释一下,如果能从1走到2,那么从1开始不会比从2开始更差,因为到2时的油>=0,因此1走不完,2更走不完,同理到p都走不完,直接从p+1开始试就好,复杂度线性。

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 const int maxn = 100000 + 100;
     6 
     7 int n, con = 1;
     8 int sup[maxn], req[maxn];
     9 
    10 int main()
    11 {
    12     //freopen("input.txt", "r", stdin);
    13     //freopen("output.txt", "w", stdout);
    14     int iCase;
    15     scanf("%d", &iCase);
    16     while (iCase--) {
    17         scanf("%d", &n);
    18         for (int i = 0; i < n; i++) {
    19             scanf("%d", &sup[i]);
    20         }
    21         for (int i = 0; i < n; i++) {
    22             scanf("%d", &req[i]);
    23         }
    24 
    25         printf("Case %d: ", con++);
    26         int st = 0;
    27         while (st < n) {
    28             int tmp = st;
    29             int cur = 0, T = n;
    30             while (T--) {
    31                 cur += sup[tmp];
    32                 cur -= req[tmp];
    33                 if (cur < 0) break;
    34                 tmp++;
    35                 tmp %= n;
    36             }
    37             if (T == -1) break;
    38             if (tmp + 1 <= st) {
    39                 st = n;
    40                 break;
    41             }
    42             else st = tmp + 1;
    43         }
    44         if (st == n) {
    45             printf("Not possible
    ");
    46         }
    47         else printf("Possible from station %d
    ", st+1);
    48     }
    49     return 0;
    50 }
  • 相关阅读:
    Python 特点
    Python简介
    数据库查询语句
    人月神话读书笔记01
    团队介绍
    团队项目一 原型展示+电梯演讲
    全球疫情可视化展示
    NABCD模型
    第六周学习进度
    构建之法阅读笔记03
  • 原文地址:https://www.cnblogs.com/npugen/p/9672323.html
Copyright © 2011-2022 走看看