UVa11093
//当汽车从第i个加油站到第j个加油站无法继续走下去的时候,这时候[i,j]区间的所有加油站都无法作为起点,因为当我们到第k个加油站的时候,起码是带着>=0的油去的,现在不带油直接从第k个开始肯定更不行了。
1 #include<bits/stdc++.h> 2 3 using namespace std; 4 5 const int maxn = 100001; 6 7 int t; 8 9 int n; 10 11 int icase; 12 13 int a[maxn+10]; 14 15 int b[maxn+10]; 16 17 int main(){ 18 scanf("%d",&t); 19 while(t--){ 20 scanf("%d",&n); 21 memset(a, 0, sizeof(a)); 22 memset(b, 0, sizeof(b)); 23 for(int i = 1; i <= n; ++i){ 24 scanf("%d",&a[i]); 25 } 26 for(int i = 1; i <= n; ++i){ 27 scanf("%d",&b[i]); 28 } 29 if(n == 1){ 30 if(a[1] >= b[1]){ 31 printf("Case %d: Possible from station %d\n",++icase,1); 32 }else { 33 printf("Case %d: Not possible\n",++icase); 34 } 35 continue; 36 } 37 int cur = 1; 38 int i = 1; 39 int cnt = 0; 40 int fuel = 0; 41 int flag = 0; 42 while(cnt != n){ 43 fuel += a[i]; 44 if(fuel >= b[i]){ 45 fuel -= b[i]; 46 ++i; 47 ++cnt; 48 if(i > n){ 49 i = 1; 50 flag = 1; 51 } 52 } else { 53 if(flag == 1){ 54 flag = 2; 55 break; 56 } 57 ++i; 58 if(i > n){ 59 i = 1; 60 flag = 1; 61 } 62 cur = i; 63 cnt = 0; 64 fuel = 0; 65 } 66 } 67 if(flag == 2){ 68 printf("Case %d: Not possible\n",++icase); 69 } else { 70 printf("Case %d: Possible from station %d\n",++icase,cur); 71 } 72 } 73 }