Toj- Dominoes Game

 Dominoes Game

描述

Recently, my lovely daughter YuYu indulges in playing a game called dominoes. When the game starts, I first place some blocks with varying heights in a long row. I told her that she can select only one of them and topple it to fall either left or right. If the falling block hits another one, it will cause it to fall over too (if the top of a falling block just touches the bottom of another one, that block still stays standing). YuYu is so gready that she wants to knock over as many blocks as possible. Now, your task is to write a program and help YuYu to find the maximum number of blocks she can knock over.

输入

There will be multiple test cases. Each test case starts with an integer n indicating the number of blocks.
The next line contains n(1<=n<=10000) pairs of integers. Each pair, x h, indicates the position x of a block of height h (x>=0, h>0 and all numbers will be no larger than 32767).
You must help YuYu to select the block to initially knock over (and in what direction) so as to knock over as many blocks as possible.
Input will terminate with an integer 0 on a line.

输出

For each input test case, output a line containing an integer m indicating the maximum number of blocks that can be knocked over.

样例输入

 

2
0 2 3 3
3
1 2 2 3 3 2
0

样例输出

 

1
3

// 可以跨着推倒 1 1 3 1 5 5 的话虽然3 1 不能推倒 1 1 但是5 5可以所以结果应该是3

所以只要现在牌的高度=max(前一个牌的高度减去前一个牌到现在牌的距离),(现在牌的高度)就可以了因为有正推和反推所以用两个数组避免影响。

#include<bits/stdc++.h>
using namespace std;
struct jilu
{
    int x,h;
}node[10001],node1[10001];

bool cmp(jilu a,jilu b)
{
    return a.x<b.x;
}
int main()
{
   std::ios::sync_with_stdio(false);
   int n;
   while(cin>>n)
   {
       if(n==0)break;
       int max1=1;
       for(int i=0;i<n;i++)
       {
        cin>>node[i].x>>node[i].h;
        node1[i].x=node[i].x;
        node1[i].h=node[i].h;
       }
        sort(node,node+n,cmp);
        sort(node1,node1+n,cmp);
      int sum=1;
       for(int i=1;i<n;i++)
       {
           if(node[i].x-node[i-1].x<node[i-1].h)
           {
               sum++;
               node[i].h=max(node[i-1].h-(node[i].x-node[i-1].x),node[i].h);
           }

           else
            sum=1;
           if(sum>max1)
                max1=sum;
       }
       sum=1;
        for(int i=n-1;i>0;i--)
       {
           if(node1[i].x-node1[i-1].x<node1[i].h)
           {
              sum++;
              node1[i-1].h=max(node1[i].h-(node1[i].x-node1[i-1].x),node1[i-1].h);
           }
           else
            sum=1;
           if(sum>max1)
                max1=sum;
       }
       cout<<max1<<endl;
   }
}

 

 

转载于:https://www.cnblogs.com/xbqdsjh/p/11563491.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值