Half of Same
题意#
略....
思路#
对于两个数字 ,假设二者可以通过减去一些 ,使其变得相同,那么
即 ,并且 最大为
因此我们可以通过枚举任意两个数字的差值的所有因子进行判断是否合法来更新最大值
CODE
/********************
Author: Nanfeng1997
Contest: Codeforces - Codeforces Round #748 (Div. 3)
URL: https://codeforces.ml/contest/1593/problem/D2
When: 2022-03-15 19:54:20
Memory: 256MB
Time: 1000ms
********************/
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
void solve() {
int n; cin >> n;
map<int, int> cnt;
vector<int> a(n); for(int &x: a) cin >> x, cnt[x] ++;
for(auto &[k, v]: cnt) if(v >= n / 2) {
cout << "-1\n";
return;
}
int ans = 0;
auto chk = [&] (int x) -> bool {
map<int, int> mp;
for(int i = 0; i < n; i ++ ) mp[((a[i] % x) + x) % x] ++;
for(auto &[k, v]: mp) if(v >= n / 2) return true;
return false;
};
sort(a.begin(), a.end());
for(int i = 0; i < n; i ++ )
for(int j = i + 1; j < n; j ++ ) {
int res = a[j] - a[i];
for(int k = 1; k * k <= res; k ++ ) {
if(res % k) continue;
if(chk(k)) ans = max(ans, k);
if(chk(res / k)) ans = max(ans, res / k);
}
}
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T; cin >> T;
while(T --) solve();
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现