Codeforces Round #303 (Div. 2) C. Woodcutters 贪心
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 贪心的更多相关文章
- DP Codeforces Round #303 (Div. 2) C. Woodcutters
题目传送门 /* 题意:每棵树给出坐标和高度,可以往左右倒,也可以不倒 问最多能砍到多少棵树 DP:dp[i][0/1/2] 表示到了第i棵树时,它倒左或右或不动能倒多少棵树 分情况讨论,若符合就取最 ...
- Codeforces Round #303 (Div. 2) C dp 贪心
C. Woodcutters time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 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 ...
- 贪心 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 ...
- 「日常训练」Woodcutters(Codeforces Round 303 Div.2 C)
这题惨遭被卡..卡了一个小时,太真实了. 题意与分析 (Codeforces 545C) 题意:给定\(n\)棵树,在\(x\)位置,高为\(h\),然后可以左倒右倒,然后倒下去会占据\([x-h,x ...
- Codeforces Round #303 (Div. 2)(CF545) E Paths and Trees(最短路+贪心)
题意 求一个生成树,使得任意点到源点的最短路等于原图中的最短路.再让这个生成树边权和最小. http://codeforces.com/contest/545/problem/E 思路 先Dijkst ...
随机推荐
- Tslib触摸屏官网【转】
转自:https://github.com/kergoth/tslib C library for filtering touchscreen events tslib consists of the ...
- VS2015_动态链接库学习
非MFC动态链接库 创建一个名为ex1的Win32项目 创建一个DLL项目,保留预编译的头文件 默认文件 创建完成项目之后,包含几个默认的文件 stdafx.h文件用于包含标准系统包含的头文件 ...
- Nginx常见错误及处理方法
转载:https://www.cnblogs.com/liyongsan/p/6795851.html 404 bad request 一般原因:请求的Header过大 解决方法:配置nginx.co ...
- inux命令英文缩写的含义(方便记忆) 2
linux常用命令的英文单词缩写 命令缩写: ls:list(列出目录内容) cd:Change Directory(改变目录) su:switch user 切换用户rpm:redhat packa ...
- K&R《C语言》书中的一个Bug
最近在重温K&R的C语言圣经,第二章中的练习题2-2引起了我的注意. 原题是: Write a loop equivalent to the for loop above without us ...
- C# 中DateTime的各种使用
获得当前系统时间: DateTime dt = DateTime.Now; Environment.TickCount可以得到“系统启动到现在”的毫秒值 DateTime now = DateTime ...
- Windows内核分析——内核调试机制的实现(NtCreateDebugObject、DbgkpPostFakeProcessCreateMessages、DbgkpPostFakeThreadMessages分析)
本文主要分析内核中与调试相关的几个内核函数. 首先是NtCreateDebugObject函数,用于创建一个内核调试对象,分析程序可知,其实只是一层对ObCreateObject的封装,并初始化一些结 ...
- 【LOJ】 #2011. 「SCOI2015」情报传递
题解 一写过一交A的一道数据结构水题 我们发现大于C可以转化为这条路径上有多少个在某天之前开始调查的情报员,离线全部读入,变成树上路径查询某个区间的数出现过多少次,构建一棵根缀的主席树,查询的时候用两 ...
- shell脚本中${var1:-var2}
在一个shell脚本中看见一行代码: DATE=${:-`date "+%Y%m%d" -d "-1 day"`} 查了一下 ${var1:-var2} 这种结 ...
- java.lang.ClassCastException: android.widget.ImageButton异常处理
在调程序时总是出现异常关闭的现象,log显示: 03-26 07:58:09.528: E/AndroidRuntime(398): Caused by: java.lang.ClassCastExc ...