UVA-11093

从一号加油站开始,如果最多只能开到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);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值