• 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;
       }
    }

     

  • 相关阅读:
    数据分析之可反复与独立样本的T-Test分析
    朗朗上口的两幅对联
    mysql编码、数据表编码查看和改动总结
    2014-04-19编程之美初赛题目及答案解析
    测试集群模式安装实施Hadoop
    笔记:常用排序算法
    笔记:常用排序算法
    安装Redis并测试
    常见架构风格举例总结
    转载:PostgreSQL SQL的性能提升
  • 原文地址:https://www.cnblogs.com/xbqdsjh/p/11563491.html
Copyright © 2020-2023  润新知