题意:环形跑道上有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(环形跑道)(模拟)的更多相关文章

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

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

  2. UVa 11093 Just Finish it up

    从第一个加油站开始枚举起点,如果到第i个加油站油量不够的话,那么1~i个加油站都不可能是起点. 将第i+1个加油站作为起点继续枚举. 比如说,第一个加油站开始最多跑到第5个加油站,那么第二个加油站不可 ...

  3. UVa 11093 环形跑道(模拟)

    https://vjudge.net/problem/UVA-11093 题意:环形跑道上有n个加油站,编号为1~n.第i个加油站可以加油pi加仑,从加油站i开到下一站需要qi加仑汽油.输出最小的起点 ...

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

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

  5. Just Finish it up UVA - 11093

    Just Finish it up Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [Sub ...

  6. 【例题 8-13 UVA - 11093】Just Finish it up

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 尺取法. 假设现在取[l..r]这一段. 然后发现累加的和小于0了. 那么方法只能是不走l..l+1这一段了 即delta递减(p[ ...

  7. 紫书 例题8-13 UVa 11093 (反证法)

    这道题发现一个性质就解决了 如果以i为起点, 然后一直加油耗油, 到p这个地方要去p+1的时候没油了, 那么i, i+1, --一直到p, 如果以这些点 为起点, 肯定也走不完. 为什么呢? 用反证法 ...

  8. uva 133 The Dole Queue 双向约瑟夫环 模拟实现

    双向约瑟夫环. 数据规模只有20,模拟掉了.(其实公式我还是不太会推,有空得看看) 值得注意的是两个方向找值不是找到一个去掉一个,而是找到后同时去掉. 还有输出也很坑爹! 在这里不得不抱怨下Uva的o ...

  9. linux环形buff模拟多线程信号量操作

    互斥锁mutex变量的值非0即1,只能用来表示两种状态下的临界资源.而信号量是与之类似的,用来表示可用资源的,区别在于,信号量可以表示多个可用资源的. --值为2的信号量也就是特殊的互斥锁了. 那么下 ...

随机推荐

  1. 700k把web端程序包装为桌面程序

    electron因为自带cef所以体积巨大,还不是因为windows没有chromium的webview嘛,现在有了新edge后,这个项目通过依赖各个平台的webview,并依赖.net core,做 ...

  2. 085、Java数组之实现排序

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  3. python2中新式类和经典类的多重继承调用顺序

    class A: def foo(self): print('called A.foo()') class B(A): pass class C(A): def foo(self): print('c ...

  4. leetcode206 Reverse Linked List

    """ Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL ...

  5. 三 SVN权限设置&用户&组

    创建组,添加用户之后进行权限的设置:

  6. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:"text-primary" 类的文本样式

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. HiBench成长笔记——(8) 分析源码workload_functions.sh

    workload_functions.sh 是测试程序的入口,粘连了监控程序 monitor.py 和 主运行程序: #!/bin/bash # Licensed to the Apache Soft ...

  8. 新闻网大数据实时分析可视化系统项目——19、Spark Streaming实时数据分析

    1.Spark Streaming功能介绍 1)定义 Spark Streaming is an extension of the core Spark API that enables scalab ...

  9. 虚拟机上安装SVN服务

    服务器端安装SVN(centos)1.yum install subversion2.svn的相关配置1创建一个SVN仓库(所有项目存放与管理)mkdir -p /svndata/projects2. ...

  10. PHPstudy2018 后门简单使用

    首先声明,仅为记录使用. 测试用例php-5.4.45 + Apache index.php 使用Firefox 浏览器,可以编辑并且重发http请求 打印出“net user” base64 加密后 ...