Codeforces Round #619 (Div. 2) B. Motarack's Birthday
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array aa of nn non-negative integers.
Dark created that array 10001000 years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer kk (0≤k≤1090≤k≤109 ) and replaces all missing elements in the array aa with kk .
Let mm be the maximum absolute difference between all adjacent elements (i.e. the maximum value of |ai−ai+1||ai−ai+1| for all 1≤i≤n−11≤i≤n−1 ) in the array aa after Dark replaces all missing elements with kk .
Dark should choose an integer kk so that mm is minimized. Can you help him?
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 size of the array aa .
The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (−1≤ai≤109−1≤ai≤109 ). If ai=−1ai=−1 , then the ii -th integer is missing. It is guaranteed that at least one integer is missing in every test case.
It is guaranteed, that the sum of nn for all test cases does not exceed 4⋅1054⋅105 .
Output
Print the answers for each test case in the following format:
You should print two integers, the minimum possible value of mm and an integer kk (0≤k≤1090≤k≤109 ) that makes the maximum absolute difference between adjacent elements in the array aa equal to mm .
Make sure that after replacing all the missing elements with kk , the maximum absolute difference between adjacent elements becomes mm .
If there is more than one possible kk , you can print any of them.
Example
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
1 11
5 35
3 6
0 42
0 0
1 2
3 4 大意是给一个缺少一些数的序列,要求在每一个空缺的位置添上一个相同的数,使得这个序列中相邻两数之差的最大绝对值尽可能小。
首先把这个序列读入,然后遍历一遍,统计两个量:与空缺位置相邻的数里的最大值mmax和最小值mmin。
为什么要相邻呢?因为如果一个数不和空缺位置相邻的话,空缺位置不论填什么都和这个数没有关系,最后处理的时候再看它就行了。
然后令k=(mmax+mmin)/2,再次遍历一遍序列先把空缺位置填上k然后计算相邻两数之差并更新答案。
#include <bits/stdc++.h>
using namespace std;
int a[];
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
scanf("%d",&n);
int i;
memset(a,,sizeof(a));
int mmax=-;
int mmin=;
int cnt_neg=;
for(i=;i<=n;i++)scanf("%d",&a[i]);
for(i=;i<=n;i++)
{
//scanf("%d",&a[i]);必须要全读进去再处理,要不然a[i+1]还是0
if(a[i]==-)cnt_neg++;
if(i>&&a[i]==-&&a[i-]!=-)
{
mmax=max(mmax,a[i-]);
mmin=min(mmin,a[i-]);
}
if(i<n&&a[i]==-&&a[i+]!=-)
{
mmax=max(mmax,a[i+]);
mmin=min(mmin,a[i+]);
}
}
if(cnt_neg==n)
{
cout<<<<' '<<<<endl;
continue;
}
int k=(mmax+mmin)/;
int m=;
for(i=;i<=n;i++)if(a[i]==-)a[i]=k;
for(i=;i<=n;i++)
{
m=max(m,abs(a[i]-a[i-]));
}
cout<<m<<' '<<k<<endl;
}
return ;
}
Codeforces Round #619 (Div. 2) B. Motarack's Birthday的更多相关文章
- Codeforces Round #619 (Div. 2) A~D题解
最近网课也开始了,牛客上一堆比赛题目也没补,所以就D题后面的也懒得补了 A.Three String 水题 #include <cstdio> #include <cstring&g ...
- Codeforces Round #619 (Div. 2)
A. Three Strings 题意:给三个长度相同的非空字符串abc,依次将c中的每个字符和a或者b中对应位置的字符进行交换,交换必须进行,问能否使得ab相同. 思路:对于每一个位置,如果三个字符 ...
- Codeforces Round #619 (Div. 2)E思维+二维RMQ
题:https://codeforces.com/contest/1301/problem/E 题意:给个n*m的图形,q个询问,每次询问问询问区间最大的合法logo的面积是多少 分析:由于logo是 ...
- Codeforces Round #619 (Div. 2)D(模拟)
先把一种最长路线记录下来,根据k的大小存到ans中相应的答案再输出 #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using n ...
- Codeforces Round #619 (Div. 2)C(构造,容斥)
#define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; int main(){ ios::syn ...
- Codeforces Round #619 (Div. 2) Ayoub's function
Ayoub thinks that he is a very smart person, so he created a function f(s)f(s) , where ss is a binar ...
- Codeforces Round #619 (Div. 2) A. Three Strings
You are given three strings aa , bb and cc of the same length nn . The strings consist of lowercase ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
随机推荐
- Java初识与配置环境
Java初识 Java简介 Java是一门面向对象的程序设计语言.功能强大并且简单易用,极好的实现了面向对象理论.允许程序以类似人类的思维方式进行复杂的编程. Java具有简单性.面向对象.分布式.健 ...
- 查看和设置mysql字符集
http://218.194.248.2/~wuxiaogang/cpcourse/database/mysql/charset.htm 1. 修改mysql的my.cnf# vi /etc/my.c ...
- 开源镜像站,vmware下载
vmware下载:https://www.newasp.net/soft/345086.html 官网下载链接:https://www.centos.org/download/ http://mirr ...
- MaxM2引擎各种人物触发脚本
捡起物品触发@PickUpItem[@PickUpItem]#if#actSENDMSG 5 [%s]在[%m(%x,%y)]捡起物品[<$STR(E2)>] 精炼成功触发@REFINEM ...
- Word2010如何从指定页设置页码
光标定位:将光标定位于需要开始编页码的页首位置. 插入分隔符的”下一页”:选择“页面布局—>分隔符—> 下一页”插入. 插入页码:选择“插入—>页码—> 页面底端”,选 ...
- Nginx之server和location配置使用
配置server代码段: server { server_name www.meiduo.site; listen ; root /home/python/Desktop/meiduo_mall_ad ...
- for await of异步操作集合遍历
function Gen (time) { return new Promise((resolve,reject) => { setTimeout(function () { resolve(t ...
- es8中对string补白的方式
//允许将空字符串或其他字符串添加到原始字符串的开头或结尾for(let i = 1; i < 32; i++) { if(i < 10) { console.log(`0{i}`) }e ...
- JS json对象(Object)和字符串(String)互转方法
[JS json对象(Object)和字符串(String)互转方法] 参考:https://blog.csdn.net/wenqianla2550/article/details/78232706 ...
- Java - 闭包
概述 简单介绍 闭包 1. 聚合关系 概述 常见的 类间关系 场景 类 A 主要类 持有 类B 的实例 有点行为, 需要 类 B 的介入 类 B 有自己的行为 A 会在某些时候调用 B 的行为 代码示 ...