假设当前从p点开始进行判断,如果仅仅只能走到q点,那么我们下次就从q+1点开始进行模拟与判断,因为从p点到后续的点如果可达,那么到达后续的点的油量>=0,此时如果从这些点都不能继续向后推进,那么再次判断油量为0的情况就没意义了,也就是重复判断了,具体实现见如下代码:
#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<deque>
#include<functional>
using namespace std;
int main(){
int T;
cin >> T;
for (int Case = 1; Case <= T; Case++){
int n;
cin >> n;
vector<int> p;
vector<int> q;
for (int i = 0; i < n; i++){
int t;
cin >> t;
p.push_back(t);
}
for (int i = 0; i < n; i++){
int t;
cin >> t;
q.push_back(t);
}
int startID = 0;
while (startID < n){
int start = startID;
int res = 0, step;
for (step = 0; step < n; step++){
if (res + p[start] - q[start] < 0){
break;
}
else{
res += p[start] - q[start];
start = (start + 1) % n;
}
}
if (step == n){
break;
}
else{
startID += step+1;
}
}
cout << "Case " << Case << ":";
if (startID < n){
cout <<" Possible from station "<< startID + 1<<endl;
}
else{
cout << " Not possible\n";
}
}
return 0;
}