从一号加油站开始,如果最多只能开到P号加油站,那么1,2,3…p号都不能作为起点。那么,从P+1号继续模拟。
实现的时候,有两个细节需要注意。
1.循环的时候
pos++;
if(pos==n){
pos=0;
}
这两句必须在一起,不然会越界。
2.
pos是从temp开始,走到能走到的最后一个。如果pos<temp,代表已经走过了0处,则temp,temp+1…n-1,都不行,那么这种情况就是不可能情况。所以,这个条件也是判断结束的标志。如果不用这个条件,则会死循环。
if(temp>pos){
break;
}
#include<bits/stdc++.h>
using namespace std;
const int mxn=100000+5;
int has,need[mxn],oil[mxn],n;
bool can(int &pos){
int start=pos;
has = oil[pos];
while(has>=need[pos]){
has-=need[pos];
pos++;
if(pos==n){
pos=0;
}
has+=oil[pos];
if(pos==start){
return true;
}
}
return false;
}
int main(void){
int t;
cin>>t;
for(int qwe=1;qwe<=t;qwe++){
cin>>n;
for(int i=0;i<n;i++){
scanf("%d",&oil[i]);
}
for(int i=0;i<n;i++){
scanf("%d",&need[i]);
}
has=0;
int pos=0;
int flag=0;
for(pos=0;pos<n;){
int temp=pos;
has=0;
has+=oil[temp];
flag = can(pos);
if(flag){
break;
}
else{
has=0;
if(temp>pos){
break;
}
else{
pos++;
}
}
}
if(flag){
printf("Case %d: Possible from station %d\n",qwe,pos+1);
}
else{
printf("Case %d: Not possible\n",qwe);
}
}
}