Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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 Input

Input
  1. 5
    1 2
    2 1
    5 10
    10 9
    19 1
Output
  1. 3
Input
  1. 5
    1 2
    2 1
    5 10
    10 9
    20 1
Output
  1. 4

Hint

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

题意:一条路上有树,树有高度h,我们可以将树向左或向右砍倒只要它倒下去不会压到其他树(不论这些树是站着还是倒下了)。求最多可以砍倒多少树。

这一场cf真是水的一匹,我当时怎么没打!!!简直上分福利局。

我们只要按照题目给定的操作走一遍就可以了。优先向左倒,就连数据都已经给你排好序了。

附AC代码:

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. long long x[];
  5. long long h[];
  6.  
  7. int main(){
  8. int n;
  9. cin>>n;
  10. for(int i=;i<n;i++){
  11. cin>>x[i]>>h[i];
  12. }
  13. if(n>){
  14. int ans=;
  15. for(int i=;i<n-;i++){
  16. if(x[i]-h[i]>x[i-]){
  17. ans++;
  18. }
  19. else if(x[i]+h[i]<x[i+]){
  20. ans++;
  21. x[i]+=h[i];
  22. }
  23. }
  24. cout<<ans<<endl;
  25. }
  26. else
  27. cout<<n<<endl;
  28. return ;
  29. }

C - Woodcutters的更多相关文章

  1. CF R303 div2 C. Woodcutters

    C. Woodcutters time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  2. (贪心 or DP)Woodcutters -- Codefor 545C

    http://codeforces.com/contest/545/problem/C  Woodcutters time limit per test 1 second memory limit p ...

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

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

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

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

  5. 545C. Woodcutters

    题目链接 题意: n个树,在x1,x2,...,xn的位置,树的高度依次是h1,h2,...,hn 求的是当把树砍倒时候,不占用相邻树的位置,最大砍树个数 可向左 向右砍,即树向左向右倒,很显然 当树 ...

  6. Codeforces 545C Woodcutters

    http://codeforces.com/contest/545/problem/C 题目大意: 给n棵树的在一维数轴上的坐标,以及它们的高度.现在要你砍倒这些树,树可以向左倒也可以向右倒,砍倒的树 ...

  7. 「日常训练」Woodcutters(Codeforces Round 303 Div.2 C)

    这题惨遭被卡..卡了一个小时,太真实了. 题意与分析 (Codeforces 545C) 题意:给定\(n\)棵树,在\(x\)位置,高为\(h\),然后可以左倒右倒,然后倒下去会占据\([x-h,x ...

  8. Codeforces Round #303 (Div. 2) C dp 贪心

    C. Woodcutters time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  9. python瓦登尔湖词频统计

    #瓦登尔湖词频统计: import string path = 'D:/python3/Walden.txt' with open(path,'r',encoding= 'utf-8') as tex ...

随机推荐

  1. Linux C函数库大全

    (1)字符测试函数 isalnum(测试字符是否为英文字母或数字) isalpha(测试字符是否为英文字母) isascii(测试字符是否为ASCII码字符) isblank(测试字符是否为空格字符) ...

  2. CF 558B(Amr and The Large Array-计数)

    B. Amr and The Large Array time limit per test 1 second memory limit per test 256 megabytes input st ...

  3. 手机号码归属地查询免费api接口代码

    依据手机号码查询用户的卡类型.运营商.归属地.区域等信息. 手机归属地实体类 package org.wx.xhelper.model; /** * 手机归属地 * @author wangxw * ...

  4. to_char(sysdate,'D')的用法

    select to_char(sysdate,'D') from dual;   这周的第几天:Day of week (1–7) 1,2,3,4,5,6,7--注意:每星期的第1天是”星期日“ se ...

  5. HDU 1040 As Easy As A+B [补]

    今天去老校区找她,不想带电脑了,所以没时间A题了 /*******************************************************************/ As Ea ...

  6. win7 64位安装vs2013 出现'System.AccessViolationException的错误

    用管理员身份运行CMD,输入netsh winsock reset并回车(注意,必须是已管理员身份运行,这个重置LSP连接)

  7. HDOJ_1000

    #include int main() { int i, j; while(scanf("%d%d", &i, &j) == 2) printf("%d\ ...

  8. weblogic 修改控制台console访问路径 url

    出于安全的考虑需要对weblogic的console进行屏避,或者修改默认的访问路径,主要有两种方法:(这里针对weblogic8.1) 一.进入默认的控制台,例如“localhost/console ...

  9. zTree async 动态参数处理

    async:{ enable: true,//开启异步机制 url: opts.url,//异步地址 otherParam: {  'plateNo': function(){ return $('# ...

  10. Struts2中Action接收参数

    Struts2中Action接收参数的方法主要有以下三种: Struts2中Action接收参数的方法主要有以下三种: 1.使用Action的属性接收参数:     a.定义:在Action类中定义属 ...