Travel Around Country(贪心)

题目链接:点击跳转

Travel Around Country

Time Limit: 10000 msMemory Limit: 32768 KB

Driving a car around the country with PPMM is a one of Vivid��s Dreams. Now he is thinking about which city could be his start point, although he hasn��t got a car yet��

There are N main cities forming a loop in our country. Vivid wants to choose one city as his start point, then goes around the loop, and at last back to the city which he started from.

Of course, the car can��t finish the loop without refueling. Fortunately there always has a gas station in each city, and Vivid will take all of the oil from the gas station. You can assume the car consumes one unit of oil per one unit of distance. At the start point, the car has no oil at all, and will take all of the oil from the gas station.

Given N cities in order, with the amount of the oil it contains, and the distance to the next city. Please help Vivid to choose his start point.

Input

There are multiple test cases. Each test case begins with a Integer N (the number of the city, 2 <= N <=10000), then N lines followed with two Integers O (the amount of oil this city has) and D (the distance to the next city).

Output

Each test case has a line with a Integer, indicate which city could be the start point for Vivid. We consider the first city as 0, the second city as 1, and so on. If there is no such city can be found, just output impossible. If there are more than one city can be found, output the first city in the input.

Sample Input

2
10 10
10 10
2
10 20
20 10
2
10 20
10 20

Sample Output

0
1
impossible

解题思路:暴力从0开始枚举能不能走完即可,注意找到就要跳出

代码如下:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define N 10005
int a[N],b[N];
int main(){int n;while(scanf("%d",&n)!=EOF){for(int i=0;i<n;i++){scanf("%d %d",&a[i],&b[i]);}int ans=-1;for(int i=0;i<n;i++){if(ans!=-1)break;int o=a[i],cnt=i;for(int j=0;j<n;j++){if(o<b[cnt])break;o-=b[cnt];cnt++;if(cnt==n){cnt=0;}o+=a[cnt];if(j==n-1){ans=i;break;}}}if(ans!=-1)printf("%d\n",ans);else printf("impossible\n");}return 0;
}

ZOJ 2656 Travel Around Country相关推荐

  1. 四川第七届 I Travel(bfs)

    Travel The country frog lives in has nn towns which are conveniently numbered by 1,2,-,n1,2,-,n. Amo ...

  2. SCU 4444: Travel(最短路)

    Travel The country frog lives in has n towns which are conveniently numbered by 1,2,-,n . Among n(n− ...

  3. 【转载】图论 500题——主要为hdu/poj/zoj

    转自--http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  4. ZOJ 2913 Bus Pass (近期的最远BFS HDU2377)

    题意  在全部城市中找一个中心满足这个中心到全部公交网站距离的最大值最小 输出最小距离和满足最小距离编号最小的中心 最基础的BFS  对每一个公交网站BFS  dis[i]表示编号为i的点到全部公交网 ...

  5. hdu 2433 Travel

    http://acm.hdu.edu.cn/showproblem.php?pid=2433 题意: 求删除任意一条边后,任意两点对的最短路之和 以每个点为根节点求一个最短路树, 只需要记录哪些边在最 ...

  6. 英语口语笔记B1-Unit7.交通出行-04-Train travel in Europe

    口语笔记B1-Unit7.交通出行-04-Train travel in Europe Train travel in Europe 词汇语法及实用表达 对话场景 文化拓展 思维发散 Train tr ...

  7. CF983E NN country

    题目 展开 题目描述 In the NN country, there are nn cities, numbered from 11 to nn , and n - 1n−1 roads, conn ...

  8. POJ ZOJ题目分类

    POJ,ZOJ题目分类(多篇整合版,分类很细致,全面) 标签: 题目分类POJ整理 2015-04-18 14:44 1672人阅读 评论(0) 收藏 举报 本文章已收录于: 分类: ACM资料(5) ...

  9. POJ,ZOJ题目分类(多篇整合版,分类很细致,全面)

    水题: 3299,2159,2739,1083,2262,1503,3006,2255,3094 初级: 一.基本算法:        (1)枚举 (1753,2965)       (2)贪心(13 ...

最新文章

  1. Docker学习(五)-----Docker查看日志
  2. java mobile phone games_j2me100-src Java
  3. Hadoop + MapReduce 端口自定义配置
  4. 很近没读书了,读书笔记之大道至简
  5. linux 自动安装 get,linux下自动获取并安装软件包 apt-get 的命令介绍
  6. Hadoop初级之Hadoop基本概念与应用前景
  7. 光纤模块与光纤收发器的区别
  8. LinQ To Object 基本用法
  9. ConcurrentHashMap 总结( 上 )
  10. 【转载】spring mvc 使用session
  11. 如何入门分子动力学模拟?
  12. crontab实现定时执行脚本重启服务
  13. 脉冲响应函数方差分解Eviews
  14. bzoj3375[Usaco2004 Mar]Paranoid Cows 发疯的奶牛*
  15. [ChneChen的随笔]中国文化清单上我们还记得什么
  16. OC循环渐进:文件管理--计算文件大小的五种方式
  17. GDT(全居描述符表)和LDT(局部描述符表)
  18. AM335x启动流程(BootRom-MLO-Uboot)
  19. 不花钱一样可以引流获客?这6招功劳不小
  20. 上市即亏损?谈谈爱奇艺和拼多多上市后的艰难人生

热门文章

  1. 分享一套完整的汽车维修订单管理系统源码 带数据库文档
  2. mxnet制作.lst和.rec文件
  3. android版本5.1下载,android bench5.1版
  4. 简单的服务调用框架实现
  5. 953-验证外星语词典
  6. 爬虫python程序实例-《Python爬虫开发与项目实战》pdf完整版
  7. 达人评测 r3 5300G怎么样 性能相当于什么水平
  8. java操作derby,Java操作Derby数据库
  9. Keil5 MDK(MDK-Community edition)社区版安装教程
  10. pdf密码解开步骤,pdf权限密码怎么解开?