[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 ...
随机推荐
- C++之初体验
#include<iostream> using namespace std; int main() { cout<<" Hello imooc "< ...
- 原生JS操作iframe里的dom
转:http://www.css88.com/archives/2343 一.父级窗口操作iframe里的dom JS操作iframe里的dom可是使用contentWindow属性,contentW ...
- Getting started with Processing 第十三章——延伸(1)
导入库: 导入库的名称为:import processing.libName.* 声音 播放声音 支持的格式:wav,aiff,mp3声明: SoundFile blip;创建:blip = new ...
- 雷林鹏分享:jQuery EasyUI 表单 - 创建树形下拉框
jQuery EasyUI 表单 - 创建树形下拉框 树形下拉框(ComboTree)是一个带有下列树形结构(Tree)的下拉框(ComboBox).它可以作为一个表单字段进行使用,可以提交给远程服务 ...
- 如何下载一个物种的全部EST序列 | NCBI | 表达序列标签
EST:表达序列标签,expressed sequence tags . 顾名思义,很好理解,就是表达出来的序列,即从基因组DNA上表达出来的RNA,但是我们没法测序RNA,所以我们最终测的是表达序列 ...
- OnSen UI结合AngularJs打造”美团"APP"订单”页面 --Hybrid App
1.页面效果图: 演示链接地址:http://www.nxl123.cn/bokeyuan/meiTuanDemo_order/ 2.核心代码 order.html: <ons-page id= ...
- LeetCode--496--下一个更大元素I(java)
给定两个没有重复元素的数组 nums1和 nums2 ,其中nums1 是 nums2 的子集.找到 nums1 中每个元素在 nums2 中的下一个比其大的值. nums1 中数字 x 的下一个更大 ...
- Canvas画板
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA6QAAAGgCAIAAAAy0z21AAAgAElEQVR4nO3dfYwkZ30n8JqZfcNex8
- Confluence 6 为边栏添加自定义内容
你可以使用 wiki 标记和自定义内容来对边栏进行更进一步的自定义. 希望添加自定义内容到你的边栏中: 进入空间后,然后从边栏的底部选择 空间工具(Space tools) > 外观和感觉(Lo ...
- MyEclipse6.5的反编译插件的安装
常用的几种反编译工具 1. JD-GUI[推荐] JD-GUI是属于Java Decompiler项目(JD项目)下个的图形化运行方式的反编译器.JD-Eclipse属于Java Decompiler ...