https://vjudge.net/problem/UVA-11093

题意:环形跑道上有n个加油站,编号为1~n。第i个加油站可以加油pi加仑,从加油站i开到下一站需要qi加仑汽油。输出最小的起点,使得可以走完一圈后回到起点。

思路:直接枚举。注意剪枝就可以了,如从1号加油站出发,开到加油站p前没油了,那么2,3,4...p-1肯定不是解。

 #include<iostream>
using namespace std; const int maxn = + ;
int n;
int ans;
int p[maxn];
int q[maxn]; bool solve()
{
int pas;
int ok = ;
for (int i = ; i < n; i++)
{
pas = ;
for (int j = ; j <= n; j++)
{
int t = (i + j) % (n + );
pas += p[t];
pas -= q[t];
if (pas < )
{
if (t>=i) { i = t; break; } //t还没有作为过起点
else return false; //t已经作为过起点
}
if (j == n && pas >= ) ok = ;
}
if (ok) { ans = i; return true; }
}
return false;
} int main()
{
//freopen("D:\\txt.txt", "r", stdin);
int t, kase = ;
cin >> t;
while (t--)
{
cin >> n;
for (int i = ; i < n; i++)
cin >> p[i];
for (int i = ; i < n; i++)
cin >> q[i];
if (solve()) cout << "Case " << ++kase << ": Possible from station " << ans+ << endl;
else cout << "Case " << ++kase << ": Not possible" << endl;
}
return ;
}

UVa 11093 环形跑道(模拟)的更多相关文章

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

    题意:环形跑道上有n(n <= 100000)个加油站,编号为1~n.第i个加油站可以加油pi加仑.从加油站i开到下一站需要qi加仑汽油.你可以选择一个加油站作为起点,起始油箱为空(但可以立即加 ...

  2. UVA 11093 Just Finish it up 环形跑道 (贪心)

    有一个环形跑道,上面有n个加油站,到i号加油站可以加pi的油,跑到下一站要花费qi的油,起点任意选,问是否有一个起点可跑完整个跑道. 从i开始跑,如果遇到某个站j不能跑了,那么从i到j之间的站开始跑, ...

  3. 【uva 11093】Just Finish it up(算法效率--贪心)

    题意:环形跑道上有N个加油站,编号为1~N.第 i 个加油站可以加油Ai加仑,从加油站 i 开到下一站需要Bi加仑汽油.问可作为起点走完一圈后回到起点的最小加油站编号. 解法:我们把每个加油站的Ai, ...

  4. UVa 11210 - Chinese Mahjong 模拟, 枚举 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  5. UVA 1156 - Pixel Shuffle(模拟+置换)

    UVA 1156 - Pixel Shuffle 题目链接 题意:依据题目中的变换方式,给定一串变换方式,问须要运行几次才干回复原图像 思路:这题恶心的一比,先模拟求出一次变换后的相应的矩阵,然后对该 ...

  6. UVA 12050 - Palindrome Numbers 模拟

    题目大意:给出i,输出第i个镜像数,不能有前导0. 题解:从外层开始模拟 #include <stdio.h> int p(int x) { int sum, i; ;i<=x;i+ ...

  7. Uva 679 Dropping Balls (模拟/二叉树的编号)

    题意:有一颗二叉树,深度为D,所有节点从上到下从左到右编号为1,2,3.....在结点一处放一个小球,每个节点是一个开关,初始全是关闭的,小球从顶点落下,小球每次经过开关就会把它的状态置反,现在问第k ...

  8. ●UVa 1589 Xiangqi(模拟)

    ●赘述题意 给出一个中国象棋残局,告诉各个棋子的位置,黑方只有1枚“将”,红方有至少2枚,至多7枚棋子,包含1枚“帅G”,和若干枚“车R”,“马H”,“炮C”.当前为黑方的回合,问黑方的“将”能否在移 ...

  9. 【每日一题】 UVA - 213 Message Decoding 模拟解码+读入函数+阅读题

    题意:阅读理解难度一道比一道难orz.手摸了好久样例 题解: 读入:大循环用getline读入header顺便处理一下,  里面再写两重循环,外层一次读三个串,内层一次读num个串. 之后就查表,线性 ...

随机推荐

  1. Tomcat重启session失效

    在Tomcat的目录下找到context.xml,取消掉<Manager pathname="" /> 这句的注释.

  2. [LeetCode] 207 Course Schedule_Medium tag: BFS, DFS

    There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prereq ...

  3. [LeetCode] 256. Paint House_Easy tag: Dynamic Programming

    There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...

  4. MatLab 2014a编译jar包时mcc无法使用的问题

    http://blog.csdn.net/heroafei/article/details/43273373 MatLab 2014a编译jar包时mcc无法使用的问题 2015-01-29 16:5 ...

  5. Leetcode: Binary Tree Postorder Transversal

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  6. Docker深入浅出3-容器管理

    docker客户端非常简单,我们可以直接输入docker命令来查看Docker客户端所有的命令项 [root@admin-fxr ~]# docker Usage: docker COMMAND A ...

  7. 剑指offer3

    输入一个链表,从尾到头打印链表每个节点的值. 思路:首先借助一个栈,遍历链表中的每一个值,然后存储到栈中,利用栈的先进后出特点,然后添加到数组中返回. package demo3; import ja ...

  8. nginx之rewrite重写,反向代理,负载均衡

    rewrite重写(伪静态): 在地址栏输入xx.com/user-xxx.html, 实际上访问的就是xxx.com/user.php?id=xxx rewrite就这么简单 附上ecshop re ...

  9. VNC的安装和常用命令

    主要参考文章:http://www.cnblogs.com/coderzh/archive/2008/07/16/1243990.html                         http:/ ...

  10. java android 环境变量配置备忘录

    java android 环境变量配置备忘录,全新重装系统要用到,备忘下来; JAVA_HOME D:\Java\jdk1.6.0_07 CLASSPATH  .;%JAVA_HOME%\lib\dt ...