codeforces545C
Woodcutters
Little Susie listens to fairy tales before bed every day. Today's fairy tale was about wood cutters and the little girl immediately started imagining the choppers cutting wood. She imagined the situation that is described below.
There are n trees located along the road at points with coordinates x1, x2, ..., xn. Each tree has its height hi. Woodcutters can cut down a tree and fell it to the left or to the right. After that it occupies one of the segments [xi - hi, xi] or [xi;xi + hi]. The tree that is not cut down occupies a single point with coordinate xi. Woodcutters can fell a tree if the segment to be occupied by the fallen tree doesn't contain any occupied point. The woodcutters want to process as many trees as possible, so Susie wonders, what is the maximum number of trees to fell.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of trees.
Next n lines contain pairs of integers xi, hi (1 ≤ xi, hi ≤ 109) — the coordinate and the height of the і-th tree.
The pairs are given in the order of ascending xi. No two trees are located at the point with the same coordinate.
Output
Print a single number — the maximum number of trees that you can cut down by the given rules.
Examples
5
1 2
2 1
5 10
10 9
19 1
3
5
1 2
2 1
5 10
10 9
20 1
4
Note
In the first sample you can fell the trees like that:
- fell the 1-st tree to the left — now it occupies segment [ - 1;1]
- fell the 2-nd tree to the right — now it occupies segment [2;3]
- leave the 3-rd tree — it occupies point 5
- leave the 4-th tree — it occupies point 10
- fell the 5-th tree to the right — now it occupies segment [19;20]
In the second sample you can also fell 4-th tree to the right, after that it will occupy segment [10;19].
sol:dp还是挺明显的吧,同一个点有3种状态[0/1/2] 向左倒,向右倒,不动,O(1)转移即可
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n,dp[N][];
struct Shu
{
int Weiz,Gao;
}Tree[N];
int main()
{
int i;
R(n);
for(i=;i<=n;i++)
{
R(Tree[i].Weiz); R(Tree[i].Gao);
}
dp[][]=dp[][]=; dp[][]=;
for(i=;i<=n;i++)
{
dp[i][]=max(max(dp[i-][],dp[i-][]),dp[i-][]);
if(Tree[i].Weiz-Tree[i].Gao>Tree[i-].Weiz) dp[i][]=max(dp[i-][],dp[i-][])+;
if(Tree[i].Weiz-Tree[i].Gao>Tree[i-].Weiz+Tree[i-].Gao) dp[i][]=max(dp[i][],dp[i-][]+);
if((i==n)||(Tree[i].Weiz+Tree[i].Gao<Tree[i+].Weiz)) dp[i][]=dp[i][]+;
}
Wl(max(max(dp[n][],dp[n][]),dp[n][]));
return ;
}
/*
Input
5
1 2
2 1
5 10
10 9
19 1
Output
3 Input
5
1 2
2 1
5 10
10 9
20 1
Output
4
*/
codeforces545C的更多相关文章
- Codeforces 刷水记录
Codeforces-566F 题目大意:给出一个有序数列a,这个数列中每两个数,如果满足一个数能整除另一个数,则这两个数中间是有一条边的,现在有这样的图,求最大联通子图. 题解:并不需要把图搞出来, ...
随机推荐
- 记一次项目上线后Log4j2不输出日志的坑
公司项目采用了Log4j2来输出日志,在开发环境和测试环境下均可以输出日志,但在生成环境就没有日志输出.开始毫无头绪,后来通过不断的排查,终于解决了这个问题.在此记录下该问题的解决过程,便于后 ...
- UIWindow 官方文档解析
UIWindow定义了一个window对象,其用于管理和协调一个app在设备屏幕上的显示.除非一个app能在外部设备上显示内容,一般就只有一个window. window的主要功能:1)提供一个区域来 ...
- Roslyn入门(二)-C#语义
先决条件 Visual Studio 2017 .NET Compiler Platform SDK Rosyln入门(一)-C#语法分析 简介 今天,Visual Basic和C#编译器是黑盒子:输 ...
- Node+GitLab实现小程序CI系统
为什么要实现自动部署 小程序开发迭代里,有以下几个个头痛的问题, 如何准确并快速的的把小程序上传去后台,并让测试人员进行测试? 测试同事找开发要二维码,效率较低 本地生成的二维码会出现携带本地代码.未 ...
- 通过 Systemd Journal 收集日志
随着 systemd 成了主流的 init 系统,systemd 的功能也在不断的增加,比如对系统日志的管理.Systemd 设计的日志系统好处多多,这里笔者就不再赘述了,本文笔者主要介绍 syste ...
- H5 audio标签
37-audio标签 注意点: audio标签的使用和video标签的使用基本一样, video中能够使用的属性在audio标签中大部分都能够使用, 并且功能都一样 只不过有3个属性不能用, heig ...
- C#格式化字符串大全
C#格式化字符串大全 分类: VS/C# 1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) string.Format("{0:C}" ...
- 【学习总结】Git学习-参考廖雪峰老师教程四-时光机穿梭
学习总结之Git学习-总 目录: 一.Git简介 二.安装Git 三.创建版本库 四.时光机穿梭 五.远程仓库 六.分支管理 七.标签管理 八.使用GitHub 九.使用码云 十.自定义Git 期末总 ...
- Cookie-parser
let express = require('express'); let app =new express(); // 引入cookie-parser; let cookieParser = req ...
- mybatis出现NoSuchMethodException异常
今天在idea中调试项目(ssm搭建的项目)的时候,mybatis突然出现了NoSuchMethodException异常,具体的异常时: java.lang.NoSuchMethodExceptio ...