UVa11093

 //当汽车从第i个加油站到第j个加油站无法继续走下去的时候,这时候[i,j]区间的所有加油站都无法作为起点,因为当我们到第k个加油站的时候,起码是带着>=0的油去的,现在不带油直接从第k个开始肯定更不行了。 1 #include<bits/stdc++.h>
 2
 3 using namespace std;
 4
 5 const int maxn = 100001;
 6
 7 int t;
 8
 9 int n;
10
11 int icase;
12
13 int a[maxn+10];
14
15 int b[maxn+10];
16
17 int main(){
18         scanf("%d",&t);
19         while(t--){
20             scanf("%d",&n);
21             memset(a, 0, sizeof(a));
22             memset(b, 0, sizeof(b));
23             for(int i = 1; i <= n; ++i){
24                 scanf("%d",&a[i]);
25             }
26             for(int i = 1; i <= n; ++i){
27                 scanf("%d",&b[i]);
28             }
29             if(n == 1){
30                 if(a[1] >= b[1]){
31                     printf("Case %d: Possible from station %d\n",++icase,1);
32                 }else {
33                     printf("Case %d: Not possible\n",++icase);
34                 }
35                 continue;
36             }
37             int cur = 1;
38             int i = 1;
39             int cnt = 0;
40             int fuel = 0;
41             int flag = 0;
42             while(cnt != n){
43                 fuel += a[i];
44                 if(fuel >= b[i]){
45                     fuel -= b[i];
46                     ++i;
47                     ++cnt;
48                     if(i > n){
49                         i = 1;
50                         flag = 1;
51                     }
52                 } else {
53                     if(flag == 1){
54                         flag = 2;
55                         break;
56                     }
57                     ++i;
58                     if(i > n){
59                         i = 1;
60                         flag = 1;
61                     }
62                     cur = i;
63                     cnt = 0;
64                     fuel = 0;
65                 }
66             }
67             if(flag == 2){
68                 printf("Case %d: Not possible\n",++icase);
69             } else {
70                 printf("Case %d: Possible from station %d\n",++icase,cur);
71             }
72         }
73 }
时间: 2024-10-21 16:43:53

UVa11093的相关文章

UVa11093 Just Finish it up (模拟)

链接:http://vjudge.net/problem/UVA-11093 分析:模拟,枚举起点(0~n-1),假定从某一起点s出发,途中遇到了某个加油站p,在从它开到加油站p+1时油没了,这样以s+1,s+2,...,p为起点的一定不是解,直接将起点跳到p+1开始枚举,时间复杂度为O(n). 1 #include <cstdio> 2 3 const int maxn = 100005; 4 5 int n, p[maxn], q[maxn]; 6 7 int go(int n) { 8