【贪心】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

 

转载于:https://www.cnblogs.com/SoulSecret/p/8427195.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值