Codeforces Round #303 (Div. 2) C dp 贪心
1 second
256 megabytes
standard input
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.
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.
Print a single number — the maximum number of trees that you can cut down by the given rules.
5
1 2
2 1
5 10
10 9
19 1
3
5
1 2
2 1
5 10
10 9
20 1
4
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棵树位于一排 给你n棵树的坐标以及树高 砍树可以将树向左推倒或者向右推倒
有一个区间范围[xi - hi, xi] or [xi;xi + hi]. 但是不能有别的树在这个区间内(闭区间) 问最多能砍多少棵树
题解:dp 设置状态
dp[i][0] 当前位置的树不砍,前i棵树最多能砍多少棵
dp[i][1] 当前位置的树向左推倒,前i棵树最多能砍多少棵
dp[i][2] 当前位置的树向右推倒,前i棵树最多能砍多少棵
/******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
#include<queue>
#include<cmath>
#define ll __int64
#define PI acos(-1.0)
#define mod 1000000007
using namespace std;
int n;
struct node
{
int x,h;
} N[];
int dp[][];
int main()
{
scanf("%d",&n);
for(int i=; i<=n; i++)
scanf("%d %d",&N[i].x,&N[i].h);
dp[][]=;
if((N[].x+N[].h)<N[].x)
dp[][]=;
else
dp[][]=;
dp[][]=;
for(int i=; i<n; i++)
{
if((N[i-].x+N[i-].h)<(N[i].x-N[i].h))
dp[i][]=max(dp[i-][],max(dp[i-][],dp[i-][]))+;
else if((N[i].x-N[i].h)>N[i-].x)
dp[i][]=max(dp[i-][],dp[i-][])+;
else
dp[i][]=max(dp[i-][],max(dp[i-][],dp[i-][]));
if((N[i].x+N[i].h)<N[i+].x)
dp[i][]=max(dp[i-][],max(dp[i-][],dp[i-][]))+;
else
dp[i][]=max(dp[i-][],max(dp[i-][],dp[i-][]));
dp[i][]=max(dp[i-][],max(dp[i-][],dp[i-][]));
}
cout<<max(dp[n-][],max(dp[n-][],dp[n-][]))+<<endl;
}
Codeforces Round #303 (Div. 2) C dp 贪心的更多相关文章
- Codeforces Round #303 (Div. 2) C. Woodcutters 贪心
C. Woodcutters Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/probl ...
- Codeforces Round #303 (Div. 2) D. Queue —— 贪心
题目链接:http://codeforces.com/problemset/problem/545/D 题解: 问经过调整,最多能使多少个人满意. 首先是排序,然后策略是:如果这个人对等待时间满意,则 ...
- Codeforces Round #303 (Div. 2) B 水 贪心
B. Equidistant String time limit per test 1 second memory limit per test 256 megabytes input standar ...
- DP Codeforces Round #303 (Div. 2) C. Woodcutters
题目传送门 /* 题意:每棵树给出坐标和高度,可以往左右倒,也可以不倒 问最多能砍到多少棵树 DP:dp[i][0/1/2] 表示到了第i棵树时,它倒左或右或不动能倒多少棵树 分情况讨论,若符合就取最 ...
- 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String
题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...
- 水题 Codeforces Round #303 (Div. 2) D. Queue
题目传送门 /* 比C还水... */ #include <cstdio> #include <algorithm> #include <cstring> #inc ...
- 水题 Codeforces Round #303 (Div. 2) A. Toy Cars
题目传送门 /* 题意:5种情况对应对应第i或j辆车翻了没 水题:其实就看对角线的上半边就可以了,vis判断,可惜WA了一次 3: if both cars turned over during th ...
- Codeforces Round #164 (Div. 2) E. Playlist 贪心+概率dp
题目链接: http://codeforces.com/problemset/problem/268/E E. Playlist time limit per test 1 secondmemory ...
- 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】
https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...
随机推荐
- 标签视图控制器UITabBarController
标签视图控制器 UITabBarController FirstViewController*first = [[FirstViewController alloc] init]; //创建一个UIT ...
- 二模 (12) day1
第一题: 题目大意: 求由N个1,M个0组成的排列的个数,要求在排列的任意一个前缀中,1的个数不少于0的个数.N,M<=5000. 解题过程: 1.看到N,M的范围就明确肯定不会是dp,因为起码 ...
- redis Ok2
Redis::__construct 描述: 创建一个Redis客户端 范例: $redis = new Redis(); connect, open 描述: 实例连接到一个Redis. 参数:h ...
- svm特征
svm特征格式:<label><index1>:<value1><index1>:<value1>.... 其中<label> ...
- RM报表 刷新打印机列表
procedure TRMReport.ShowPreparedReport; var s: string; lPreviewForm: TRMPreviewForm; begin RMCurRepo ...
- poj 1797 Heavy Transportation(最短路径Dijkdtra)
Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 26968 Accepted: ...
- ASP.NET 分页控件
using System; using System.ComponentModel; using System.Web; using System.Web.UI; using System.Web.U ...
- C# 对MongoDB 进行增删改查的简单操作 (转)
运用到的MongoDB支持的C#驱动,当前版本为1.6.0 下载地址:https://github.com/mongodb/mongo-csharp-driver/downloads 1,连接数据库 ...
- 自适应label的高度
iOS7以下的系统可使用方法 //获得当前cell高度 CGRect frame = [self frame]; //文本赋值 self.introduction.text = text; //设置l ...
- PAT 07-2 A+B和C
有两个值得注意的地方:1.变长数组(VLA)的使用,没想到PAT上的OJ竟然支持C99,一开始不知道就没用,看了看别人的,既然,还是用吧, 它有一点我不太喜欢,它不能像一般数组那样在声明时通过赋一个0 ...