大体题意不说了,书上说的很清楚!
思路:
1.首先先从1开始模拟检测,当发现i = p时,sum < 0 时说明,1 到P都不行!就让i 从p+1开始继续找
直到发现结束位置比开始位置小的时候,那就说明无解应该输出-1.
因为一旦出现结束位置比开始位置小,说明1到 开始位置都不可以,开始位置到结束位置都不可以,而这两个的并集就是全集,所以1到n没有合适的位置!
否则就输出那个开始位置!
代码如下:
#include<cstdio>
const int maxn = 100000 + 10;
int p[maxn],q[maxn],n,flags,flage,ok;
bool go(int s){
flags=s;
int sum = 0;
for (int i = 0; i < n; ++i){
int key = (s+i)%n;
sum+=p[key]-q[key];
if (sum < 0){flage=key;return false;}
}
return ok=true;
}
int main()
{
int T,cnt=0;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for (int i = 0; i < n; ++i)scanf("%d",&p[i]);
for (int i = 0; i < n; ++i)scanf("%d",&q[i]);
ok = false;
for (int i = 0; i < n; ++i){
if(!go(i)){
if (flags > flage)break;
i=flage;
continue;
}break;
}
printf("Case %d: ",++cnt);
if (ok)printf("Possible from station %d\n",flags+1);
else printf("Not possible\n");
}
return 0;
}