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的物品,问是否存在取物方案使得价 ...
随机推荐
- C#解析Json(多方法解析Json 一)
解析:{'id':'4028d80858053bed0158053ef7a50001','sl':0.0,'sfyfz':'0','zwjyzsbh':'1000001600000018'} 1.新建 ...
- 飞天诚信usb-key登录windows+远程桌面
最近在尝试用智能卡做身份验证,以飞天诚信的ePass3000为例. 1.网络环境搭建: 用3台虚机+1台实体机搭一个单独的测试网段:172.16.188.x,如下: 机器名 IP 操作系统 作用 do ...
- 【数论+技巧】神奇的Noip模拟试题第二试 T1 素数统计
1. 素数统计 (pcount.pas/.c/.cpp) [问题描述] 小tan的老师揣谙戈给同学们布置了一道题,要求统计给定区间内素数的个数.“这不是很简单吗?”小tan忍不住说.揣谙戈冷 ...
- Oracle top N实现
在Oracle中实现select top N:由于Oracle不支持select top 语句,所以在Oracle中经常是用order by 跟rownum的组合来实现select top n的查询. ...
- SharePoint 2013 Nintex Workflow 工作流帮助(十二)
博客地址 http://blog.csdn.net/foxdave 工作流动作 31. Create task(User interaction分组,企业版才有) 该操作用于在Microsoft Ex ...
- 分享Windows Server 2012 R2的获取正版密钥方法
然后使用“我有ISIC卡”验证,目前可用号码:S420546009858. 分享Windows Server 2012 R2的获取正版密钥方法. 首先登陆dreamspark注册一个账号https:/ ...
- c++的调试与运行
编译F9:运行F10:编译运行F11. 设置断点:在代码所在行的行首单击,该行即被加亮.注意:设置断点后,此时程序运行进入调试状态,要想运行程序,就不能使用F10或者F11,而是要使用F5调试,然后使 ...
- PAT 08-2 求矩阵的局部最大值
这题挺简单的,但,每日一篇.说两点:第一,我的粗心导致我这题花了大把的时间去找错误,看到4个测试用例对了三个,我以为是那块的边界条件没考虑到,又或者是存在隐蔽的逻辑或语法错误,通过与别人程序的反复对比 ...
- jQuery easyui 提示框
1:弹出提示窗的使用 (1)屏幕右下弹出提示窗口: $.messager.show({ title:'My Title', msg:'Message will be closed after 4 se ...
- Hadoop MRUnit使用(一)
之前在写MR job的时候,由于要在云梯,或者一淘的开发集群上运行:所以处理方法是,在本地打成jar包,然后scp到客户端网关机上,然后在提交job运行.这样的问题时,有时候如果遇到一些逻辑上的问题, ...