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

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

linkkkk

题意:

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

思路:

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

还有一种思路就是枚举两两之间差值的因子。

代码:

#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;
}
目录
相关文章
|
6月前
|
机器学习/深度学习 人工智能 BI
The Great Hero(Codeforces Round #700 (Div. 2))模拟+贪心思想和排序
The Great Hero(Codeforces Round #700 (Div. 2))模拟+贪心思想和排序
64 0
Codeforces Round #747 (Div. 2) D. The Number of Imposters(扩展域并查集 带权并查集)
Codeforces Round #747 (Div. 2) D. The Number of Imposters(扩展域并查集 带权并查集)
118 0
Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861C Did you mean...【字符串枚举,暴力】
C. Did you mean... time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard out...
1134 0
Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861B Which floor?【枚举,暴力】
B. Which floor? time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output...
1324 0