zoukankan      html  css  js  c++  java
  • Gym 102021D : Down the Pyramid(思维)

      Do you like number pyramids? Given a number sequence that represents the base, you are usually supposed to build the rest of the “pyramid” bottom-up: For each pair of adjacent numbers, you would compute their sum and write it down above them. For example, given the base sequence [1, 2, 3], the sequence directly above it would be [3, 5], and the top of the pyramid would be [8]:

                 

      However, I am not interested in completing the pyramid – instead, I would much rather go underground. Thus, for a sequence of n non-negative integers, I will write down a sequence of n + 1 non-negative integers below it such that each number in the original sequence is the sum of the two numbers I put below it. However, there may be several possible sequences or perhaps even none at all satisfying this condition. So, could you please tell me how many sequences there are for me to choose from?

    Input

    The input consists of:

    • one line with the integer n (1 ≤ n ≤ 106 ), the length of the base sequence.

    • one line with n integers a1, . . . , an (0 ≤ ai ≤ 108 for each i), forming the base sequence.

    Output

    Output a single integer, the number of non-negative integer sequences that would have the input sequence as the next level in a number pyramid.

    Sample Input 1           Sample Output 1

    6                2

    12 5 7 7 8 4

    Sample Input 2           Sample Output 2

    3               0

    10 1000 100 

    概述:给你一个序列,根据这个序列,写出它下一层金字塔的可能出现的序列数目。

    分析: 假设给出n=4的序列b1,b2,b3,b4.你要求的就是它下方的一层a1,a2,a3,a4,a5.

        明显有

          b1=a1+a2,b2=a2+a3,b3=a3+a4,b4=a4+a5;

        从而得到

          a1=a1,

          a2=b1-a1,

          a3=b2-a2=b2-b1+a1,

          a4=b3-a3=b3-b2+b1-a1,

          a5=b4-a4=b4-b3+b2-b1+a1;

        这时可以发现a序列的确定仅与a1相关,换句话说求a序列的数目也就是求a1的数目。问题转换成了求解求a1的范围使得a1~a5都≥0。

     

    话不多说,上代码  

     1 #include <iostream>
     2 #include <algorithm>
     3 using namespace std;
     4 const int INF = 0x3f3f3f3f;
     5 const int maxn = 1000000 + 5;
     6 int b[maxn];
     7 int main()
     8 {
     9     int n;
    10     cin >> n;
    11     for (int i = 1; i <= n;i++)
    12         cin >> b[i];
    13     int mina1 = 0, maxa1 = INF;
    14     int temp = 0;
    15     for (int i = 1; i <= n;i++)
    16     {
    17         temp = b[i] - temp;
    18         if(i%2)
    19             maxa1 = min(maxa1, temp);   
    20         else
    21             mina1 = max(mina1, -temp);       
    22     }
    23     cout << mina1 << " " << maxa1 << endl;
    24     if(maxa1>=mina1)
    25         cout << maxa1 - mina1 + 1;
    26     else
    27         cout << 0;
    28     return 0;
    29 }
    View Code

      

  • 相关阅读:
    C#_委托
    一个asp+ACCESS省市二级联动菜单程序
    asp怎么实现二级联动下拉菜单
    ASP用户登录代码
    Windows 2003 IIS 不支持ASP的问题
    iptables
    VNC ( Virtual Network Computing )
    MongoDB的备份(mongodump)与恢复(mongorestore)
    Locally managed (LMT) vs. Dictionary managed (DMT) tablespace
    向现有的磁盘组加入/删除ASM磁盘
  • 原文地址:https://www.cnblogs.com/chen-tian-yuan/p/10601761.html
Copyright © 2011-2022 走看看