Codeforces Round #670 (Div. 2) 深夜掉分(A - C題補題)

RioTian發表於2020-09-13

1406A. Subset Mex

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

Example

input

4
6
0 2 1 5 0 1
3
0 1 2
4
0 2 0 1
6
1 2 3 4 5 6

output

5
3
4
0

Note

In the first test case,\(A=\{0,1,2\},B=\{0,1,5\}\) is a possible choice.

In the second test case, \(A=\{0,1,2\},B=∅\) is a possible choice.

In the third test case, \(A=\{0,1,2\},B=\{0\}\) is a possible choice.

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

題意:

給定一個集合,並定義 \(mex\) 操作:集合中的最小非負數。

如:\(mex(\{1,4,0,2,2,1\})=3\)

求 集合分為兩部分的最大值:\(max( mex(A) + mex(B) )\)

思路:

通過維護兩個變數從0開始,如果有0、1、2、3...這樣的直接慢慢向上疊加

#include<bits/stdc++.h>
#define ms(a,b) memset(a,b,sizeof a)
using namespace std;
typedef long long ll;
const int N = 1e5 + 100;
ll n, a[N];
void solve() {
	cin >> n; 
	for (int i = 0; i < n; ++i)cin >> a[i];
	sort(a, a + n);
	ll m = 0, k = 0;
	for (int i = 0; i < n; ++i) {
		if (a[i] == m)m++;
		else if (a[i] == k)k++;
	}
	cout << m + k << endl;//m、k相當於兩個集合中的非負最小值
}

int main() {
	//freopen("in.txt", "r", stdin);
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	ll _; cin >> _;
	while (_--)solve();
}

1406B. Maximum Product

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

Example

input

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

output

-120
12
0
945

Note

In the first test case, choosing \(a1,a2,a3,a4,a5\) is a best choice: \((−1)⋅(−2)⋅(−3)⋅(−4)⋅(−5)=−120\).

In the second test case, choosing \(a1,a2,a3,a5,a6\) is a best choice: \((−1)⋅(−2)⋅(−3)⋅2⋅(−1)=12\).

In the third test case, choosing\(a1,a2,a3,a4,a5\) is a best choice: \((−1)⋅0⋅0⋅0⋅(−1)=0\).

In the fourth test case, choosing \(a1,a2,a3,a4,a6\) is a best choice: \((−9)⋅(−7)⋅(−5)⋅(−3)⋅1=945\).

題意:

給定 大小為n的一個陣列,求下標 \((i,j,k,l,t) (i<j<k<l<t).\) 使得\(a1,a2,a3,a4,a5\) 最大

思路:

一開始以為不能排序,搞得卡了好久。

先對所給陣列進行排序,這樣必然倒數5個最大,又因為存在負數的關係,所以也許 $ - * - $ 反而最大。詳情見程式碼

#include<bits/stdc++.h>
#define ms(a,b) memset(a,b,sizeof a)
using namespace std;
typedef long long ll;
const int N = 1e5 + 100;
ll n, a[N];
void solve() {
	cin >> n; ll ans[4] = {};
	for (int i = 1; i <= n; i++) cin >> a[i];
	sort(a + 1, a + n + 1);
	ans[0] = a[n] * a[n - 1] * a[n - 2] * a[n - 3] * a[n - 4];
	ans[1] = a[n] * a[n - 1] * a[n - 2] * a[1] * a[2];
	ans[2] = a[n] * a[1] * a[2] * a[3] * a[4];
	sort(ans, ans + 3); cout << ans[2] << endl;
}

int main() {
	//freopen("in.txt", "r", stdin);
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	ll _; cin >> _;
	while (_--)solve();
}

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

題目太長這裡不貼上了。

題意:

思路:

DFS搜尋,詳細待補。請先閱讀程式碼

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10;
int n;
int p1, p2, p3, c[N];
vector<int>g[N];
void dfs(int u, int fa) {
	c[u] = 1;
	for (auto v:g[u]) if (v != fa) {
		dfs(v, u), c[u] += c[v];
	}
	if (!p3) {
		if (c[u] == 1) p1 = fa, p2 = u;
		if (n - c[u] == c[u]) p3 = fa;
	}
}
signed main() {
	ios::sync_with_stdio(false);
	int t;
	cin >> t;
	while (t--) {
		cin >> n;
		p1 = p2 = p3 = 0;
		for (int i = 1; i <= n; i++) g[i].clear(), c[i] = 0;
		for (int i = n; --i;) {
			int u, v;
			cin >> u >> v;
			g[u].push_back(v);
			g[v].push_back(u);
		}
		dfs(1, -1);
		cout << p1 << ' ' << p2 << '\n' << p2 << ' ' << (p3 ? p3 : p1) << '\n';
	}
	return 0;
}

相關文章