zoukankan      html  css  js  c++  java
  • 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[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/1背包问题(DP)
    冒泡排序
    快速排序
    最长上升子序列
    二分查找
    n后问题
    crontab 定时任务
    删除以某字符串开头的表
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4516132.html
Copyright © 2011-2022 走看看