• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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[20];
//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=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
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("");
}
//**************************************************************************************


pair<int,int> a[maxn];
int main()
{
    int n=read();
    for(int i=0;i<n;i++)
        a[i].first=read(),a[i].second=read();
    sort(a,a+n);
    int ans=0;
    int kiss=-inf;
    for(int i=0;i<n;i++)
    {
        if(a[i].first-a[i].second>kiss)
        {
            kiss=a[i].first;
            ans++;
        }
        else if(i==n-1||a[i].first+a[i].second<a[i+1].first)
        {
            kiss=a[i].first+a[i].second;
            ans++;
        }
        else
            kiss=a[i].first;
    }
    printf("%d",ans);
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
启动Windows防火墙提示“0x8007042c\&quot;发布时间:2022-07-13
下一篇:
C#多线程技术总结(同步)发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap