1406A. Subset Mex

https://codeforces.com/contest/1406/problem/A

Codeforces Round #670 (Div. 2) 深夜掉分(A - C题补题) - 图1

Example

input

  1. 4
  2. 6
  3. 0 2 1 5 0 1
  4. 3
  5. 0 1 2
  6. 4
  7. 0 2 0 1
  8. 6
  9. 1 2 3 4 5 6

output

  1. 5
  2. 3
  3. 4
  4. 0

Note

In the first test case,Codeforces Round #670 (Div. 2) 深夜掉分(A - C题补题) - 图2 is a possible choice.

In the second test case, Codeforces Round #670 (Div. 2) 深夜掉分(A - C题补题) - 图3 is a possible choice.

In the third test case, Codeforces Round #670 (Div. 2) 深夜掉分(A - C题补题) - 图4 is a possible choice.

In the fourth test case,$ A={1,3,5},B={2,4,6}$ is a possible choice.

题意:

给定一个集合,并定义 Codeforces Round #670 (Div. 2) 深夜掉分(A - C题补题) - 图5 操作:集合中的最小非负数。

如:Codeforces Round #670 (Div. 2) 深夜掉分(A - C题补题) - 图6%3D3#card=math&code=mex%28%5C%7B1%2C4%2C0%2C2%2C2%2C1%5C%7D%29%3D3)

求 集合分为两部分的最大值:Codeforces Round #670 (Div. 2) 深夜掉分(A - C题补题) - 图7%20%2B%20mex(B)%20)#card=math&code=max%28%20mex%28A%29%20%2B%20mex%28B%29%20%29)

思路:

通过维护两个变量从0开始,如果有0、1、2、3…这样的直接慢慢向上叠加

  1. #include<bits/stdc++.h>
  2. #define ms(a,b) memset(a,b,sizeof a)
  3. using namespace std;
  4. typedef long long ll;
  5. const int N = 1e5 + 100;
  6. ll n, a[N];
  7. void solve() {
  8. cin >> n;
  9. for (int i = 0; i < n; ++i)cin >> a[i];
  10. sort(a, a + n);
  11. ll m = 0, k = 0;
  12. for (int i = 0; i < n; ++i) {
  13. if (a[i] == m)m++;
  14. else if (a[i] == k)k++;
  15. }
  16. cout << m + k << endl;//m、k相当于两个集合中的非负最小值
  17. }
  18. int main() {
  19. //freopen("in.txt", "r", stdin);
  20. ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  21. ll _; cin >> _;
  22. while (_--)solve();
  23. }

1406B. Maximum Product

https://codeforces.com/contest/1406/problem/B

Codeforces Round #670 (Div. 2) 深夜掉分(A - C题补题) - 图8

Example

input

  1. 4
  2. 5
  3. -1 -2 -3 -4 -5
  4. 6
  5. -1 -2 -3 1 2 -1
  6. 6
  7. -1 0 0 0 -1 -1
  8. 6
  9. -9 -7 -5 -3 -2 1

output

  1. -120
  2. 12
  3. 0
  4. 945

Note

In the first test case, choosing Codeforces Round #670 (Div. 2) 深夜掉分(A - C题补题) - 图9 is a best choice: Codeforces Round #670 (Div. 2) 深夜掉分(A - C题补题) - 图10%E2%8B%85(%E2%88%922)%E2%8B%85(%E2%88%923)%E2%8B%85(%E2%88%924)%E2%8B%85(%E2%88%925)%3D%E2%88%92120#card=math&code=%28%E2%88%921%29%E2%8B%85%28%E2%88%922%29%E2%8B%85%28%E2%88%923%29%E2%8B%85%28%E2%88%924%29%E2%8B%85%28%E2%88%925%29%3D%E2%88%92120).

In the second test case, choosing Codeforces Round #670 (Div. 2) 深夜掉分(A - C题补题) - 图11 is a best choice: Codeforces Round #670 (Div. 2) 深夜掉分(A - C题补题) - 图12%E2%8B%85(%E2%88%922)%E2%8B%85(%E2%88%923)%E2%8B%852%E2%8B%85(%E2%88%921)%3D12#card=math&code=%28%E2%88%921%29%E2%8B%85%28%E2%88%922%29%E2%8B%85%28%E2%88%923%29%E2%8B%852%E2%8B%85%28%E2%88%921%29%3D12).

In the third test case, choosingCodeforces Round #670 (Div. 2) 深夜掉分(A - C题补题) - 图13 is a best choice: Codeforces Round #670 (Div. 2) 深夜掉分(A - C题补题) - 图14%E2%8B%850%E2%8B%850%E2%8B%850%E2%8B%85(%E2%88%921)%3D0#card=math&code=%28%E2%88%921%29%E2%8B%850%E2%8B%850%E2%8B%850%E2%8B%85%28%E2%88%921%29%3D0).

In the fourth test case, choosing Codeforces Round #670 (Div. 2) 深夜掉分(A - C题补题) - 图15 is a best choice: Codeforces Round #670 (Div. 2) 深夜掉分(A - C题补题) - 图16%E2%8B%85(%E2%88%927)%E2%8B%85(%E2%88%925)%E2%8B%85(%E2%88%923)%E2%8B%851%3D945#card=math&code=%28%E2%88%929%29%E2%8B%85%28%E2%88%927%29%E2%8B%85%28%E2%88%925%29%E2%8B%85%28%E2%88%923%29%E2%8B%851%3D945).

题意:

给定 大小为n的一个数组,求下标 Codeforces Round #670 (Div. 2) 深夜掉分(A - C题补题) - 图17%20(i%3Cj%3Ck%3Cl%3Ct).#card=math&code=%28i%2Cj%2Ck%2Cl%2Ct%29%20%28i%3Cj%3Ck%3Cl%3Ct%29.) 使得Codeforces Round #670 (Div. 2) 深夜掉分(A - C题补题) - 图18 最大

思路:

一开始以为不能排序,搞得卡了好久。

先对所给数组进行排序,这样必然倒数5个最大,又因为存在负数的关系,所以也许 $ - * - $ 反而最大。详情见代码

  1. #include<bits/stdc++.h>
  2. #define ms(a,b) memset(a,b,sizeof a)
  3. using namespace std;
  4. typedef long long ll;
  5. const int N = 1e5 + 100;
  6. ll n, a[N];
  7. void solve() {
  8. cin >> n; ll ans[4] = {};
  9. for (int i = 1; i <= n; i++) cin >> a[i];
  10. sort(a + 1, a + n + 1);
  11. ans[0] = a[n] * a[n - 1] * a[n - 2] * a[n - 3] * a[n - 4];
  12. ans[1] = a[n] * a[n - 1] * a[n - 2] * a[1] * a[2];
  13. ans[2] = a[n] * a[1] * a[2] * a[3] * a[4];
  14. sort(ans, ans + 3); cout << ans[2] << endl;
  15. }
  16. int main() {
  17. //freopen("in.txt", "r", stdin);
  18. ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  19. ll _; cin >> _;
  20. while (_--)solve();
  21. }

1406C. Link Cut Centroids

https://codeforces.com/contest/1406/problem/C

题目太长这里不粘贴了。

题意:

思路:

DFS搜索,详细待补。请先阅读代码

  1. #include <bits/stdc++.h>
  2. #define int long long
  3. using namespace std;
  4. const int N = 1e5 + 10;
  5. int n;
  6. int p1, p2, p3, c[N];
  7. vector<int>g[N];
  8. void dfs(int u, int fa) {
  9. c[u] = 1;
  10. for (auto v:g[u]) if (v != fa) {
  11. dfs(v, u), c[u] += c[v];
  12. }
  13. if (!p3) {
  14. if (c[u] == 1) p1 = fa, p2 = u;
  15. if (n - c[u] == c[u]) p3 = fa;
  16. }
  17. }
  18. signed main() {
  19. ios::sync_with_stdio(false);
  20. int t;
  21. cin >> t;
  22. while (t--) {
  23. cin >> n;
  24. p1 = p2 = p3 = 0;
  25. for (int i = 1; i <= n; i++) g[i].clear(), c[i] = 0;
  26. for (int i = n; --i;) {
  27. int u, v;
  28. cin >> u >> v;
  29. g[u].push_back(v);
  30. g[v].push_back(u);
  31. }
  32. dfs(1, -1);
  33. cout << p1 << ' ' << p2 << '\n' << p2 << ' ' << (p3 ? p3 : p1) << '\n';
  34. }
  35. return 0;
  36. }