CF R303 div2 C. Woodcutters
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].
题目大意是给出树的点和他们的高度,伐木工砍伐时可以让他们向左倒或向右倒,倒下后占据覆盖的坐标,但不能倒到已经被占据的点上。
是一道简单的dp和贪心题,可以看到,第一棵树和最后一棵树都是必定可以砍倒的,而从左到右遍历中间的树尽量让他们向左倒,这样倒下后占据的点不会影响到后面的点,每棵树给它一个occupy属性表明它占据的坐标的最大值。dp[i]=max(dp[i-1],i向左倒,i向右倒)。
AC代码:
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int dp[];
struct tree{
int h,x,occupy;
}t[];
int main(){
int n,i;
while(cin>>n){
memset(dp,,sizeof(dp));
for(i=;i<n;i++){
cin>>t[i].x>>t[i].h;
t[i].occupy=t[i].x;
}
dp[]=;
if(n==){
cout<<<<endl;
continue;
}
if(n==){
cout<<<<endl;
continue;
}
for(i=;i<n-;i++){
int th=t[i].h,tx=t[i].x,toc=t[i-].occupy;
int l=dp[i-],r=dp[i-];
if((tx+th)<t[i+].x){
r++;
t[i].occupy=tx+th;
}
if(tx-th>toc){
l++;
t[i].occupy=tx;
}
dp[i]=max(max(dp[i-],l),r);
}
dp[n-]=dp[n-]+;
cout<<dp[n-]<<endl;
}
return ;
}
CF R303 div2 C. Woodcutters的更多相关文章
- cf 442 div2 F. Ann and Books(莫队算法)
cf 442 div2 F. Ann and Books(莫队算法) 题意: \(给出n和k,和a_i,sum_i表示前i个数的和,有q个查询[l,r]\) 每次查询区间\([l,r]内有多少对(i, ...
- CF#603 Div2
差不多半年没打cf,还是一样的菜:不过也没什么,当时是激情,现在已是兴趣了,开心就好. A Sweet Problem 思维,公式推一下过了 B PIN Codes 队友字符串取余过了,结果今天早上一 ...
- CF R631 div2 1330 E Drazil Likes Heap
LINK:Drazil Likes Heap 那天打CF的时候 开场A读不懂题 B码了30min才过(当时我怀疑B我写的过于繁琐了. C比B简单多了 随便yy了一个构造发现是对的.D也超级简单 dp了 ...
- CF#581 (div2)题解
CF#581 题解 A BowWow and the Timetable 如果不是4幂次方直接看位数除以二向上取整,否则再减一 #include<iostream> #include< ...
- [CF#286 Div2 D]Mr. Kitayuta's Technology(结论题)
题目:http://codeforces.com/contest/505/problem/D 题目大意:就是给你一个n个点的图,然后你要在图中加入尽量少的有向边,满足所有要求(x,y),即从x可以走到 ...
- CF 197 DIV2 Xenia and Bit Operations 线段树
线段树!!1A 代码如下: #include<iostream> #include<cstdio> #define lson i<<1 #define rson i ...
- CF#345 div2 A\B\C题
A题: 贪心水题,注意1,1这组数据,坑了不少人 #include <iostream> #include <cstring> using namespace std; int ...
- CF 192 Div2
A.Cakeminator 暴搞之,从没有草莓覆盖的行.列遍历 char map[30][30]; int vis[30][30]; int hang[30],lie[30]; int main() ...
- CF 191 div2
A.数据量很小,直接爆搞. #include <iostream> #include <cstdio> #include <algorithm> #include ...
随机推荐
- 检测android机器是否有GPS模块
public boolean hasGPSDevice(Context context) { final LocationManager mgr = (LocationManager)context. ...
- 基于meanshift的手势跟踪与电脑鼠标控制(手势交互系统)
基于meanshift的手势跟踪与电脑鼠标控制(手势交互系统) zouxy09@qq.com http://blog.csdn.net/zouxy09 一年多前开始接触计算机视觉这个领域的时候,年幼无 ...
- AndroidManifest.xml配置文件详解
AndroidManifest.xml配置文件对于Android应用开发来说是非常重要的基础知识,本文旨在总结该配置文件中重点的用法,以便日后查阅.下面是一个标准的AndroidManifest.xm ...
- 你的阅读造就了你 You are what you read
在豆瓣上看到的一篇很有思想和正能量的文章,在这里请允许我用原创的方式来呈现给大家.如果你是在校的大学生或者研究生博士生,这篇文章会让你有很多的共鸣.如果你已真正的踏入这个社会,也将受益匪浅. 电脑 ...
- hdu 2828 Lamp 重复覆盖
题目链接 给n个灯和m个开关, 每个灯可以由若干个开关控制, 每个开关也可以控制若干个灯, 问你能否找到一种开关的状态, 使得所有的灯都亮. 将灯作为列, 然后把每个开关拆成两行, 开是一行, 关是一 ...
- Art of Unit Test (1) - Breaking Dependency
#!/usr/bin/env python # encoding: utf-8 import unittest """ the simplyest way to test ...
- Cocos2d-x 安装教程for mac(Xcode)
cocos2d v3.x 版本出来后,从配置安装到创建项目都是命令行,下面简单说一下. 1. 下载地址 http://cn.cocos2d-x.org/download/ (虽然没有标明 for ...
- VHDL数据类型转换
函 数 名 功 能 STD_LOGIC_1164包集合 TO_STDLOG ...
- Microsoft Project 2010 简体中文专业版 + 永久激活密钥
Microsoft Project 2010 简体中文专业版 + 永久激活密钥 (2014-02-17 11:44:16) 转载▼ 标签: 文化 分类: 学习 Microsoft Project已成为 ...
- delphi写的整合汇编与api的简单的窗口程序
program Project1; { Types and Structures Definition }type WNDCLASSEX = packed record cbSize: Lon ...