UVa11093 Just Finish it up (模拟)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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     for (int st = 0; st < n; st++) {
 9         int fuel = 0, i;
10         for (i = 0; i < n; i++) {
11             fuel += p[(st+i)%n] - q[(st+i)%n];
12             if (fuel < 0) { st += i; break; }
13         }
14         if (i == n) return st;
15     }
16     return -1;
17 }
18 
19 int main() {
20     int T;
21     scanf("%d", &T);
22     for (int kase = 1; kase <= T; kase++) {
23         scanf("%d", &n);
24         for (int i = 0; i < n; i++) scanf("%d", &p[i]);
25         for (int i = 0; i < n; i++) scanf("%d", &q[i]);
26         int ans = go(n);
27         printf("Case %d: ", kase);
28         if (ans < 0) printf("Not possible\n");
29         else printf("Possible from station %d\n", ans + 1);
30     }
31     return 0;
32 }

 

以上是关于UVa11093 Just Finish it up (模拟)的主要内容,如果未能解决你的问题,请参考以下文章

例题 8-13 UVA - 11093Just Finish it up

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

uva 11093Just Finish it up(算法效率+贪心)

UVa11093

Gym - 100623J Just Too Lucky (数位dp)

UVa 11490 Just Another Problem