日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Half of Same 思维,模拟,调试

發(fā)布時(shí)間:2025/3/19 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Half of Same 思维,模拟,调试 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.


題意 :

  • 給一序列,每次操作可以讓一個(gè)元素減去k(k>=1),求最大的k使得若干次操作后序列中至少一半元素相等,若k為任意大,則輸出-1

思路 :

  • 首先枚舉任意兩個(gè)數(shù)的差,那么答案k一定存在于這些差的因數(shù)中,所以枚舉每組差的因數(shù),以當(dāng)前差的被減數(shù)為媒介(最終至少一半數(shù)等于這個(gè)數(shù))判斷當(dāng)前k是否滿足條件,取滿足的最大值
  • 特別地,當(dāng)原序列中有一個(gè)元素已經(jīng)大于等于元素的個(gè)數(shù)的一半(n為奇數(shù)是(n+1)/2,n為偶數(shù)是n/2,但由于向下取整,可以統(tǒng)一為(n+1)/2),那么k為任意大,輸出-1
#include <iostream> #include <algorithm> #include <cstring> #include <unordered_map> #define debug(a) cout << #a << " = " << a << endl; // 調(diào)試 #define x first #define y second using namespace std; typedef long long ll;const int N = 50;int n; int a[N];int update(int dif, int x) {int k = 0;for (int i = 1; i * i <= dif; i ++ ){if (dif % i) continue;int cnt = 0;for (int j = 1; j <= n; j ++ )if (abs(a[j] - x) % i == 0) cnt ++ ;if (cnt >= (n + 1) / 2) k = max(k, i);cnt = 0;for (int j = 1; j <= n; j ++ )if (abs(a[j] - x) % (dif / i) == 0) cnt ++ ;if (cnt >= (n + 1) / 2) k = max(k, dif / i);}return k; }int main() {ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);int _;cin >> _;while (_ -- ){unordered_map<int, int> ma;cin >> n;for (int i = 1; i <= n; i ++ ) cin >> a[i], ma[a[i]] ++ ;bool flag = false;for (auto i : ma)if (i.second >= (n + 1) / 2){flag = true;break;}if (flag){cout << -1 << endl;continue;}int k = 0;for (int i = 1; i <= n; i ++ )for (int j = i + 1; j <= n; j ++ ){int dif = abs(a[i] - a[j]);if (dif == 0) continue; // k>=1k = max(k, update(dif, a[i])); // debug(dif)debug(k) // 調(diào)試}cout << k << endl;}return 0; }

總結(jié)

以上是生活随笔為你收集整理的Half of Same 思维,模拟,调试的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。