1. Problem F: Tanks a Lot
  2. Imagine you have a car with a very large gas tank - large enough to hold whatever amount you need.
  3. You are traveling on a circular route on which there are a number of gas stations. The total gas in all
  4. the stations is exactly the amount it takes to travel around the circuit once. When you arrive at a gas
  5. station, you add all of that stations gas to your tank. Starting with an empty tank, it turns out there
  6. is at least one station to start, and a direction (clockwise or counter-clockwise) where you can make it
  7. around the circuit. (On the way home, you might ponder why this is the case - but trust us, it is.)
  8. Given the distance around the circuit, the locations of the gas stations, and the number of miles your
  9. car could go using just the gas at each station, find all the stations and directions you can start at and
  10. make it around the circuit.
  11. Input
  12. There will be a sequence of test cases. Each test case begins with a line containing two positive integers
  13. c and s, representing the total circumference, in miles, of the circle and the total number of gas stations.
  14. Following this are s pairs of integers t and m. In each pair, t is an integer between and c measuring
  15. the clockwise location (from some arbitrary fixed point on the circle) around the circumference of one
  16. of the gas stations and m is the number of miles that can be driven using all of the gas at the station.
  17. All of the locations are distinct and the maximum value of c is ,. The last test case is followed
  18. by a pair of s.
  19. Output
  20. For each test case, print the test case number (in the format shown in the example below) followed by a
  21. list of pairs of values in the form i d, where i is the gas station location and d is either C, CC, or CCC,
  22. indicating that, when starting with an empty tank, it is possible to drive from location i around in a
  23. clockwise (C) direction, counterclockwise (CC) direction, or either direction (CCC), returning to location
  24. i. List the stations in order of increasing location.
  25. Sample Input
  26.  
  27. Sample Output
  28. Case : C CC C
  29. Case : CCC CCC CCC CCC CCC

题意:给你一个长度为c的环,环上面有m个加油站,各个加油站油的总和刚好够你在环上面跑一圈,一开始你的车没有油,现在你可以选一个加油站作为出发点并获得该加油站的所有油,然后选择顺时针或逆时针行走,没经过一个加油站你可以获得那里的油,问是否可以最终回到选择为起始的那个加油站。 这m个加油站有哪些是可以作为起始点的,可以的话应该顺时针出发还是逆时针出发还是两个方向都可以?

思路:对于环可以头尾接一遍变成直线,将加油站按顺序排列。设need_i为只用第i个加油站的油到第i+1个加油站还需要的油量, 比如1(2)---》 5(3)---》7(1)  那么从油站1到油站5还欠了2, 即need为-2, 从油站5到油站7多了2,即need为2

那么对于第i个加油站能作为起点,相当于从i开始的need数组的前缀和不能为负数。 那么我们可以直接做一遍前缀和,然后对于一个区间[l,r],要想以l位置为起点的前缀和在这个区间没有负数,相当于sum[l-1]<=min(sum[k])  l<=k<=r  相当于这个区间的值都减去sum[l-1]  查询区间最小值可以用rmq处理

对于逆时针方向 做法就完全一样了,逆着求一下need数组。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <queue>
  5. #include <vector>
  6. #include <string>
  7. #include <stack>
  8. #include <cmath>
  9. #include <cstdlib>
  10. #include <iostream>
  11. #include <map>
  12. #include <set>
  13. using namespace std;
  14. const int INF = 0x3f3f3f3f;
  15. typedef long long ll;
  16. const int N = 5e5 + ;
  17.  
  18. int sum1[N], sum2[N];
  19. int dir[N];
  20. int dp[N][];
  21. int mm[N];
  22. int n, k;
  23.  
  24. struct node {
  25. int t, m;
  26. friend bool operator < (node a, node b) {
  27. return a.t < b.t;
  28. };
  29. };
  30. node g[N];
  31. void initRMQ(int nn, int b[]) {
  32. mm[] = -;
  33. for(int i = ; i <= nn; ++i)
  34. {
  35. mm[i] = ((i & (i - )) == ) ? mm[i - ] + : mm[i - ];
  36. dp[i][] = b[i];
  37. }
  38. for(int j = ; j <= mm[nn]; ++j)
  39. for(int i = ; i + ( << j) - <= nn; ++i)
  40. dp[i][j] = min(dp[i][j - ], dp[i + ( << (j-))][j - ]);
  41. }
  42. int rmq(int x, int y) {
  43. int k = mm[y - x + ];
  44. return min(dp[x][k], dp[y - ( << k) + ][k]);
  45. }
  46. void init1() {
  47. int T = k * ;
  48. memset(dir, , sizeof dir);
  49. sum1[] = ;
  50. for(int i = ; i <= T; ++i) {
  51. int s = (g[i].t - g[i - ].t + n) % n;
  52. sum1[i - ] = sum1[i - ] + g[i-].m - s;
  53. }
  54. initRMQ(T, sum1);
  55. }
  56. void init2() {
  57. int T = k * ;
  58. sum2[] = sum2[T + ] = ;
  59. for(int i = T; i > ; --i) {
  60. int s = (g[i].t - g[i - ].t + n) % n;
  61. sum2[i] = sum2[i + ] + g[i].m - s;
  62. }
  63. initRMQ(T, sum2);
  64.  
  65. }
  66. void solve1() {
  67. for(int i = ; i <= k; ++i) {
  68. if(sum1[i - ] <= rmq(i, i + k - )) {
  69. dir[ g[i].t ] = ;
  70. }
  71. }
  72. }
  73. void solve2() {
  74. int T = k * ;
  75. for(int i = T; i > T - k; --i) {
  76. if(sum2[i + ] <= rmq(i - k + , i)) {
  77. if(dir[ g[i].t ]) dir[ g[i].t ] = ;
  78. else dir[ g[i].t ] = ;
  79. }
  80. }
  81. }
  82. int main() {
  83. #ifdef LOCAL
  84. freopen("in.txt", "r", stdin);
  85. #endif
  86. int cas = ;
  87. while(~scanf("%d%d", &n, &k)) {
  88. if(n == && k == ) break;
  89. for(int i = ; i <= k; ++i) {
  90. scanf("%d%d", &g[i].t, &g[i].m);
  91. }
  92. sort(g + , g + + k);
  93. for(int i = ; i <= k; ++i) {
  94. g[i + k].t = g[i].t;
  95. g[i + k].m = g[i].m;
  96. }
  97. // for(int i = 1; i <= k * 2; ++i) printf("%d %d\n", g[i].t, g[i].m);
  98. init1();
  99. solve1();
  100. init2();
  101. solve2();
  102. printf("Case %d: ", cas++);
  103. for(int i = ; i <= k; ++i) {
  104. if(dir[ g[i].t ] == ) printf("%d C ", g[i].t);
  105. if(dir[ g[i].t ] == ) printf("%d CC ", g[i].t);
  106. if(dir[ g[i].t ] == ) printf("%d CCC ", g[i].t);
  107. }
  108. puts("");
  109. }
  110. return ;
  111. }

Gym 100646 F Tanks a Lot RMQ的更多相关文章

  1. Gym 100646 You’ll be Working on the Railroad dfs

    You'll be Working on the Railroad 题目连接: http://codeforces.com/gym/100646/attachments Description Con ...

  2. Gym 100646 Problem C: LCR 模拟题

    Problem C: LCR 题目连接: http://codeforces.com/gym/100646/attachments Description LCR is a simple game f ...

  3. Gym 100646 Problem E: Su-Su-Sudoku 水题

    Problem E: Su-Su-Sudoku/center> 题目连接: http://codeforces.com/gym/100646/attachments Description By ...

  4. Gym 100637F F. The Pool for Lucky Ones

    F. The Pool for Lucky Ones Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  5. codeforces Gym 100187F F - Doomsday 区间覆盖贪心

    F. Doomsday Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/F ...

  6. Codeforces gym 100685 F. Flood bfs

    F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Desc ...

  7. Gym 100637F F. The Pool for Lucky Ones 暴力

    F. The Pool for Lucky Ones Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  8. Codeforces Gym 100513F F. Ilya Muromets 线段树

    F. Ilya Muromets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/probl ...

  9. Codeforces Gym 100513F F. Ilya Muromets 水题

    F. Ilya Muromets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/probl ...

随机推荐

  1. Redis-3.2.6 配置文件中文翻译

    ############## # 指定配置文件: ################################## INCLUDES ############################### ...

  2. 在Azure虚拟机上安装VNC

    我们知道,Azure提供的linux虚拟机镜像是没有桌面的,是base版的,大多情况下能满足绝大部分工作需要,甚至很多习惯使用命令行的读者,反而用不惯带桌面的linux OS,但是有些情况下,桌面还是 ...

  3. .NET中的异步

    .NET中4种异步方式? ThreadPool.QueueUserworkItem实现 APM模式(就是BeginXXX和EndXXX成对出现.) EAP模式(就是Event based, 准确说来就 ...

  4. MySQL 磁盘I/O问题

    一.使用磁盘阵列:RAID,廉价磁盘冗余阵列,可靠性,性能好. 二.使用 Symbolic Links 分布I/O 利用操作系统的符号链接将不同的数据库或表.索引指向不同的物理磁盘,达到分布磁盘I/O ...

  5. 项目vue2.0仿外卖APP(三)

    项目的结构如下:                   项目资源准备 准备项目的各种图片资源等等 注意:在webpack可以不用css sprite,直接用单张图片,因为它会帮忙打包. 还有SVG图片, ...

  6. python中单引号, 双引号,三引号的差异

    1. 单引号和双引号用法都是一样的,但是如果字符串里有相同的字符时要使用\进行转义 举例:1) print 'hello'2) print "hello"1和2,结果都是hello ...

  7. links and softwares

    links 普通 http://www.ncpa-classic.com//special/2014gejujie/index.shtml ; 中国大剧院 http://tieba.baidu.com ...

  8. PHP 链接多种数据库 的方法

    数据库中  单词之间的空格(一个语句前面和后面做字符串拼接的时候最好留空格 )  可以随便加   其他地方  禁止随便加空格!!(加了 就报错)! =====================总结=== ...

  9. ASP.NET获取百度地图提供的API接口里面的JSON

    思路:开始是想直接在前台获取,但是跨域访问还是有点难度,而且格式必须是josnp格式的,最后嫌麻烦,不得已放弃. 我做的ASP.NET  而这个有自带的解析类,直接引用就行了 先在后台获取到JOSN: ...

  10. fabric

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 # fab test   [root@192.168.85.99:22] Executing ...