C. Woodcutters
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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
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
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].

题意:n棵树位于一排 给你n棵树的坐标以及树高 砍树可以将树向左推倒或者向右推倒

有一个区间范围[xi - hi, xi] or [xi;xi + hi]. 但是不能有别的树在这个区间内(闭区间) 问最多能砍多少棵树

题解:dp 设置状态

dp[i][0]  当前位置的树不砍,前i棵树最多能砍多少棵

dp[i][1]  当前位置的树向左推倒,前i棵树最多能砍多少棵

dp[i][2]  当前位置的树向右推倒,前i棵树最多能砍多少棵

 /******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
#include<queue>
#include<cmath>
#define ll __int64
#define PI acos(-1.0)
#define mod 1000000007
using namespace std;
int n;
struct node
{
int x,h;
} N[];
int dp[][];
int main()
{
scanf("%d",&n);
for(int i=; i<=n; i++)
scanf("%d %d",&N[i].x,&N[i].h);
dp[][]=;
if((N[].x+N[].h)<N[].x)
dp[][]=;
else
dp[][]=;
dp[][]=;
for(int i=; i<n; i++)
{
if((N[i-].x+N[i-].h)<(N[i].x-N[i].h))
dp[i][]=max(dp[i-][],max(dp[i-][],dp[i-][]))+;
else if((N[i].x-N[i].h)>N[i-].x)
dp[i][]=max(dp[i-][],dp[i-][])+;
else
dp[i][]=max(dp[i-][],max(dp[i-][],dp[i-][]));
if((N[i].x+N[i].h)<N[i+].x)
dp[i][]=max(dp[i-][],max(dp[i-][],dp[i-][]))+;
else
dp[i][]=max(dp[i-][],max(dp[i-][],dp[i-][]));
dp[i][]=max(dp[i-][],max(dp[i-][],dp[i-][]));
}
cout<<max(dp[n-][],max(dp[n-][],dp[n-][]))+<<endl;
}

Codeforces Round #303 (Div. 2) C dp 贪心的更多相关文章

  1. Codeforces Round #303 (Div. 2) C. Woodcutters 贪心

    C. Woodcutters Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/probl ...

  2. Codeforces Round #303 (Div. 2) D. Queue —— 贪心

    题目链接:http://codeforces.com/problemset/problem/545/D 题解: 问经过调整,最多能使多少个人满意. 首先是排序,然后策略是:如果这个人对等待时间满意,则 ...

  3. Codeforces Round #303 (Div. 2) B 水 贪心

    B. Equidistant String time limit per test 1 second memory limit per test 256 megabytes input standar ...

  4. DP Codeforces Round #303 (Div. 2) C. Woodcutters

    题目传送门 /* 题意:每棵树给出坐标和高度,可以往左右倒,也可以不倒 问最多能砍到多少棵树 DP:dp[i][0/1/2] 表示到了第i棵树时,它倒左或右或不动能倒多少棵树 分情况讨论,若符合就取最 ...

  5. 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String

    题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...

  6. 水题 Codeforces Round #303 (Div. 2) D. Queue

    题目传送门 /* 比C还水... */ #include <cstdio> #include <algorithm> #include <cstring> #inc ...

  7. 水题 Codeforces Round #303 (Div. 2) A. Toy Cars

    题目传送门 /* 题意:5种情况对应对应第i或j辆车翻了没 水题:其实就看对角线的上半边就可以了,vis判断,可惜WA了一次 3: if both cars turned over during th ...

  8. Codeforces Round #164 (Div. 2) E. Playlist 贪心+概率dp

    题目链接: http://codeforces.com/problemset/problem/268/E E. Playlist time limit per test 1 secondmemory ...

  9. 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】

    https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...

随机推荐

  1. JavaScript 字符串和日期内容整理

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. DP 剪枝

    DP其实也是和搜索一样可以有剪枝的,昨晚看到一个超级好的DP剪枝题:(HDU - 5009) N段东东,要染色,每次给一个区间染色需要的花费为  该区间颜色总数的平方.  每一段只能被染一次色.求 最 ...

  3. c# MVC在WEB.Config中配置MIME

    在IIS中,默认没有添加.json格式的MIME,所有无法读取服务器中的.json格式的文件,返回结果404 方式一:在IIS中手动添加MIME 1.点击MIME进入MIME列表 2.添加MIME 3 ...

  4. (DFS)zoj1008-Gnome Tetravex

    现在zoj暂时关了,实际上是在scuoj上做的. 题目地址 看起来题目比较复杂,实际上主要需要思维的是如何恰当的剪枝及合适的DFS角度. 问题等价于将n*n个可能相同的方块放到一个n*n的表中,使满足 ...

  5. ubuntu 12.04内核升级到3.13.1

    1.背景:今天上午连接Android调试之后,突然又出现了无法识别usb的问题.具体表现为:除usb无线网卡有效外,其他usb设备包括usb鼠标.u盘.android手机插上后都没反应.dmesg一直 ...

  6. scikit-learn 机器学习工具包

    scikit-learn官网:http://scikit-learn.org/stable/ scikit-learn初始化学习:  http://scikit-learn.org/stable/in ...

  7. K2 BPM医疗行业EMS解决方案

    EMS,即Event Management System,K2医疗行业EMS解决方案包括四方面的内容. 详情链接:http://www.k2software.cn/zh-hans/ems-soluti ...

  8. 蓝桥杯 algo_5 最短路 (bellman,SPFA)

    问题描述 给定一个n个顶点,m条边的有向图(其中某些边权可能为负,但保证没有负环).请你计算从1号点到其他点的最短路(顶点从1到n编号). 输入格式 第一行两个整数n, m. 接下来的m行,每行有三个 ...

  9. python框架(flask/django/tornado)比较

    一.对外数据接口 三者作为web框架,都是通过url映射对外的接口 flask:以decorator的形式,映射到函数中 django:以字典形式,映射到函数 tornado: 以字典形式,映射到类中 ...

  10. C++C++ 指针(二)--c++ 指针(二)--c++

    一.内存管理:new和delete 1.new操作符:从操作系统获得内存块,并返回该内存块的首地址. delete操作符:将new申请的内存返还给操作系统. 开始一个简单的例子: #include & ...