题目链接:https://vjudge.net/problem/UVA-11093
直接模拟,枚举出发点,要点是在枚举过程中无法通过P点,则1,2,3...P这些点都不能作为起点,直接跳过就可以
这样时间复杂度为O(n)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
const int N=1e5+10;
int a[N],b[N];
int n;
int main()
{
//freopen("/home/zlwang/C++/t2.txt","r",stdin);
int T,kase=0;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
for(int i=0;i<n;i++)
scanf("%d",&b[i]);
ll g;
int p,p0=0;
bool flag;
while(1)
{
flag=false;
p=p0;
g=a[p];
while(g>=b[p])
{
g-=b[p];
p++;
if(p>=n) p=0;
if(p==p0)
{
flag=true;
break;
}
g+=a[p];
}
if(flag) break;
else if(p<p0) break;
p0=p+1;
}
printf("Case %d: ",++kase);
if(flag) printf("Possible from station %d\n",p0+1);
else printf("Not possible\n");
}
return 0;
}