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。
话不多说,上代码
#include <iostream>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = + ;
int b[maxn];
int main()
{
int n;
cin >> n;
for (int i = ; i <= n;i++)
cin >> b[i];
int mina1 = , maxa1 = INF;
int temp = ;
for (int i = ; i <= n;i++)
{
temp = b[i] - temp;
if(i%)
maxa1 = min(maxa1, temp);
else
mina1 = max(mina1, -temp);
}
cout << mina1 << " " << maxa1 << endl;
if(maxa1>=mina1)
cout << maxa1 - mina1 + ;
else
cout << ;
return ;
}
Gym 102021D : Down the Pyramid(思维)的更多相关文章
- C - CodeCoder vs TopForces Gym - 101142C (连通块+思维)
题目链接: C - CodeCoder vs TopForces Gym - 101142C 题目大意:给你n个人的信息,每一个人的信息包括两个.t1和t2.A>B的前提是A的t1和t2至少有一 ...
- Gym 100971A Treasure Island BFS 思维题
A - Treasure Island Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64 ...
- Gym - 101350A Sherlock Bones(思维)
The great dog detective Sherlock Bones is on the verge of a new discovery. But for this problem, he ...
- Gym - 101572D Distinctive Character bfs 思维
题目传送门 题目大意: 给出n个01串,让你构造一个字符串,使这个字符串和这些字符串中相似程度最高 尽可能低.如果两个字符串对应位置相同,则相似程度加一. 思路: 每一个01串更改自己的一部分后,都可 ...
- Gym 100796B Wet Boxes(思维)题解
题意:给一个如图坐标系,每个方形都放在下面两个中间,已知一个木块湿了那么他下面所有的都会湿,显然,不能湿两次.问,每次给出一个坐标,把他弄湿,有几个木块从干变成湿了. 思路:我们把坐标系拉直,就变成了 ...
- Gym 101480I Ice Igloos(思维乱搞)题解
题意:给个最多500 * 500的平面,有半径最多不为1的n个圆,现在给你1e5条线段,问你每条线段和几个圆相交,时限10s 思路: 因为半径<1,那么我其实搜索的范围只要在线段附近就好了.x1 ...
- GCPC 2018
C.Coolest Ski Route 题意:n个点,m条边组成的有向图,求任意两点之间的最长路径 dfs记忆化搜索 #include<iostream> #include<stri ...
- 2017 ACM-ICPC EC-Final ShangHai 东亚洲大陆-上海
比赛链接:传送门 Gym 101775A Chat Group(签到:待补) Gym 101775B Scapegoat(待补) Gym 101775C Traffic Light(贪心+思维) 思路 ...
- UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)
UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...
随机推荐
- Python 进阶_迭代器 & 列表解析
目录 目录 迭代器 iter 内建的迭代器生成函数 迭代器在 for 循环中 迭代器与字典 迭代器与文件 创建迭代器对象 创建迭代对象并实现委托迭代 迭代器的多次迭代 列表解析 列表解析的样例 列表解 ...
- 用 Flask 来写个轻博客 (18) — 使用工厂模式来生成应用对象
Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 工厂模式 使用工厂方法 Factory Method 创建 app 对 ...
- appium常见问题11_小米手机初次启动app,报错255“Requires permission android.permission.WRITE_SECURE_SETTINGS”
问题: 新申请的测试机到啦,申请机型是小米9.打开开发者模式.USB调试后,连接电脑,准备跑一下自动化脚本.但是在pycharm中点击run后,出现报错,报错code:255,提示“Requires ...
- c#生成html静态文件时出现空白行,怎么去掉utf-8中的bom
public static void UTF8RemoveBOM(string filepath) { UTF8RemoveBOM(filepath, filepath); } public st ...
- u盘在电脑读不出来,但别的可以读
其实一般这种问题都是驱动程序问题导致的,解决方法很简单,不管是win7还是win10都适用: 插入u盘然后在设备管理里删掉设备重新插拔试试. 详细步骤是: 插上U盘 打开设备管理器 ...
- 各种Web服务器与Nginx的对比
Tomcat和Jetty面向Java语言,先天就是重量级的Web服务器,它们的性能与Nginx没有可比性. IIS只能在windows操作系统上运行,Windows作为服务器在稳定性与其他一些性能上都 ...
- 多线性方程组迭代算法——Gauss-Seidel迭代算法的Python实现
多线性方程组(张量)迭代算法的原理请看这里:原理部分请留言,不方便公开分享 Jacobi迭代算法里有详细注释:多线性方程组迭代算法——Jacobi迭代算法的Python实现 import numpy ...
- 在Ubuntu custom kernel上裝perf by compile
Using perf, the Linux Performance Analysis tool on Ubuntu Karmic A lot has been going on with Linux ...
- The Complete Javascript Number Reference 转载自:http://www.hunlock.com/blogs/The_Complete_Javascript_Number_Reference
The Complete Javascript Number Reference Filed: Mon, Apr 30 2007 under Programming|| Tags: reference ...
- k8s之ingress-nginx部署一直提示健康检查10254端口不通过问题就处理
之前部署了一套k8s集群,但是到部署ingress-nginx的时候,一直提示10254端口拒绝不通:如下图. 这是因为我之前装的是docker1.17.默认的驱动是systemd.因为systemd ...