当前位置:网站首页>B. Difference Array--Codeforces Round #808 (Div. 1)
B. Difference Array--Codeforces Round #808 (Div. 1)
2022-08-01 22:42:00 【秦小咩】
B. Difference Array
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given an array aa consisting of nn non-negative integers. It is guaranteed that aa is sorted from small to large.
For each operation, we generate a new array bi=ai+1−aibi=ai+1−ai for 1≤i<n1≤i<n. Then we sort bb from small to large, replace aa with bb, and decrease nn by 11.
After performing n−1n−1 operations, nn becomes 11. You need to output the only integer in array aa (that is to say, you need to output a1a1).
Input
The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. The description of the test cases follows.
The first line of each test case contains one integer nn (2≤n≤1052≤n≤105) — the length of the array aa.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤a1≤…≤an≤5⋅1050≤a1≤…≤an≤5⋅105) — the array aa.
It is guaranteed that the sum of nn over all test cases does not exceed 2.5⋅1052.5⋅105, and the sum of anan over all test cases does not exceed 5⋅1055⋅105.
Output
For each test case, output the answer on a new line.
Example
input
Copy
5 3 1 10 100 4 4 8 9 13 5 0 0 0 8 13 6 2 4 8 16 32 64 7 0 0 0 0 0 0 0
output
Copy
81 3 1 2 0
Note
To simplify the notes, let sort(a)sort(a) denote the array you get by sorting aa from small to large.
In the first test case, a=[1,10,100]a=[1,10,100] at first. After the first operation, a=sort([10−1,100−10])=[9,90]a=sort([10−1,100−10])=[9,90]. After the second operation, a=sort([90−9])=[81]a=sort([90−9])=[81].
In the second test case, a=[4,8,9,13]a=[4,8,9,13] at first. After the first operation, a=sort([8−4,9−8,13−9])=[1,4,4]a=sort([8−4,9−8,13−9])=[1,4,4]. After the second operation, a=sort([4−1,4−4])=[0,3]a=sort([4−1,4−4])=[0,3]. After the last operation, a=sort([3−0])=[3]a=sort([3−0])=[3].
一道细节题,细节很多
重点是0的讨论
0 0 0 8 13为例
初始
三个零 + 8 13
差分 两个零 8 5
排序 两个零, 5 8
差分 一个零 5 3
排序 一个零 3 5
差分 没有0 3 2
排序 没有0 2 3
差分 1
我们宏观统计序列0的个数,一旦有0,就以为我们差分时第一个非零数字是要被加进去的。规律是差分一次零就会减少1,且差分过程中会出现零。
一个致命的细节是,等差数列,在差分中全变成0,需要特判。
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+100;
int T,n;
int a[N],b[N];
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int cnt=0,len=0;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(int i=1; i<=n; i++)
{
int x;
cin>>x;
if(!x)
cnt++;
else
{
len++;
a[len]=x;
}
}
while(len>1)
{
int newl=0;
if(cnt)
{
newl++;
cnt--;
b[newl]=a[1];
}
for(int i=2; i<=len; i++)
{
if(a[i]-a[i-1])
{
newl++;
b[newl]=a[i]-a[i-1];
}
else
{
cnt++;
}
}
sort(b+1,b+1+newl);
for(int i=1; i<=newl; i++)
{
a[i]=b[i];
}
len=newl;
}
if(len==1)
cout<<a[1]<<endl;
else
{
cout<<0<<endl; //特判全部清空
}
}
return 0;
}
边栏推荐
- 2022年最新河北建筑八大员(机械员)模拟考试题库及答案
- 字符串——Trie
- 美赞臣EDI 940仓库装运订单详解
- 从0到1:图文投票小程序设计与研发笔记
- 小程序毕设作品之微信美食菜谱小程序毕业设计成品(8)毕业设计论文模板
- 联邦学习入门
- 深度学习Course2第一周Practical aspects of Deep Learning习题整理
- 小程序毕设作品之微信体育馆预约小程序毕业设计成品(4)开题报告
- Graph Theory - Strongly Connected Component Condensation + Topological Sort
- 自建 Prometheus 采集腾讯云容器服务监控数据最佳实践
猜你喜欢
SQL29 Calculate the average next day retention rate of users

2022 edition of MySQL tutorial, top collection good, take your time

Codeforces CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) A-D 题解

Flutter基础学习(一)Dart语言入门

编曲软件FL studio20.8中文版功能和作用

小程序容器+自定义插件,可实现混合App快速开发

小程序毕设作品之微信体育馆预约小程序毕业设计成品(3)后台功能

2022年最新河北建筑八大员(机械员)模拟考试题库及答案

y84. Chapter 4 Prometheus Factory Monitoring System and Actual Combat -- Advanced Prometheus Alarm Mechanism (15)

SRv6 L3VPN的工作原理
随机推荐
SRv6 L3VPN的工作原理
2022年最新河北建筑八大员(机械员)模拟考试题库及答案
将vim与系统剪贴板的交互使用
小程序毕设作品之微信美食菜谱小程序毕业设计成品(7)中期检查报告
Codeforces CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) A-D Solution
数据分析04
Error creating bean with name ‘dataSource‘:Unsatisfied dependency expressed through field ‘basicPro
selenium无头,防检测
Prufer sequence
企业公众号文章写作方向:如何写出读者认可的优质内容
SOM Network 1: Principles Explained
[Mobile Web] Mobile terminal adaptation
毫秒级!千万人脸库快速比对,上亿商品图片检索,背后的极速检索用了什么神器?
论文解读(GSAT)《Interpretable and Generalizable Graph Learning via Stochastic Attention Mechanism》
使用Jenkins做持续集成,这个知识点必须要掌握
[Niu Ke brush questions-SQL big factory interview questions] NO4. Travel scene (a taxi)
Deep learning Course2 first week Practical aspects of Deep Learning exercises
vscode hide menu bar
如何理解 new (...args: any[]) => any
华为无线设备配置双链路冷备份(AP指定配置方式)