本文已参与「新人创作礼」活动,一起开启掘金创作之路。
@TOC
Технокубок 2021 - Финал-B. Restore Modulo
传送门 Time Limit: 1 second Memory Limit: 256 megabytes
Problem Description
For the first place at the competition, Alex won many arrays of integers and was assured that these arrays are very expensive. After the award ceremony Alex decided to sell them. There is a rule in arrays pawnshop: you can sell array only if it can be compressed to a generator.
This generator takes four non-negative numbers , , , . and must be positive, non-negative and for it must be true that . The array of length is created according to the following rules:
- , here xmody denotes remainder of the division of by ;
- for all such that .
For example, if , , , and , then .
Price of such an array is the value of in this generator.
Alex has a question: how much money he can get for each of the arrays. Please, help him to understand for every array whether there exist four numbers , , , that generate this array. If yes, then maximize .
Input
The first line contains a single integer () — the number of arrays.
The first line of array description contains a single integer () — the size of this array.
The second line of array description contains integers ( ) — elements of the array.
It is guaranteed that the sum of array sizes does not exceed .
Output
For every array print:
Sample Input
6
6
1 9 17 6 14 3
3
4 2 2
3
7 3 4
3
2 2 4
5
0 1000000000 0 1000000000 0
2
1 1
Sample Onput
19 8
-1
-1
-1
2000000000 1000000000
0
题目大意
给你一个包含个数字的数组,让你找是否存在,使得:
如果存在,要找最大的。
解题思路
首先根据题目要求,需要满足以下条件:
然后根据和的关系,有,又有。那么只有两种情况:
- 或
即:
- 或
因此,我们需要关注和的差值。 不难发现,这样的差值最多有两个,再多就无法解出答案。
多说无益,上例子:
样例1:1 9 17 6 14 3
计算和的差值。分别为
8 8 -11 8 -11
(9-1
、17-9
、6-17
、14-6
、3-14
) 只有两种:8
和-11
因此我们得到方程组: 且 两个方程组需要同时满足。 解方程组: 若第一个方程组中成立,则第二个方程组中必须成立,得 若第一个方程组中成立,则第二个方程组中必须成立,得 第一个解满足题目要求,第二个解不满足要求,所以最大的是,此时,样例1输出19 8
样例2:4 2 2
计算和的差值。分别为
-2 0
(2-4
、2-2
) 只有两种:-2
和0
因此我们得到方程组: 且 两个方程组需要同时满足。 解方程组得: 或 都不满足条件,输出-1
样例3:7 3 4
计算得两种差值:
-4
和1
解得c=-1 m=-5
或c=1 m=5
显然第一组不符合题意,但是第二组可要小心啦 中的元素都是得到的,怎么可能比还大呢? 所以又产生一个条件:
样例4:2 2 4
计算得两种差值:
0
和2
解得c=0 m=-2
或c=2 m=2
第一组,第二组 不符合
样例5:0 1000000000 0 1000000000 0
计算得两种差值:
1000000000
和-1000000000
解得c=1000000000 m=2000000000
或c=-1000000000 m=-2000000000
第一组m=2000000000 c=1000000000
符合题意 第二组不符合题意
样例6:1 1
计算只得一种差值:
0
只有一个方程组: 当成立时,可以无穷大。 顾输出特殊数字0
AC代码
#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
int a[100010];
int main()
{
int N;
cin >> N;
while (N--)//N组测试数据
{
int n;
cd(n);//scanf("%d",&n);
int M = 0;
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
M = max(M, a[i]);//顺便记录最大值,因为m必须大于MAX{a}
}
int LeiJiChuXianDeDiff = 0; //控制在0,1,2
int ChuXianDeDiff[2];//出现的diff
bool canNot = 0; //canNot为1时代表不可以
for (int i = 1; i < n; i++)//计算差值
{
int thisDiff = a[i] - a[i - 1];//差值diff
if (!LeiJiChuXianDeDiff) //还没有出现过diff
{
LeiJiChuXianDeDiff++;//累计出现的diff
ChuXianDeDiff[0] = thisDiff;//记录下这个diff
}
else if (LeiJiChuXianDeDiff == 1) //出现过一个diff了
{
if (thisDiff != ChuXianDeDiff[0]) //这次的和上次的不一样
{
LeiJiChuXianDeDiff++;//累计出现的diff
ChuXianDeDiff[1] = thisDiff;//记录下这个diff
}
}
else //出现过2个diff了
{
if (thisDiff != ChuXianDeDiff[0] && thisDiff != ChuXianDeDiff[1]) //和出现过的两个diff都不一样
{
canNot = 1;//3个方程组不能同时满足
puts("-1");
break;
}
}
}
if (!canNot) //还存在可能
{
if (LeiJiChuXianDeDiff <= 1)//没有出现过diff或只出现过一个diff
{
puts("0");//只有一个方程组,m可以无穷大,输出0
}
else//出现了2个diff
{
int KeYiDeM = 0;//可以的m
int ans = 0;//答案m
int diff = ChuXianDeDiff[0];//第一个
int m = diff - ChuXianDeDiff[1];
int ansDiff = 0;//答案c
if (m > M && diff >= 0) //可以了一种情况
{
KeYiDeM++;
ans = m;
ansDiff = diff;
}
diff = ChuXianDeDiff[1];//更新diff
m = diff - ChuXianDeDiff[0];//更新m
if (m > M && diff >= 0) //可以了一种情况
{
KeYiDeM++;
if (KeYiDeM == 1) //就这一种情况
{
ans = m;
ansDiff = diff;
}
else //有两种情况,需要取最大
{
if (ans > m) //第一种情况的m更大
{
//不用动
}
else //第二种情况m更大
{
ans = m;
ansDiff = diff;
}
}
}
if (KeYiDeM) //不是0
{
printf("%d %d\n", ans, ansDiff);
}
else
{
puts("-1");
}
}
}
}
return 0;
}
总结
若存在,需满足:
- 出现的不同的的个数
- 大于所有的的最大值
同步发文于我的CSDN,原创不易,转载请附上原文链接哦~
Tisfy:blog.csdn.net/Tisfy/artic…