Codeforces Round #748 (Div. 3)
比賽連結
Codeforces Round #748 (Div. 3)
D2. Half of Same
Polycarp has an array of \(n\left(n\right.\) is even) integers \(a_{1}, a_{2}, \ldots, a_{n}\). Polycarp conceived of a positive integer \(k\). After that, Polycarp began performing the following operations on the array: take an index \(i(1 \leq i \leq n)\)
After Polycarp performed some (possibly zero) number of such operations, it turned out that at least half of the numbers in the array became the same. Find the maximum \(k\) at which such a situation is possible, or print \(-1\) if such a number can be arbitrarily large.
Input
The first line contains one integer \(t(1 \leq t \leq 10)\) - the number of test cases. Then \(t\) test cases follow.
Each test case consists of two lines. The first line contains an even integer \(n(4 \leq n \leq 40)\) ( \(n\) is even). The second line contains \(n\) integers \(a_{1}, a_{2}, \ldots a_{n}\left(-10^{6} \leq a_{i} \leq 10^{6}\right)\)
It is guaranteed that the sum of all \(n\) specified in the given test cases does not exceed \(100\) .
Output
For each test case output on a separate line an integer \(k(k \geq 1)\) - the maximum possible number that Polycarp used in operations on the array, or \(-1\), if such a number can be arbitrarily large.
Example
input
4
6
48 13 22 -15 16 35
8
-1 0 1 -1 0 1 -1 0
4
100 -1000 -1000 -1000
4
1 1 1 1
output
13
2
-1
-1
解題思路
思維
顯然,對於選出來的任意兩個數 \(a,b\),有 \(a\%k=b\%k\),即 \((a-b)\%k=0\),可以列舉選出來的最小值,將所有數減去這個最小值,這些數對 \(k\) 取模的模數都為 \(0\),即列舉所有差值的因子,如果其出現的次數大於等於 \(n/2\),則該因子滿足條件,此時更新答案,注意,如果出現其他數和最小值相等,則該數必選,因此時差值為 \(0\),肯定滿足條件
- 時間複雜度:\(O(n^2\times max(\{a_i\}))\)
程式碼
// Problem: D2. Half of Same
// Contest: Codeforces - Codeforces Round #748 (Div. 3)
// URL: https://codeforces.com/contest/1593/problem/D2
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
// %%%Skyqwq
#include <bits/stdc++.h>
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
template <typename T> void inline read(T &x) {
int f = 1; x = 0; char s = getchar();
while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
x *= f;
}
const int N=45;
int t,n,a[N];
vector<int> Div(int n)
{
vector<int> res;
for(int i=1;i<=n/i;i++)
if(n%i==0)
{
res.pb(i);
if(i*i!=n)
res.pb(n/i);
}
return res;
}
int main()
{
help;
for(cin>>t;t;t--)
{
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
sort(a+1,a+1+n);
bool f=false;
for(int i=1;i+n/2-1<=n;i++)
if(a[i]==a[i+n/2-1])
{
f=true;
break;
}
if(f)
{
puts("-1");
continue;
}
int res=0;
for(int i=1;i<=n;i++)
{
vector<int> b;
int tt=0;
for(int j=1;j<=n;j++)
if(a[j]>a[i])b.pb(a[j]-a[i]);
else if(a[i]==a[j])tt++;
unordered_map<int,int> mp;
for(int i:b)
for(int j:Div(i))
mp[j]++;
for(auto t:mp)
if(t.se>=n/2-tt)res=max(res,t.fi);
}
cout<<res<<'\n';
}
return 0;
}