CF 545C Woodcutters(贪心)

题目链接:http://codeforces.com/problemset/problem/545/C

题目:

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.

Examples

input

Copy

51 22 15 1010 919 1

output

3

input

Copy

51 22 15 1010 920 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].

题解:贪心 ,能放就尽可能放。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3
 4 const int N=1e5+10;
 5 int x[N],h[N];
 6
 7 int main(){
 8     int n,ans=0;
 9     scanf("%d",&n);
10     for(int i=1;i<=n;i++) scanf("%d%d",&x[i],&h[i]);
11     x[0]=-2e9-10;x[n+1]=2e9+10;
12     for(int i=1;i<=n;i++){
13         if((x[i]-h[i])>x[i-1]) ans++;
14         else if((x[i]+h[i])<x[i+1]){
15             ans++;
16             x[i]=x[i]+h[i];
17         }
18     }
19     printf("%d\n",ans);
20     return 0;
21 }

原文地址:https://www.cnblogs.com/Leonard-/p/8510176.html

时间: 2024-08-08 03:18:24

CF 545C Woodcutters(贪心)的相关文章

[2016-02-04][CF][545C][Woodcutters]

Woodcutters Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Status 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

CF 332 C 贪心

题目链接:http://codeforces.com/problemset/problem/332/C 参考  链接: http://blog.csdn.net/cc_again/article/details/9471465 题目意思: 有n个命令,要通过p个,某主席要在通过的p个中选择k个接受. 每个任务有两个值ai,bi, ai表示如果该主席接受该命令,她的头发变灰的数量,bi表示如果该主席不接受该命令时,议员不高兴值. 对于通过的p个命令,该主席要使议员的不高兴值和最小,在相同的情况下,

CodeForces 545C Woodcutters (贪心orDP)

[题目链接]:click here~~ [题目大意]: 有n棵树,给出每棵树的位置和高度,然后把树砍掉,树可以向左倒也可以向右倒.输出最多能砍几棵树. [思路]:利用贪心的思想.第一棵树的左边和最后一棵树的右边没树,所以他们向两边倒,然后对于中间的树来说,首先先向左边倒,然后左边距离如果不够的话再向右边倒,向右倒的时候注意更新一下距离. 代码: /* * Problem: CodeForces 545C * Running time: 46MS * Complier: G++ * Author:

CF 545C

题意: 砍树, 树会向左或者向右倒,数不能倒重叠, 问最多可以砍多少树 思路: 贪心 + Dp吧, 树要尽可能网左倒,这样对后面的树影响较小, 才是最优状态 #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn = 1e5 + 131; struct Tree { int pos, val; int MaxPos; }T[maxn]; int D

CF18D 高精度+贪心

http://codeforces.com/problemset/problem/18/D Last year Bob earned by selling memory sticks. During each of n days of his work one of the two following events took place: A customer came to Bob and asked to sell him a 2x MB memory stick. If Bob had s

CF #374 (Div. 2) D. 贪心,优先队列或set

1.CF #374 (Div. 2)   D. Maxim and Array 2.总结:按绝对值最小贪心下去即可 3.题意:对n个数进行+x或-x的k次操作,要使操作之后的n个数乘积最小. (1)优先队列 #include<bits/stdc++.h> #define F(i,a,b) for (int i=a;i<b;i++) #define FF(i,a,b) for (int i=a;i<=b;i++) #define mes(a,b) memset(a,b,sizeof(

CF(437C)The Child and Toy(贪心)

题意:给出一个无向图,每个点有点权,操作是一个一个将所有点揪走直至剩下一个点,揪走一个点的代价是剩下点中与其连边的点的点权和.求完成操作所需花费的最小代价. 解法:贪心的思想,每次将剩余点中点权最大的点揪出,这样可以保证每条边都是会选择相对小的点权被消耗掉.所以直接输出所有边的边权和即可. 代码: /****************************************************** * author:xiefubao **************************

CF 500 C. New Year Book Reading 贪心 简单题

New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 ≤ i ≤ n) book is wi. As Jaehyun's house is not large enough to have a bookshelf, he k

CF #401 (Div. 2) E. Hanoi Factory (栈+贪心)

题意:给你一堆汉诺塔的盘子,设内半径为a,设外半径为b,高度为h,如果bj?≤?bi 同时bj?>?ai 我们就认为i盘子能落在在j盘子上,问你最高能落多高 思路:一看题意我们就能想到贪心,首先我们对这些圆盘先按照b从大到小排序,如果b相同,那么就要按照a从大到小排序,其实落汉诺塔的过程就像在栈一样,先进后出,所以我们可以用栈来模拟落汉诺塔的过程,如果当前的不能落上我们就把最上面的盘子拿走,直到能落下一个盘子,每一次都计算一下当前的高度,并判断是否达到最高即可 代码: #include <bi