CF Round410 C. Mike and gcd problem
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF Round410 C. Mike and gcd problem相关的知识,希望对你有一定的参考价值。
一奇一偶需要两次操作,两个奇数需要一次操作。
798D - Mike and distribution
In the beginning, it‘s quite easy to notice that the condition " 2·(ap1?+?...?+?apk) is greater than the sum of all elements in A " is equivalent to " ap1?+?...?+?apk is greater than the sum of the remaining elements in A ".
Now, let‘s store an array of indices C with Ci?=?i and then sort it in decreasing order according to array A, that is we must have ACi?≥?ACi?+?1.
Our answer will always have size . First suppose that N is odd. Add the first index to our set, that is make p1?=?C1. Now, for the remaining elements, we will consider them consecutively in pairs. Suppose we are at the moment inspecting AC2k and AC2k?+?1. If BC2k?≥?BC2k?+?1 we make pk?+?1?=?C2k, else we make pk?+?1?=?C2k?+?1.
Why does this subset work? Well, it satisfies the condition for B because each time for consecutive non-intersecting pairs of elements we select the bigger one, and we also add BC1 to the set, so in the end the sum of the selected elements will be bigger than the sum of the remaining ones.
It also satisfies the condition for A, because Ap1 is equal or greater than the complement element of p2 (that is — the index which we could‘ve selected instead of p2 from the above procedure — if we selected C2k then it would be C2k?+?1 and vice-versa). Similarly Ap2 is greater than the complement of p3 and so on. In the end we also add the last element from the last pair and this makes the sum of the chosen subset strictly bigger than the sum of the remaining elements.
The case when N is even can be done exactly the same as when N is odd, we just pick the last remaining index in the end.
The complexity is .
#include <iostream> #include <ctime> using namespace std; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int main() { ios::sync_with_stdio(0); int n; cin >> n; int g = 0, v, cnt = 0, ans = 0; while (n--) { cin >> v; g = gcd(g, v); if (v & 1) ++cnt; //记录连续的奇数的个数 else ans += (cnt / 2) + 2 * (cnt & 1), cnt = 0; } ans += (cnt / 2) + 2 * (cnt & 1), cnt = 0; cout << "YES" << endl; cout << (g == 1 ? ans : 0) << endl; cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << endl; return 0; }
以上是关于CF Round410 C. Mike and gcd problem的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #410 (Div. 2)C. Mike and gcd problem(数论)
Codeforces Round #410 (Div. 2)-A - Mike and palindrome
[卿学姐带飞系列]-Codeforces Round #410 (Div. 2)_B - Mike and strings