[Codeforces721E]Road to Home
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的更多相关文章
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- POJ 3204 Ikki's Story I - Road Reconstruction
Ikki's Story I - Road Reconstruction Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 7 ...
- 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 ...
- dp or 贪心 --- hdu : Road Trip
Road Trip Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 29 ...
- HDU 1598 find the most comfortable road(最小生成树之Kruskal)
题目链接: 传送门 find the most comfortable road Time Limit: 1000MS Memory Limit: 32768 K Description XX ...
- 三分 --- CSU 1548: Design road
Design road Problem's Link: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1548 Mean: 目的:从(0,0)到 ...
- hdu 5861 Road 两棵线段树
传送门:hdu 5861 Road 题意: 水平线上n个村子间有 n-1 条路. 每条路开放一天的价格为 Wi 有 m 天的操作,每天需要用到村子 Ai~Bi 间的道路 每条路只能开放或关闭一次. ( ...
- HDU4081 Qin Shi Huang's National Road System(次小生成树)
枚举作为magic road的边,然后求出A/B. A/B得在大概O(1)的时间复杂度求出,关键是B,B是包含magic road的最小生成树. 这么求得: 先在原图求MST,边总和记为s,顺便求出M ...
- 杭电 1595 find the safest road
find the safest road Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
随机推荐
- Creazy Ideas 智能汽车和智能交通
1.现在无人驾驶靠的是摄像头监测障碍物,计算可行路线(这个结论是看到相关视频中百度无人驾驶测试中大大的摄像头推测的,非确切信息).而非像人一样对客观事物的认知学习得到障碍物的信息,如果能将AI融入进去 ...
- java重新学习记载的一些资料。
1.ided的注册码地址. IDEA的密码:http://idea.lanyus.com/ 2.
- ip网关配置
流量查看watch more /proc/net/devip子网查询https://www.sojson.com/convert/subnetmask.htmlhttp://ip.gchao.cn/ ...
- fiddler学习笔记&&基本使用
周末在网上找了些fiddler相关的资料来看,学习下如何使用这个工具(平时接口测试用得比较多,在没有接口文档的情况下,可以通过抓包工具来提取需要测试的接口,ps.好久没写博客了,争取5月结束前再写2篇 ...
- phalcon的save方法保存失败?
phalcon的save方法保存失败? 因为表中设置了一个字段:disabled, 默认值是1, 在创建数据的时候,disabled没有传值过去,导致save方法一直是false, 当返回false时 ...
- 雷林鹏分享:XML 语法规则
XML 语法规则 XML 的语法规则很简单,且很有逻辑.这些规则很容易学习,也很容易使用. 所有的 XML 元素都必须有一个关闭标签 在 HTML 中,某些元素不必有一个关闭标签: This is a ...
- Java类成员变量的默认值
1.布尔型(boolean)变量默认值为false,byte.short.int.long为0,字符型为'\u0000'(空字符),浮点型(float double)为0.0,引用类型(String) ...
- LeetCode--371--两整数之和
问题描述: 方法: class Solution(object): def getSum(self, a, b): """ :type a: int :type b: i ...
- stylus笔记
Stylus介绍及特点 基于Node.js的css的预处理框架,其本质上做的事情与 Sass/LESS 等类似, 可以以近似脚本的方式去写CSS代码,创建健壮的.动态的.富有表现力的CSS,默认使用 ...
- windows上使用composer安装yii2
切换成中国镜像 composer config -g repo.packagist composer https://packagist.laravel-china.org 在电脑的dos命令中写入: ...