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. 学习笔记43—Linux基础集

    1.通过窗口进入 /usr/local: 请点击computer; 通过窗口进入自己home目录:点击home即可.

  2. P481tabtenn0

    编程环境为Qt Creator 4.0.3 (Community) tabtenn0.h #ifndef TABTENN0_H #define TABTENN0_H #include <stri ...

  3. 关于video.js不错的一篇博客,

    博客地址: http://www.cnblogs.com/webenh/p/5815741.html

  4. learn python the hard way习题31~40总结以及列表的扩展知识

    Python 中的列表: 形式:[ 表示打开一个列表,中间的项目用 , 隔开,然后列表以 ] 结束. for循环 两种形式: for i in ArrayName: for i in range(0, ...

  5. 停止学习框架(Stop Learning Frameworks)

    https://www.cnblogs.com/strick/p/10161733.html

  6. canvas学习之折线图

    接着上一张柱状图讲,我们是使用折线图: import {canvasPoint} from '../../assets/js/canvas';import {basicInfo,histogramMo ...

  7. hdu-5985 概率DP

    http://acm.hdu.edu.cn/showproblem.php?pid=5985 作为队里负责动态规划的同学,做不出来好无奈啊.思考了一个下午,最好还是参考了别人的思想才写出来,数学啊!! ...

  8. apiCloud 调微信支付,调支付宝支付

    data里面的参数信息,需要从后台接口中调取,点击查看微信支付详情,https://docs.apicloud.com/Client-API/Open-SDK/wxPay 首先,需要在config.x ...

  9. php 常用设计模式demo

    <?php//__get()//__set()当对象中属性不存在时调用该魔术方法//__call()当对象中方法不存在时//__callStatic()静态方法//__string()当对象不能 ...

  10. Vue音乐项目笔记(二)

    1. Vuex https://blog.csdn.net/weixin_40814356/article/details/80347366 编写: 然后,在main.js中引入 在组件中改变stat ...