http://codeforces.com/contest/545/problem/C

 Woodcutters
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 test(s)
input
5
1 2
2 1
5 10
10 9
19 1
output
3
input
5
1 2
2 1
5 10
10 9
20 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].

题意:

n个树,在x1,x2,。。。,xn的位置,树的高度依次是h1,h2,。。。,hn

求的是当把树砍倒时候,不占用相邻树的位置,最大砍树个数

可向左 向右砍,即树向左向右倒,很显然 当树的棵树大于1的时候,一定至少可以砍倒两棵树,位于最左和最右的两棵树可以直接砍倒

可以先考虑左砍树,再考虑右砍树

满足左砍树时候,不用考虑右砍树。

对xi 和 hi

左砍树 树最左可到  xi – hi

当 xi – hi> x[i-1] 时候左砍成立  x[i-1] 更新到x[i]

右砍树 树最右可到 x[i] + h[i]

当  x[i] + h[i] < x[i+1] 时候右砍成立  x[i] 更新到 x[i] + h[i]

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std; const int N = ;
const int oo = 0x3f3f3f3f; struct node
{
int x, h;
} a[N]; int main()
{
int n; while(scanf("%d", &n)!=EOF)
{
int i, sum=; memset(a, , sizeof(a)); for(i=; i<=n; i++)
scanf("%d%d", &a[i].x, &a[i].h); if(n==)
printf("1\n");
else
{
for(i=; i<n; i++)
{
if(a[i-].x<a[i].x-a[i].h)
sum++;
else if(a[i].x+a[i].h<a[i+].x)
{
a[i].x += a[i].h;
sum++;
}
} printf("%d\n", sum);
}
}
return ;
}
题意:每棵树给出坐标和高度,可以往左右倒,也可以不倒
问最多能砍到多少棵树 DP:dp[i][0/1/2] 表示到了第i棵树时,它倒左或右或不动能倒多少棵树
分情况讨论,若符合就取最大值更新,线性dp。

DP:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std; const int N = ;
const int oo = 0x3f3f3f3f; struct node
{
int x, h;
}a[N]; int dp[N][]; /// 0~L 1~no 2~R int main()
{
int n; while(scanf("%d", &n)!=EOF)
{
int i; for(i=; i<=n; i++)
scanf("%d%d", &a[i].x, &a[i].h); memset(dp, , sizeof(dp)); dp[][] = dp[][] = ;
if(a[].x-a[].x>a[].h) dp[][] = ; for(i=; i<=n; i++)
{
int t = max(dp[i-][], max(dp[i-][], dp[i-][]));
dp[i][] = t; if(a[i].x-a[i-].x>a[i].h)
dp[i][] = max(dp[i-][], dp[i-][]) + ;
if(a[i].x-a[i-].x>a[i].h+a[i-].h)
dp[i][] = max(dp[i][], dp[i-][]+);
if(i<n && a[i+].x-a[i].x>a[i].h)
dp[i][] = t + ;
if(i==n) dp[i][] = t+;
} printf("%d\n", max(dp[n][], max(dp[n][], dp[n][])));
}
return ;
}

(贪心 or DP)Woodcutters -- Codefor 545C的更多相关文章

  1. ALGO-13_蓝桥杯_算法训练_拦截导弹(贪心,DP)

    问题描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某天,雷达捕捉到敌国的导弹 ...

  2. 【bzoj4922】[Lydsy六月月赛]Karp-de-Chant Number 贪心+背包dp

    题目描述 给出 $n$ 个括号序列,从中选出任意个并将它们按照任意顺序连接起来,求以这种方式得到匹配括号序列的最大长度. 输入 第一行包含一个正整数n(1<=n<=300),表示括号序列的 ...

  3. 【贪心优化dp决策】bzoj1571: [Usaco2009 Open]滑雪课Ski

    还有贪心优化dp决策的操作…… Description Farmer John 想要带着 Bessie 一起在科罗拉多州一起滑雪.很不幸,Bessie滑雪技术并不精湛. Bessie了解到,在滑雪场里 ...

  4. bzoj 1907: 树的路径覆盖【贪心+树形dp】

    我是在在做网络流最小路径覆盖的时候找到这道题的 然后发现是个贪心+树形dp \( f[i] \)表示在\( i \)为根的子树中最少有几条链,\( v[i] \) 表示在\( i \)为根的子树中\( ...

  5. 贪心/构造/DP 杂题选做Ⅱ

    由于换了台电脑,而我的贪心 & 构造能力依然很拉跨,所以决定再开一个坑( 前传: 贪心/构造/DP 杂题选做 u1s1 我预感还有Ⅲ(欸,这不是我在多项式Ⅱ中说过的原话吗) 24. P5912 ...

  6. 贪心/构造/DP 杂题选做Ⅲ

    颓!颓!颓!(bushi 前传: 贪心/构造/DP 杂题选做 贪心/构造/DP 杂题选做Ⅱ 51. CF758E Broken Tree 讲个笑话,这道题是 11.3 模拟赛的 T2,模拟赛里那道题的 ...

  7. Moving Tables(贪心或Dp POJ1083)

    Moving Tables Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28304   Accepted: 9446 De ...

  8. Codeforces Round #164 (Div. 2) E. Playlist 贪心+概率dp

    题目链接: http://codeforces.com/problemset/problem/268/E E. Playlist time limit per test 1 secondmemory ...

  9. poj -1065 Wooden Sticks (贪心or dp)

    http://poj.org/problem?id=1065 题意比较简单,有n跟木棍,事先知道每根木棍的长度和宽度,这些木棍需要送去加工,第一根木棍需要一分钟的生产时间,如果当前木棍的长度跟宽度 都 ...

随机推荐

  1. Oracle性能优化5-索引的不足

    索引的不足 1.索引开销 a.访问开销   反问集中导致热块的竞争(对最新数据的查询)   回表性能取决聚合因子   索引的访问开销,返回几条数据快,但是返回大量的数据很慢   全表扫描与全扫描   ...

  2. spring/spirng boot添加fluent日志-aop

    此项目以aop的形式添加fluent 日志 sample介绍 spring-mvc-aop-helloworld 为spring mvc aop condition toolcommontest 为s ...

  3. Liunx read

    read 命令从标准输入中读取一行,并把输入行的每个字段的值指定给 shell 变量 1)read后面的变量var可以只有一个,也可以有多个,这时如果输入多个数据,则第一个数据给第一个变量,第二个数据 ...

  4. iOS.Animation.CAMediaTiming

    CAMediaTiming Protocol CALayre 和 CAAnimation 实现了CAMediaTiming 接口. CAMediaTiming 定义了8个属性. speed属性: Co ...

  5. windows下Git的使用教程(github)

    这表文章主要是用命令操作: 使用可视化软件操作:https://www.cnblogs.com/mswyf/p/9261859.html 一.下载安装Git Bash 下载安装:https://www ...

  6. jzoj P1163 生日派对灯

    在IOI98的节日宴会上,我们有N(10<=N<=100)盏彩色灯,他们分别从1到N被标上号码.这些灯都连接到四个按钮:  按钮1:当按下此按钮,将改变所有的灯:本来亮着的灯就熄灭,本来是 ...

  7. 【附源文件】软件工具类Web原型制作分享 - Sketch

    Sketch是一款轻量,易用的矢量设计工具.专门为UI设计师开发,让UI设计更简单.更高效. 本原型由国产原型工具-Mockplus制作完成. 非常适合工具类产品官网使用,本模板的交互有通过使用面板组 ...

  8. Java学习笔记:数据校验

    在后台开发过程中,需要对参数进行校验. validation bean 是基于JSR-303标准开发出来的,使用注解的方式实现,是一套规范,可以实现参数的校验. Hibernate Validator ...

  9. day08作业---函数

    '''2.写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者.'''#学会了 原来 range(len(iter)) 是 从零到len-1 的数的组合 建新放在 ...

  10. 用sql 生成2016年全年的日期

    select to_char(日期,'yyyy-mm-dd') from( select to_date('2016-01-01','yyyy-mm-dd') + level 日期 from dual ...