C. Woodcutters

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

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

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 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 Input

5
1 2
2 1
5 10
10 9
19 1

Sample Output

3

HINT

题意

给你n棵树,在x位置,高为h,然后可以左倒右倒,然后倒下去会占据[x-h,x]或者[x,x+h]区间,或者选择不砍伐,占据[x,x]区域

问你最多砍多少棵树,砍树的条件是不能被其他树占据

题解:

一开始看到就在想DP,后来想了想,dp好麻烦,那就贪心吧

然后哦,这TM不是傻逼题吗

哦,然后搞定了

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
/* inline void P(int x)
{
Num=0;if(!x){putchar('0');puts("");return;}
while(x>0)CH[++Num]=x%10,x/=10;
while(Num)putchar(CH[Num--]+48);
puts("");
}
*/
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
puts("");
}
//************************************************************************************** pair<int,int> a[maxn];
int main()
{
int n=read();
for(int i=;i<n;i++)
a[i].first=read(),a[i].second=read();
sort(a,a+n);
int ans=;
int kiss=-inf;
for(int i=;i<n;i++)
{
if(a[i].first-a[i].second>kiss)
{
kiss=a[i].first;
ans++;
}
else if(i==n-||a[i].first+a[i].second<a[i+].first)
{
kiss=a[i].first+a[i].second;
ans++;
}
else
kiss=a[i].first;
}
printf("%d",ans);
}

Codeforces Round #303 (Div. 2) C. Woodcutters 贪心的更多相关文章

  1. DP Codeforces Round #303 (Div. 2) C. Woodcutters

    题目传送门 /* 题意:每棵树给出坐标和高度,可以往左右倒,也可以不倒 问最多能砍到多少棵树 DP:dp[i][0/1/2] 表示到了第i棵树时,它倒左或右或不动能倒多少棵树 分情况讨论,若符合就取最 ...

  2. Codeforces Round #303 (Div. 2) C dp 贪心

    C. Woodcutters time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  3. Codeforces Round #303 (Div. 2) D. Queue —— 贪心

    题目链接:http://codeforces.com/problemset/problem/545/D 题解: 问经过调整,最多能使多少个人满意. 首先是排序,然后策略是:如果这个人对等待时间满意,则 ...

  4. Codeforces Round #303 (Div. 2) B 水 贪心

    B. Equidistant String time limit per test 1 second memory limit per test 256 megabytes input standar ...

  5. 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String

    题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...

  6. 水题 Codeforces Round #303 (Div. 2) D. Queue

    题目传送门 /* 比C还水... */ #include <cstdio> #include <algorithm> #include <cstring> #inc ...

  7. 水题 Codeforces Round #303 (Div. 2) A. Toy Cars

    题目传送门 /* 题意:5种情况对应对应第i或j辆车翻了没 水题:其实就看对角线的上半边就可以了,vis判断,可惜WA了一次 3: if both cars turned over during th ...

  8. 「日常训练」Woodcutters(Codeforces Round 303 Div.2 C)

    这题惨遭被卡..卡了一个小时,太真实了. 题意与分析 (Codeforces 545C) 题意:给定\(n\)棵树,在\(x\)位置,高为\(h\),然后可以左倒右倒,然后倒下去会占据\([x-h,x ...

  9. Codeforces Round #303 (Div. 2)(CF545) E Paths and Trees(最短路+贪心)

    题意 求一个生成树,使得任意点到源点的最短路等于原图中的最短路.再让这个生成树边权和最小. http://codeforces.com/contest/545/problem/E 思路 先Dijkst ...

随机推荐

  1. 64_r1

    R-3.4.0-2.fc26.x86_64.rpm 15-May-2017 14:49 31030 R-ALL-1.6.0-4.fc26.noarch.rpm 17-Feb-2017 22:05 11 ...

  2. juery获取元素的方法

    1 从集合中通过指定的序号获取元素 html: 复制代码 代码如下: <div> <p>0</p> <p>1</p> <p>2& ...

  3. Linux搭建svn服务

    svn是为了方便代码进行版本控制 Linux)svn服务器 --> windows) svn访问端 ********* [root@svn ~]# yum install -y subversi ...

  4. 在ubuntu上安装Chrome

    1.下载谷歌浏览器源文件.链接有很多,以下是64位版本的下载地址 https://dl.google.com/linux/direct/google-chrome-stable_current_amd ...

  5. go语言入门(一)

    环境安装 Go 语言支持以下系统: Linux FreeBSD Mac OS X(也称为 Darwin) Window 安装包下载地址为:https://golang.org/dl/. Windows ...

  6. 转:google测试分享-测试经理

    原文: http://blog.sina.com.cn/s/blog_6cf812be0102vode.html 前言:这个系列分享的内容大部分都是出自于<google是如何测试的>的书, ...

  7. VMware-workstation-6.5.2-156735.exe

    480HD-KPZ2X-TA56C-4YTQQ VMware 12 专业版永久许可证密钥 5A02H-AU243-TZJ49-GTC7K-3C61N

  8. wordpress后台加载速度异常缓慢排查记录(原创)

    原因在于在function.php函数中加入了下面的代码导致了缓慢: //停用版本更新通知remove_action('load-update-core.php', 'wp_update_themes ...

  9. K8S网络排故障一则--iptables规则

    这个故障源起来在k8s上同时安装ceph群集(测试的时候机器不多啊) 当这两者都OK之后,原来k8s上的服务实例,则有的通,有的不通了. ==================== 所有可能的故障点,f ...

  10. Java StringJoiner

    Java StringJoiner Java added a new final class StringJoiner in java.util package. It is used to cons ...