Codeforces Round #748 (Div. 3) D2. Half of Same(数学 枚举 思维)

linkkkk

题意:

给出 n ( n < = 40 ) n(n<=40) n(n<=40)个数,问能否确定一个 k k k使得在有限的次数里执行操作使得数组里至少有一半的数是相同的。每次操作指的是选出一些数让其减小 k k k。求最大的 k k k,如果 k k k可以无限大输出 − 1 -1 1.

思路:

  • 枚举数组内两两元素差值,对这些差值求两两之间的 g c d gcd gcd,枚举 g c d gcd gcd,看看有没有 n / 2 n/2 n/2元素的差值是该 g c d gcd gcd的倍数,如果是的话,说明 k k k可以等于该 g c d gcd gcd。求 g c d gcd gcd的原因是如果两个数之间的差值为该 g c d gcd gcd,那么一定可以通过若干次变换变为相等的数。
  • 还有一种思路就是枚举两两之间差值的因子。

代码:

#include<iostream>
#include<cstdio> 
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<sstream>
using namespace std;

typedef long long ll;

const int maxn = 55;

int n, a[maxn];

int gcd(int a, int b) {
	return b == 0 ? a : gcd(b, a % b);
}

int main() {
	int _; cin >> _;
	while (_--) {
		cin >> n;
		map<int, int>mp;
		for (int i = 1; i <= n; i++) cin >> a[i],mp[a[i]]++;
		bool flag = 0;
		for (int i = 1; i <= n; i++) {
			if (mp[a[i]] >= n / 2) {
				flag = 1; break;
			}
		}
		if (flag) {
			puts("-1"); continue;
		}

		set<int>sd;
		for (int i = 1; i <= n; i++)
			for (int j = i + 1; j <= n; j++)
				sd.insert(abs(a[i] - a[j]));

		set<int>sg;
		for (auto t1 : sd) {
			for (auto t2 : sd) {
				sg.insert(gcd(t1, t2));
			}
		}
		int ans = 0;
		for (auto it : sg) {
			if (!it) continue;
			for (int i = 1; i <= n; i++) {
				int tmp = 0;
				for (int j = 1; j <= n; j++) {
					int now = abs(a[i] - a[j]);
					if (now % it == 0) tmp++;
				}
				if (tmp >= n / 2) ans = max(ans, it);
			}
		}
		cout << ans << endl;

	}




	return 0;
}
#include<iostream>
#include<cstdio> 
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<sstream>
using namespace std;

typedef long long ll;

const int maxn = 55;

int n, a[maxn];

int main() {
	int _; cin >> _;
	while (_--) {
		cin >> n;
		map<int, int>mp;
		for (int i = 1; i <= n; i++) cin >> a[i],mp[a[i]]++;
		sort(a + 1, a + 1 + n);
		bool flag = 0;
		for (int i = 1; i <= n; i++) {
			if (mp[a[i]] >= n / 2) {
				flag = 1; break;
			}
		}
		if (flag) {
			puts("-1"); continue;
		}

		set<int>sd;
		for (int i = 1; i <= n; i++)
			for (int j = i + 1; j <= n; j++)
				sd.insert(abs(a[i] - a[j]));
		set<int>sy;
		for (auto it : sd) {
			for (int i = 1; i * i <= it; i++)
				if (it % i == 0) sy.insert(i),sy.insert(it/i);
		}
		int ans=1;
/*		for (auto it : sy) {
			cout << it << " " ;
		}
		puts("");*/
		for (auto it : sy) {//枚举因子
			for (int i = 1; i <= n/2+1; i++) {
				int tmp = 1;
				for (int j = i + 1; j <= n; j++) {
					int now = a[j] - a[i];
					if (now % it == 0) tmp++;
				}
				//if (it == 13) cout << tmp << endl;
				if (tmp >= n / 2) ans = it;
			}
		}
		cout << ans << endl;
	}




	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豆沙睡不醒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值