Problem F: Tanks a Lot
Imagine you have a car with a very large gas tank - large enough to hold whatever amount you need.
You are traveling on a circular route on which there are a number of gas stations. The total gas in all
the stations is exactly the amount it takes to travel around the circuit once. When you arrive at a gas
station, you add all of that station’s gas to your tank. Starting with an empty tank, it turns out there
is at least one station to start, and a direction (clockwise or counter-clockwise) where you can make it
around the circuit. (On the way home, you might ponder why this is the case - but trust us, it is.)
Given the distance around the circuit, the locations of the gas stations, and the number of miles your
car could go using just the gas at each station, find all the stations and directions you can start at and
make it around the circuit.
Input
There will be a sequence of test cases. Each test case begins with a line containing two positive integers
c and s, representing the total circumference, in miles, of the circle and the total number of gas stations.
Following this are s pairs of integers t and m. In each pair, t is an integer between and c− measuring
the clockwise location (from some arbitrary fixed point on the circle) around the circumference of one
of the gas stations and m is the number of miles that can be driven using all of the gas at the station.
All of the locations are distinct and the maximum value of c is ,. The last test case is followed
by a pair of ’s.
Output
For each test case, print the test case number (in the format shown in the example below) followed by a
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,
indicating that, when starting with an empty tank, it is possible to drive from location i around in a
clockwise (C) direction, counterclockwise (CC) direction, or either direction (CCC), returning to location
i. List the stations in order of increasing location.
Sample Input Sample Output
Case : C CC C
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数组。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long ll;
const int N = 5e5 + ; int sum1[N], sum2[N];
int dir[N];
int dp[N][];
int mm[N];
int n, k; struct node {
int t, m;
friend bool operator < (node a, node b) {
return a.t < b.t;
};
};
node g[N];
void initRMQ(int nn, int b[]) {
mm[] = -;
for(int i = ; i <= nn; ++i)
{
mm[i] = ((i & (i - )) == ) ? mm[i - ] + : mm[i - ];
dp[i][] = b[i];
}
for(int j = ; j <= mm[nn]; ++j)
for(int i = ; i + ( << j) - <= nn; ++i)
dp[i][j] = min(dp[i][j - ], dp[i + ( << (j-))][j - ]);
}
int rmq(int x, int y) {
int k = mm[y - x + ];
return min(dp[x][k], dp[y - ( << k) + ][k]);
}
void init1() {
int T = k * ;
memset(dir, , sizeof dir);
sum1[] = ;
for(int i = ; i <= T; ++i) {
int s = (g[i].t - g[i - ].t + n) % n;
sum1[i - ] = sum1[i - ] + g[i-].m - s;
}
initRMQ(T, sum1);
}
void init2() {
int T = k * ;
sum2[] = sum2[T + ] = ;
for(int i = T; i > ; --i) {
int s = (g[i].t - g[i - ].t + n) % n;
sum2[i] = sum2[i + ] + g[i].m - s;
}
initRMQ(T, sum2); }
void solve1() {
for(int i = ; i <= k; ++i) {
if(sum1[i - ] <= rmq(i, i + k - )) {
dir[ g[i].t ] = ;
}
}
}
void solve2() {
int T = k * ;
for(int i = T; i > T - k; --i) {
if(sum2[i + ] <= rmq(i - k + , i)) {
if(dir[ g[i].t ]) dir[ g[i].t ] = ;
else dir[ g[i].t ] = ;
}
}
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif
int cas = ;
while(~scanf("%d%d", &n, &k)) {
if(n == && k == ) break;
for(int i = ; i <= k; ++i) {
scanf("%d%d", &g[i].t, &g[i].m);
}
sort(g + , g + + k);
for(int i = ; i <= k; ++i) {
g[i + k].t = g[i].t;
g[i + k].m = g[i].m;
}
// for(int i = 1; i <= k * 2; ++i) printf("%d %d\n", g[i].t, g[i].m);
init1();
solve1();
init2();
solve2();
printf("Case %d: ", cas++);
for(int i = ; i <= k; ++i) {
if(dir[ g[i].t ] == ) printf("%d C ", g[i].t);
if(dir[ g[i].t ] == ) printf("%d CC ", g[i].t);
if(dir[ g[i].t ] == ) printf("%d CCC ", g[i].t);
}
puts("");
}
return ;
}

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. mysql 快速简单安装法

    网上下载的编译好的包 最好安装在 /usr/local 目录下面: 我用的mysql的版本的是:mysql--linux-i686-icc-glibc23.tar.gz 在官网上就可以下载到. 先期工 ...

  2. manacher算法专题

    一.模板 算法解析:http://www.felix021.com/blog/read.php?2040 *主要用来解决一个字符串中最长回文串的长度,在O(n)时间内,线性复杂度下,求出以每个字符串为 ...

  3. JS 初级(三)接上

    传送门 http://www.cnblogs.com/Sabo-dudu/p/5788197.html 现阶段我就了解了这么多,在以后的学习中,我会不断的更新,如果有什么不同的见解可以一块学习,谁有更 ...

  4. 《C程序设计语言》- 字符输入和输出

    书籍介绍: 本书是机械工业出版社的第2版·新版,作者两位,其中一位是C语言之父Dennis Ritchie,另一位是Brian Kernighan,也是一位牛人. 本书的目的是帮助读者学习如何用C语言 ...

  5. 微信小程序常见问题集合(长期更新)

    最新更新: 新手跳坑系列:推荐阅读:<二十四>request:fail错误(含https解决方案)(真机预览问题 跳坑指南<七十>如何让微信小程序服务类目审核通过 跳坑六十九: ...

  6. ng-repeat 嵌套访问父作用域里的属性

    在一个项目中,需要嵌套循环输出一个二维表的里的数据 数据结构 [ { id:1, list:[ { id:1, name:'li' } ] }, { id:2, list:[ { id:1, name ...

  7. IE7 自动为文件路径添加域名

    对于图片等文件的路径,一般在同一个域名下的文件都会使用相对路径,但如果使用JS获取文件的路径浏览器获取到的路径都是相对路径,但IE7会自动为路径添加域名变成绝对路径... IE7下图片路径,在文件相对 ...

  8. centos 编程环境

    1,老毛桃/大白菜, iso制作将镜像文件写入u盘2, 安装,修改安装源路径 (手动修改为你的u盘dev)一般为sdb43,   安装时选择桌面安装 4, 更改安装源cd /etc/yum.repos ...

  9. 码途有道----基于系统观的核心能力构建-by-韩宏老师

    原文链接:http://blog.sina.com.cn/s/blog_7d5a09f90102v341.html 有感于同学们在大学中如何学习计算机技术有些感概,将我书(老码识途)中的序言整理了一下 ...

  10. Python 2/3 安装与运行环境设置

    Python 2/3 安装与运行环境设置: 1.Python 软件源:https://www.python.org/    下载Win版本 https://www.python.org/downloa ...