• 【贪心】CodeForces-545C:Woodcutters


    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

    5
    1 2
    2 1
    5 10
    10 9
    19 1

    Sample Output

    3

    HINT

    题意

    给你n棵树,在x位置,高为h,然后可以左倒右倒,然后倒下去会占据[x-h,x]或者[x,x+h]区间,或者选择不砍伐,占据[x,x]区域

    问你最多砍多少棵树,砍树的条件是不能被其他树占据

     1 #include <iostream>
     2 #include <bits/stdc++.h>
     3 using namespace std;
     4 const int maxn = 1e5+50;
     5 const int INF =0x7f7f7f7f;
     6 struct node
     7 {
     8     int pos,h;
     9 };
    10 node tree[maxn];
    11 int main()
    12 {
    13     int n;
    14     cin>>n;
    15     for(int i=1;i<=n;i++)
    16     {
    17         scanf("%d%d",&tree[i].pos,&tree[i].h);
    18     }
    19     tree[0].pos=-INF;
    20     tree[n+1].pos=INF;
    21     int cnt=0;
    22     for(int i=1;i<=n;i++)
    23     {
    24         if(tree[i].pos-tree[i].h>tree[i-1].pos)
    25         {
    26             cnt++;
    27             continue;
    28         }
    29         if(tree[i+1].pos>tree[i].h+tree[i].pos)
    30         {
    31             tree[i].pos+=tree[i].h;
    32             cnt++;
    33         }
    34     }
    35     cout << cnt << endl;
    36     return 0;
    37 }
    View Code
  • 相关阅读:
    spring使用中ModelAttribute的内容被覆盖
    html中,纯数字或纯英文的一串字符超出父容器不会折行显示,如何解决?
    js实现刷新页面出现随机背景图
    为tomcat配置项目必须的引擎文件
    mysql如何出查出最近7天,最近30天,最近n天的记录?
    为了显示此页面,Firefox 必须发送将重复此前动作的数据(例如搜索或者下订单)
    git 恢复到旧版本命令
    七大经典排序算法总结(C语言描述)
    C语言描述栈的实现及操作(链表实现)
    C语言的文件读写操作函数小结
  • 原文地址:https://www.cnblogs.com/SoulSecret/p/8427195.html
Copyright © 2020-2023  润新知