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

 

 Woodcutters
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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.

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 test(s)
input
5
1 2
2 1
5 10
10 9
19 1
output
3
input
5
1 2
2 1
5 10
10 9
20 1
output
4
Note

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].

 

题意:

n个树,在x1,x2,。。。,xn的位置,树的高度依次是h1,h2,。。。,hn

求的是当把树砍倒时候,不占用相邻树的位置,最大砍树个数

 

可向左 向右砍,即树向左向右倒,很显然 当树的棵树大于1的时候,一定至少可以砍倒两棵树,位于最左和最右的两棵树可以直接砍倒

 

可以先考虑左砍树,再考虑右砍树

满足左砍树时候,不用考虑右砍树。

对xi 和 hi

 

左砍树 树最左可到  xi – hi

当 xi – hi> x[i-1] 时候左砍成立  x[i-1] 更新到x[i]

右砍树 树最右可到 x[i] + h[i]

当  x[i] + h[i] < x[i+1] 时候右砍成立  x[i] 更新到 x[i] + h[i]

 

 

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;

const int N = 110000;
const int oo = 0x3f3f3f3f;

struct node
{
    int x, h;
} a[N];

int main()
{
    int n;

    while(scanf("%d", &n)!=EOF)
    {
        int i, sum=2;

        memset(a, 0, sizeof(a));

        for(i=1; i<=n; i++)
            scanf("%d%d", &a[i].x, &a[i].h);

        if(n==1)
            printf("1\n");
        else
        {
            for(i=2; i<n; i++)
            {
                if(a[i-1].x<a[i].x-a[i].h)
                    sum++;
                else if(a[i].x+a[i].h<a[i+1].x)
                {
                    a[i].x += a[i].h;
                    sum++;
                }
            }

            printf("%d\n", sum);
        }
    }
    return 0;
}

 

题意:每棵树给出坐标和高度,可以往左右倒,也可以不倒
     问最多能砍到多少棵树

DP:dp[i][0/1/2] 表示到了第i棵树时,它倒左或右或不动能倒多少棵树 分情况讨论,若符合就取最大值更新,线性dp。

DP:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;

const int N = 110000;
const int oo = 0x3f3f3f3f;

struct node
{
    int x, h;
}a[N];

int dp[N][3]; /// 0~L 1~no 2~R

int main()
{
    int n;

    while(scanf("%d", &n)!=EOF)
    {
        int i;

        for(i=1; i<=n; i++)
            scanf("%d%d", &a[i].x, &a[i].h);

        memset(dp, 0, sizeof(dp));

        dp[1][0] = dp[1][1] = 1;
        if(a[2].x-a[1].x>a[1].h) dp[1][2] = 1;

        for(i=2; i<=n; i++)
        {
            int t = max(dp[i-1][0], max(dp[i-1][1], dp[i-1][2]));
            dp[i][1] = t;

            if(a[i].x-a[i-1].x>a[i].h)
                dp[i][0] = max(dp[i-1][0], dp[i-1][1]) + 1;
            if(a[i].x-a[i-1].x>a[i].h+a[i-1].h)
                dp[i][0] = max(dp[i][0], dp[i-1][2]+1);
            if(i<n && a[i+1].x-a[i].x>a[i].h)
                dp[i][2] = t + 1;
            if(i==n) dp[i][2] = t+1;
        }

        printf("%d\n", max(dp[n][0], max(dp[n][1], dp[n][2])));
    }
    return 0;
}

 

posted on 2015-11-26 19:59  栀蓝  阅读(255)  评论(0编辑  收藏  举报

levels of contents