1. 程式人生 > 其它 >Codeforces Round #788 (Div. 2)

Codeforces Round #788 (Div. 2)

比賽連結:

https://codeforces.com/contest/1670

C. Where is the Pizza?

題目大意:

給定兩個排列 \(a\)\(b\),有一個排列 \(c\)\(c[i]\) = \(a[i]\)\(b[i]\),已知 \(c\) 的某些位置的值,求出有多少個可能的排列 \(c\),結果對 \(1e9 + 7\) 取模。

思路:

如果 \(c[i]\)\(a[i]\),那麼滿足 \(a[j] = b[i]\) 的位置 \(j\)\(c[j]\) 一定是 \(b[i]\)
所以所有的元素會構成一個個環,求的就是環的數量。

程式碼:

#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int N = 1e5 + 10, mod = 1e9 + 7;
int T, n, fa[N], sz[N];
int find(int x){
	if (fa[x] == x) return x;
	return fa[x] = find(fa[x]);
}
void join(int a, int b){
	a = find(a), b = find(b);
	if (a == b) return;
	if (a > b) swap(a, b);
	fa[b] = a;
	sz[a] += sz[b];
}
void solve(){
	cin >> n;
	for (int i = 1; i <= n; i ++ ){
		fa[i] = i;
		sz[i] = 1;
	}
	vector <int> a(n), b(n), c(n), d(n + 1);
	vector <bool> st(n + 1, false);
	for (int i = 0; i < n; i ++ )
		cin >> a[i];
	for (int i = 0; i < n; i ++ ){
		cin >> b[i];
		join(a[i], b[i]);
	}
	for (int i = 0; i < n; i ++ ){
		cin >> c[i];
		st[find(c[i])] = true;
	}
	LL ans = 1;
	for (int i = 1; i <= n; i ++ )
		if (find(i) == i && sz[i] > 1 && !st[i])
			ans = ans * 2 % mod;
	cout << ans << "\n";
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	cin >> T;
	while (T -- )
		solve();
	return 0;
}

D. Very Suspicious

題目大意:

無數個六邊形在平面上拼接起蜂窩狀,可以新增與邊平行的直線,給一個 \(n\),問最少新增多少條直線可以使平面中的等邊三角形大於等於 \(n\)
\(ps\):等邊三角形內部不能有直線。

思路:

直線只有三個方向,如果添加了 \(i\) 條直線,將 \(i\) 平均分配給每一個方向即可(可以看官方的證明)。
容易得到兩個方向的直線就可以產生兩個等邊三角形。
設三個方向的直線的數量分別為 \(a, b, c\)。產生 \(2 * (a * b + b * c + c * a)\) 個等邊三角形。
五萬多條直線就可以產生超過 1e9 個三角形,數量小,所以暴力預處理

一下,再二分查詢即可。

程式碼:

#include <bits/stdc++.h>
using namespace std;
int T, n;
vector <int> ans;
void init(){
	for (int i = 0; ; i ++ ){
		int a = i / 3, b = i / 3, c = i / 3, res = i % 3;
		if (res == 2) a ++ , b ++ ;
		else if (res == 1) a ++ ;
		int x = a * b + a * c + b * c;
		ans.push_back(2 * x);
		if (2 * x > 1e9) break;
	}
}
void solve(){
	cin >> n;
	cout << lower_bound(ans.begin(), ans.end(), n) - ans.begin() << "\n";
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	init();
	cin >> T;
	while (T -- )
		solve();
	return 0;
}