首页 > 代码库 > UVA - 11093 Just Finish it up(环形跑道)(模拟)

UVA - 11093 Just Finish it up(环形跑道)(模拟)

题意:环形跑道上有n(n <= 100000)个加油站,编号为1~n。第i个加油站可以加油pi加仑。从加油站i开到下一站需要qi加仑汽油。你可以选择一个加油站作为起点,起始油箱为空(但可以立即加油)。你的任务是选择一个起点,使得可以走完一圈后回到起点。假定油箱中的油量没有上限。如果无解,输出Not possible,否则输出可以作为起点的最小加油站编号。

分析:如果从加油站st开始,一直到加油站id油没了,说明id之前的加油站都不可以作为起点。枚举并验证所有起点即可。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int MAXN = 100000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int a[MAXN], b[MAXN];
int tot, n, st;
bool solve(){
    for(int i = 0; i < n; ++i){//枚举起始点
        st = i;
        int id = i;
        int tot = 0;
        while(tot + a[id] >= b[id]){
            tot = tot + a[id] - b[id];
            ++id;
            if(id >= n) id -= n;
            if(id == st) return true;//循环一圈,可作为起点
        }
        i = id;//证明第id之前的加油站都不能作为起点
        if(i < st) return false;//st一直到最后的加油站都判断过了,都不能作为起点
    }
    return false;
}
int main(){
    int T;
    scanf("%d", &T);
    int kase = 0;
    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]);
        printf("Case %d: ", ++kase);
        if(solve()){
            printf("Possible from station %d\n", st + 1);
        }
        else{
            printf("Not possible\n");
        }
    }
    return 0;
}

  

 

UVA - 11093 Just Finish it up(环形跑道)(模拟)