zoukankan      html  css  js  c++  java
  • 【贪心】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
  • 相关阅读:
    python os.stat() 和 stat模块详解
    Python中datetime库的用法
    js中Array的map()函数,其中的回调函数还能这么用
    通常用于返回值的三目运算符
    浏览器渲染页面的时候,不同的script块之间的关系
    模板字面量
    let和const
    js 数组 map方法
    一些常用的js循环,如for
    session
  • 原文地址:https://www.cnblogs.com/SoulSecret/p/8427195.html
Copyright © 2011-2022 走看看