Codeforces Round #748 (Div. 3)
思路:
能被25整除的数的最后两位一定只有四种情况,00,50,25,75。所以我们只要
1.找到一个0时,从当前位置开始往前找,找到第一个0或者第一个5,
2.找到一个5时,从当前位置开始往前找,找到第一个2或者第一个7,
此时更新一下res,更新方法为与(n - j - 1)这个数取min(下标从1开始)。
原因为我们要删的为j这个位置之后的数,而之后的数的总数为n - j,但其中包含一位符合要求的最后一位不能删,所以为n - j - 1
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <queue> #include <set> #include <vector> #include <unordered_map> #define x first #define y second using namespace std; typedef long long LL; typedef pair<int, int> PII; const int N = 200010; const int MOD = 1000000007; int a[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); int T; scanf("%d", &T); while (T--) { char s[20]; scanf("%s", s + 1);//最好用scanf,这里cin有意外 int n = strlen(s + 1); //00 50 25 75 int res = 18; for (int i = n; i >= 1; i -- ) { if(s[i] == '0') { for (int j = i - 1; j >= 1; j -- ) if(s[j] == '5' || s[j] == '0') res = min(res, n - j - 1); } if(s[i] == '5') { for (int j = i - 1; j >= 1; j -- ) if(s[j] == '2' || s[j] == '7') res = min(res, n - j - 1); } } cout << res << endl; } return 0; }
思路:
考虑先进行set去重,如果只有一种元素,那么k = 0,则输出− 1
其他情况,就在set排完序后,进行对差的gcd操作,因为如果要全部一样,那么就要差全为0
让x 个差只减一个数全变成0 ,就是求这x 数的gcd
主要学习一下对这些差的gcd操作,先让res = a[2] - a[1],然后循环从3开始
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <queue> #include <set> #include <vector> #include <unordered_map> #define x first #define y second using namespace std; typedef long long LL; typedef pair<int, int> PII; const int N = 45; const int MOD = 1000000007; int a[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); int T; cin >> T; while (T--) { int n, m = 0; cin >> n; set<int> s;//用于去重 for (int i = 0; i < n; i ++ ) { int x; cin >> x; if(s.count(x) == 0) { s.insert(x); a[++m] = x; } } if(m == 1) { cout << -1 << endl; continue; } sort(a + 1, a + m + 1); int res = a[2] - a[1]; for (int i = 3; i <= m; i ++ ) res = __gcd(res, a[i] - a[i - 1]); cout << res << endl; } return 0; }
思路:
和D1思路类似,枚举数组内两两元素差值,对这些差值求两两之间的gcd,枚举gcd,看看有没有n / 2元素的差值是该gcd的倍数,如果是的话,说明k可以等于该gcd。求gcd的原因是如果两个数之间的差值为该gcd,那么一定可以通过若干次变换变为相等的数。
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <set> #include <vector> #include <map> #include <unordered_set> #include <unordered_map> #define x first #define y second #define IOS ios::sync_with_stdio(false);cin.tie(0); using namespace std; typedef long long LL; typedef pair<int, int> PII; const int N = 45, M = 200010, MOD = 1000000007, INF = 0x3f3f3f3f; int a[N]; int main() { IOS; int T; cin >> T; while(T -- ) { int n; cin >> n; map<int, int> mp; for (int i = 1; i <= n; i ++ ) { cin >> a[i]; mp[a[i]]++; } bool flag = false; for (int i = 1; i <= n; i ++ ) if(mp[a[i]] >= n / 2) flag = true; if(flag) { cout << -1 << endl; continue; } set<int> s;//去重 for (int i = 1; i <= n; i ++ ) for (int j = i + 1; j <= n; j ++ ) s.insert(abs(a[i] - a[j])); set<int> K;//所有的k for(auto x : s) for(auto y : s) K.insert(__gcd(x, y)); int res = 0; for(auto k : K) { if(k == 0) continue; for (int i = 1; i <= n; i ++ ) { int cnt = 0; for (int j = 1; j <= n; j ++ ) { int tmp = abs(a[i] - a[j]); if(tmp % k == 0) cnt++; } if(cnt >= n / 2) res = max(res, k); } } cout << res << endl; } return 0; }

浙公网安备 33010602011771号