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][1,2,3], the sequence directly above it would be [3, 5][3,5], and the top of the pyramid would be [8][8]:
However, I am not interested in completing the pyramid – instead, I would much rather go underground. Thus, for a sequence of nn non-negative integers, I will write down a sequence of n + 1n+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 Format
The input consists of:
- one line with the integer nn (1 \le n \le 10^6)(1≤n≤106), the length of the base sequence.
- one line with n integers a_1, \cdots , a_na1,⋯,an (0 \le ai \le 10^8(0≤ai≤108 for each i)i), forming the base sequence.
Output Format
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.
样例输入1
6 12 5 7 7 8 4
样例输出1
2
样例输入2
3 10 1000 100
样例输出2
0
题目来源
German Collegiate Programming Contest 2018
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <utility> #include <vector> #include <map> #include <queue> #include <stack> #include <cstdlib> #include <cmath> typedef long long ll; #define lowbit(x) (x&(-x)) #define ls l,m,rt<<1 #define rs m+1,r,rt<<1|1 using namespace std; #define pi acos(-1) int n; const int N=1e6+9; const ll inf=1e14+4; ll a[N]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%lld",&a[i]); } ll l=0,r=0; ll maxx=inf,minn=0;//一定要>=0 for(int i=1;i<=n;i++){ if(i%2==1){ l=a[i]-r; maxx=min(maxx,l); } if(i%2==0){ r=a[i]-l; minn=max(minn,r*(-1)); } } //解的范围[minn,maxx] /* 如 12 5 7 11 x 12-x -7+x 14-x x-3 令x==0 0 12 -7 14 -3 [7,12] */ if(minn>maxx){ printf("0\n"); } else{ printf("%lld\n",maxx-minn+1); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现