zoukankan      html  css  js  c++  java
  • Just Finish it up UVA

    Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

    []   [Go Back]   [Status]  

    Description

     

     

    0 6

    Problem J: Just Finish it up

    Input: standard input

    Output: standard output

     

    Along a circular track, there are N gas stations, which are numbered clockwise from 1 up to N. At station i, there are i gallons of petrol available. To race from station 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 is (petrol available at station i), subsequent N integers denote the value of is (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

    Output for 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

    Case 1: Not possible

    Case 2: Possible from station 4

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 using namespace std;
     5 int a[220000],n;
     6 int work()
     7 {
     8     int sum=0,l=0,r=0,i,m=n<<1;
     9     for(i=n; i<m; i++)a[i]=a[i-n];
    10     while(1)
    11     {
    12         while(l<n&&sum<0)sum-=a[l++];
    13         if(l==n)break;
    14         while(r<m&&sum>=0)
    15         {
    16             sum+=a[r++];
    17             if(r-l==n&&sum>=0)break;
    18         }
    19         if(r-l==n&&sum>=0)break;
    20     }
    21     return l+1;
    22 }
    23 int main()
    24 {
    25     int t,r,i,suma,sumb,x;
    26     scanf("%d",&t);
    27     for(r=1; r<=t; r++)
    28     {
    29         scanf("%d",&n);
    30         suma=sumb=0;
    31         for(i=0; i<n; i++)scanf("%d",&a[i]),suma+=a[i];
    32         for(i=0; i<n; i++)scanf("%d",&x),sumb+=x,a[i]-=x;
    33         printf("Case %d: ",r);
    34         if(suma<sumb)
    35         {
    36             printf("Not possible
    ");
    37         }
    38         else
    39             printf("Possible from station %d
    ",work());
    40     }
    41 }
    View Code
  • 相关阅读:
    Java 中String、StringBuffer、StringBuilder的差别
    [转载]StringBuffer versus String
    二维数组的连续子数组的最大和
    一位数组的最大和
    js中常见的去重方式
    魅族2016Java互联网方向其中一道笔试题--青蛙跳台阶问题
    美团在线编程2016--最大差值
    [转载]MySQL开发中常用的查询语句总结
    实现字符串全排列
    笔试题---最大子序列和
  • 原文地址:https://www.cnblogs.com/ERKE/p/3674962.html
Copyright © 2011-2022 走看看