这道题有特殊之处,在模拟i站起点时,走到p站走不下去了,则跳出后直接从p站继续模拟就好,中间i+1到p-1不用模拟,多余,否则TLE
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<bitset>
#include<set>
using namespace std;
typedef long long LL;
#define MAXD 100000 + 10
int main(){
int T,Case = 1;
int add[MAXD];
int cost[MAXD];
int next[MAXD];
scanf("%d",&T);
while(T--){
int n,pos;
int ok = 0;
int brk = 0;
scanf("%d",&n);
for(int i = 0 ; i < n ; i++)
next[i] = i + 1;
next[n - 1] = 0;
for(int i = 0 ; i < n ; i++)
scanf("%d",&add[i]);
for(int i = 0 ; i < n ; i++)
scanf("%d",&cost[i]);
for(int i = 0 ; i < n ;){
pos = i; /*当前位置是pos*/
int now_oil = 0;
int _ok = 1;
for(int j = pos ; ; j = next[j]){
now_oil += add[j];
now_oil -= cost[j];
if(now_oil < 0){
_ok = 0;
i = next[j];
if(i == 0 || j < pos){
brk = 1;
}
break;
}
if(next[j] == pos) break;
}
if(brk) break;
else if(_ok){
ok = 1;
break;
}
}
if(ok) printf("Case %d: Possible from station %d\n",Case++,pos + 1);
else
printf("Case %d: Not possible\n",Case++);
}
return 0;
}