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,这个数列中每两个数,如果满足一个数能整除另一个数,则这两个数中间是有一条边的,现在有这样的图,求最大联通子图. 题解:并不需要把图搞出来, ...
随机推荐
- mysql 索引原理
一.索引的本质 MySQL官方对索引的定义为:索引(Index)是帮助MySQL高效获取数据的数据结构.提取句子主干,就可以得到索引的本质:索引是数据结构. 我们知道,数据库查询是数据库的最主要功能之 ...
- 【原创】jssh linux scp ssh 免密登录开源工具
项目名 JSSH git地址: https://gitee.com/chejiangyi/jssh 项目介绍 linux scp(文件上传,下载) shell命令的java ssh jar和linux ...
- ML.NET 示例:多类分类之鸢尾花分类
写在前面 准备近期将微软的machinelearning-samples翻译成中文,水平有限,如有错漏,请大家多多指正. 如果有朋友对此感兴趣,可以加入我:https://github.com/fei ...
- 体验usually.js的管道函数——pipe函数
体验usually.js的管道函数——pipe函数 usually.js 是一个面向现代 Web 开发的 JavaScript 函数库,基于 ES6 开发.最新版本2.4.1,最新版本usually. ...
- 正确理解Handle对象
上古时期的程序员, 肯定都知道Handle对象, 一般中文翻译成句柄. 一般的Handle在实现上, 都是一个整数, 而这个整数可以理解为一个指针, 指针指向的地址呢, 又保存了另外一个指针. 之所以 ...
- require('./expample.js).default详解
最近总碰到类似于 var a = require('./expample.js).default 这样的代码,感觉很奇葩,总结一波. 为什么会出现这个问题? import 是静态编译的,而 requi ...
- 用python表白了!!!
用python 画一颗心,代码: import numpy as np import matplotlib.pyplot as plt x = np.linspace(-8 , 8, 1024) ...
- anaconda 出现add 。。。进不去
找到.condarc 文件 C:\Users\leiyi内 把里面内容替换为 channels: - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pk ...
- Java 学习使用常见的开源连接池
目录 连接池介绍 自定义连接池 JDBC Tomcat Pool DBCP(DataBase Connection Pool) 使用配置文件来设置DBCP C3P0 Druid 连接池介绍 在说连接池 ...
- 学习WebSocket
初识WebSocket: index.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...