Problem

有一条长为l的公路(可看为数轴),n盏路灯,每盏路灯有照射区间且互不重叠。

有个人要走过这条公路,他只敢在路灯照射的地方唱歌,固定走p唱完一首歌,歌曲必须连续唱否则就要至少走t才能继续唱。

问最多能唱多少首歌

Solution

贪心:对于一段照射区间要么不唱歌要么能唱多久唱多久

提早结束,后面提早开始,和延迟结束,准时开始效果是一样的

DP,f[i]表示到第 i 段为止最多能唱多少首歌,g[i]表示唱完f[i]首歌的最左的端点。

f[i]=f[j]+(r-max(l,g[j]+t))/p

g[i]=r-(r-max(l,g[j]+t))%p

这样时间复杂度是O(N^2)

我们可以发现,转移必定来源于最后一个g[j] + t <= l[i]到最后一个g[j] + t <= r[i]的j

又因为r[i] < l[i + 1],所以每次转移的第一个必定是前一个转移的最后一个或后面的j

Notice

注意当f相等时但g更小是也要更新

Code

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
const int INF = 1e9, N = 100000;
const double eps = 1e-6, phi = acos(-1.0);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
int f[N + 5], g[N + 5], Left[N + 5], Right[N + 5], ans = 0;
int sqz()
{
int l = read(), n = read(), p = read(), t = read();
rep(i, 1, n) Left[i] = read(), Right[i] = read();
int last = 0;
g[0] = -t;
rep(i, 1, n) g[i] = INF;
rep(i, 1, n)
{
while (g[last + 1] + t <= Left[i] && last < n) last++;
while (g[last] + t <= Right[i] && last <= n)
{
if (f[last] + (Right[i] - max(Left[i], g[last] + t)) / p > f[i] ||
f[last] + (Right[i] - max(Left[i], g[last] + t)) / p == f[i] &&
Right[i] - (Right[i] - max(Left[i], g[last] + t)) % p < g[i])
{
f[i] = f[last] + (Right[i] - max(Left[i], g[last] + t)) / p;
g[i] = Right[i] - (Right[i] - max(Left[i], g[last] + t)) % p;
}
last++;
}
last--;
if (f[i - 1] > f[i] || f[i - 1] == f[i] && g[i - 1] < g[i])
{
f[i] = f[i - 1];
g[i] = g[i - 1];
}
ans = max(ans, f[i]);
}
printf("%d\n", ans);
}

[Codeforces721E]Road to Home的更多相关文章

  1. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  2. POJ 3204 Ikki's Story I - Road Reconstruction

    Ikki's Story I - Road Reconstruction Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 7 ...

  3. Codeforces #380 div2 C(729C) Road to Cinema

    C. Road to Cinema time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  4. dp or 贪心 --- hdu : Road Trip

    Road Trip Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 29 ...

  5. HDU 1598 find the most comfortable road(最小生成树之Kruskal)

    题目链接: 传送门 find the most comfortable road Time Limit: 1000MS     Memory Limit: 32768 K Description XX ...

  6. 三分 --- CSU 1548: Design road

    Design road Problem's Link:   http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1548 Mean: 目的:从(0,0)到 ...

  7. hdu 5861 Road 两棵线段树

    传送门:hdu 5861 Road 题意: 水平线上n个村子间有 n-1 条路. 每条路开放一天的价格为 Wi 有 m 天的操作,每天需要用到村子 Ai~Bi 间的道路 每条路只能开放或关闭一次. ( ...

  8. HDU4081 Qin Shi Huang's National Road System(次小生成树)

    枚举作为magic road的边,然后求出A/B. A/B得在大概O(1)的时间复杂度求出,关键是B,B是包含magic road的最小生成树. 这么求得: 先在原图求MST,边总和记为s,顺便求出M ...

  9. 杭电 1595 find the safest road

    find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

随机推荐

  1. 学习笔记50—多重假设检验与Bonferroni校正、FDR校正

    总结起来就三句话: (1)当同一个数据集有n次(n>=2)假设检验时,要做多重假设检验校正 (2)对于Bonferroni校正,是将p-value的cutoff除以n做校正,这样差异基因筛选的p ...

  2. java web 方面

    1.Tomcat的优化经验. 2.http请求的GET与POST方式的区别. (1)get是从服务器上获取数据,post是向服务器传送数据. (2)get是把参数数据队列加到提交表单的ACTION属性 ...

  3. [C#]创建表格(.xlsx)的典型方法

    Time:2017-10-11   10:12:13 利用EPPlus(4.1): 下载引用地址:http://epplus.codeplex.com/ --EPPlus is a .net libr ...

  4. Django 基础介绍

    Django 介绍 Python下有许多款不同的 Web 框架.Django是重量级选手中最有代表性的一位.许多成功的网站和APP都基于Django. Django是一个开放源代码的Web应用框架,由 ...

  5. Python全栈开发-Day8-Socket网络编程

    本节内容 断言 Socket构建框架 ftp构建框架 Socket粘包 Socket介绍 Socket参数介绍 基本Socket实例 通过Socket实现简单SSH SocketServer 支持多用 ...

  6. python中列表和元组的操作(结尾格式化输出小福利)

    一. 列表 1. 查 names = "YanFeixu WuYifan" names_1 = ["YanFeixu"," WuYifan" ...

  7. 猫眼电影爬取(三):requests+pyquery,并将数据存储到mysql数据库

    还是以猫眼电影为例,这次用pyquery库进行爬取 1.简单demo,看看如何使用pyquery提取信息,并将提取到的数据进行组合 # coding: utf-8 # author: hmk impo ...

  8. JavaScript Transpilers: 为什么需要用它们?Babel的使用介绍。

    英文原文 https://scotch.io/tutorials/javascript-transpilers-what-they-are-why-we-need-them 摘译(文章内的代码有些过期 ...

  9. 搭建智能合约开发环境Remix IDE及使用

    目前开发智能的IDE, 首推还是Remix, 而Remix官网, 总是由于各种各样的(网络)原因无法使用,本文就来介绍一下如何在本地搭建智能合约开发环境remix-ide并介绍Remix的使用. 写在 ...

  10. ZCU板级调试Bug记录

    本帖用以记录在ZCU102板级调试间遇到的Bug. 1.PL端的AXI总线在读取DDR中的数据的时候,在一个burst内不能跨越page boundary.跨越page boundary会在该burs ...