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.

Sample test(s)
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].

题目大意是给出树的点和他们的高度,伐木工砍伐时可以让他们向左倒或向右倒,倒下后占据覆盖的坐标,但不能倒到已经被占据的点上。

是一道简单的dp和贪心题,可以看到,第一棵树和最后一棵树都是必定可以砍倒的,而从左到右遍历中间的树尽量让他们向左倒,这样倒下后占据的点不会影响到后面的点,每棵树给它一个occupy属性表明它占据的坐标的最大值。dp[i]=max(dp[i-1],i向左倒,i向右倒)。

AC代码:

 #include<iostream>
#include<cstring>
#include<string>
using namespace std;
int dp[];
struct tree{
int h,x,occupy;
}t[];
int main(){
int n,i;
while(cin>>n){
memset(dp,,sizeof(dp));
for(i=;i<n;i++){
cin>>t[i].x>>t[i].h;
t[i].occupy=t[i].x;
}
dp[]=;
if(n==){
cout<<<<endl;
continue;
}
if(n==){
cout<<<<endl;
continue;
}
for(i=;i<n-;i++){
int th=t[i].h,tx=t[i].x,toc=t[i-].occupy;
int l=dp[i-],r=dp[i-];
if((tx+th)<t[i+].x){
r++;
t[i].occupy=tx+th;
}
if(tx-th>toc){
l++;
t[i].occupy=tx;
}
dp[i]=max(max(dp[i-],l),r);
}
dp[n-]=dp[n-]+;
cout<<dp[n-]<<endl;
}
return ;
}

CF R303 div2 C. Woodcutters的更多相关文章

  1. cf 442 div2 F. Ann and Books(莫队算法)

    cf 442 div2 F. Ann and Books(莫队算法) 题意: \(给出n和k,和a_i,sum_i表示前i个数的和,有q个查询[l,r]\) 每次查询区间\([l,r]内有多少对(i, ...

  2. CF#603 Div2

    差不多半年没打cf,还是一样的菜:不过也没什么,当时是激情,现在已是兴趣了,开心就好. A Sweet Problem 思维,公式推一下过了 B PIN Codes 队友字符串取余过了,结果今天早上一 ...

  3. CF R631 div2 1330 E Drazil Likes Heap

    LINK:Drazil Likes Heap 那天打CF的时候 开场A读不懂题 B码了30min才过(当时我怀疑B我写的过于繁琐了. C比B简单多了 随便yy了一个构造发现是对的.D也超级简单 dp了 ...

  4. CF#581 (div2)题解

    CF#581 题解 A BowWow and the Timetable 如果不是4幂次方直接看位数除以二向上取整,否则再减一 #include<iostream> #include< ...

  5. [CF#286 Div2 D]Mr. Kitayuta's Technology(结论题)

    题目:http://codeforces.com/contest/505/problem/D 题目大意:就是给你一个n个点的图,然后你要在图中加入尽量少的有向边,满足所有要求(x,y),即从x可以走到 ...

  6. CF 197 DIV2 Xenia and Bit Operations 线段树

    线段树!!1A 代码如下: #include<iostream> #include<cstdio> #define lson i<<1 #define rson i ...

  7. CF#345 div2 A\B\C题

    A题: 贪心水题,注意1,1这组数据,坑了不少人 #include <iostream> #include <cstring> using namespace std; int ...

  8. CF 192 Div2

    A.Cakeminator 暴搞之,从没有草莓覆盖的行.列遍历 char map[30][30]; int vis[30][30]; int hang[30],lie[30]; int main() ...

  9. CF 191 div2

    A.数据量很小,直接爆搞. #include <iostream> #include <cstdio> #include <algorithm> #include ...

随机推荐

  1. mysql建立数据库的方法

    mysql建立数据库的方法 方法一:使用create mysql> create database roudy; Query OK, 1 row affected (0.00 sec) mysq ...

  2. Extjs springmvc session 超时 处理

    如果你的项目使用ExtJS作为表现层,你会发现,SESSION超时控制将是一个问题.本文将就自己的经验,来解决这一问题,当然,解决问题并非只有一种方法,我只是提出我的方法.首先,做超时控制,必需使用过 ...

  3. Zend Studio错误总结

    1.在升级了wampserver之后,用zendstudio发现很多地方都出错了,这时候一般先要project-clean一下,然后到run 和 debug的configuration里面把除了stu ...

  4. 有关文件夹与文件的查找,删除等功能 在 os 模块中实现

    最近在写的程序频繁地与文件操作打交道,这块比较弱,还好在百度上找到一篇不错的文章,这是原文传送门,我对原文稍做了些改动. 有关文件夹与文件的查找,删除等功能 在 os 模块中实现.使用时需先导入这个模 ...

  5. Unix/Linux环境C编程入门教程(8) FreeBSD CCPP开发环境搭建

    1. FreeBSD是一种自由类Unix操作系统,是由经过BSD.386BSD和4.4BSD发展而来的类Unix的一个重要分支.FreeBSD拥有超过200名活跃开发者和上千名贡献者.FreeBSD被 ...

  6. Error pulling origin: error: The following untracked working tree files would be overwritten by...

    git在pull时,出现这样的错误的时候,可能非常多人进进行stash.相关stash的请看:Error pulling origin: error: Your local changes to th ...

  7. 【leetcode边做边学】二分查找应用

    很多其它请关注我的HEXO博客:http://jasonding1354.github.io/ 简书主页:http://www.jianshu.com/users/2bd9b48f6ea8/lates ...

  8. 大型票务系统中username和password的安全性问题

    讨论请移步至:http://www.zhiliaotech.com/ideajam/idea/detail/307 相关文章: <今天你买到票了吗?--从铁道部12306.cn站点漫谈电子商务站 ...

  9. IHttpModule与IHttpHandler的区别整理

    IHttpModule与IHttpHandler的区别整理1.先后次序.先IHttpModule,后IHttpHandler. 注:Module要看你响应了哪个事件,一些事件是在Handler之前运行 ...

  10. CreateEvent,OpenEvent成功后 是否需要::CloseHandle(xxx); 避免句柄泄漏

    bool bExist = false; HANDLE hHandle = ::CreateEvent(NULL,  FALSE,  FALSE,  L"Global\\xxxxx_name ...