Codeforces Round 983 (Div. 2)

A. Circuit

#include <bits/stdc++.h>

using namespace std;

using i32 = int32_t;
using i64 = long long;


#define int i64

using vi = vector<int>;
using pii = pair<int,int>;

void solve() {
	int n;
	cin >> n;
	int cnt = 0;
	for(int i = 1, x, N = n * 2; i <= N; i ++ ) {
		cin >> x;
		cnt += (x == 1);
	}
	int res = cnt & 1;
	int ret = cnt;
	if(cnt > n) ret = n - (cnt - n);
	cout << res << " " << ret << "\n";
}

i32 main(){
	ios::sync_with_stdio(false), cin.tie(nullptr);
	int T;
	cin >> T;
	while(T --)
		solve();
	return 0;
}

B. Medians

只分成三段,且中间一段的中位数是 \(k\) 就好了。中间一段的长度只有 \(1,3\)两种情况。

#include <bits/stdc++.h>

using namespace std;

using i32 = int32_t;
using i64 = long long;


#define int i64

using vi = vector<int>;
using pii = pair<int,int>;

void solve() {
	int n, k;
	cin >> n >> k;
	if(k * 2 - 1 == n) {
		cout << 1 << "\n";
		cout << 1 << "\n";
	} else if(k % 2 == 0) {
		cout << 3 << "\n";
		cout << 1 << " " << k << " " << k + 1 << "\n";
	} else {
		if(k - 1 >= 2 and k + 2 <= n) {
			cout << 3 << "\n";
			cout << 1 << " " << k - 1 << " " << k + 2 << "\n";
		} else {
			cout << "-1\n";
		}
	}
	return;
}

i32 main(){
	ios::sync_with_stdio(false), cin.tie(nullptr);
	int T;
	cin >> T;
	while(T --)
		solve();
	return 0;
}

C. Trinity

考虑枚举最大值,次大值一定满足大于最大值的一半,然后再单独考虑一下最小值。

#include <bits/stdc++.h>

using namespace std;

using i32 = int32_t;
using i64 = long long;


#define int i64

using vi = vector<int>;
using pii = pair<int,int>;

void solve() {
	int n;
	cin >> n;

	vi a(n);
	for(auto &i: a) cin >> i;

	ranges::sort(a);
	
	int res = 0;
	for(int i = 0, j, x, t; i < n; i ++) {
		x = a[i] / 2;
		j = ranges::upper_bound(a,x) - a.begin();
		t = i - j + 1;
		if(j - 1 >= 0 and a[j - 1] + a[j] > a[i]) t ++;
		res = max(res, t);
	}
	cout << n - res << "\n";
	return;
}


i32 main(){
	ios::sync_with_stdio(false), cin.tie(nullptr);
	int T;
	cin >> T;
	while(T --)
		solve();
	return 0;
}

D. Genokraken

其实根据题目可以确定两个限制,每一条链都是递增的,BFS 层序遍历也是递增的,除了根之外每个点至多只有一个儿子。

我们用队列记录叶子点,然后按照递增顺序枚举,每次从队列中取出一个点。如果这个点没有子节点,则这个点将不会有子节点。如果有子节点,则不满足 BFS 序递增。

#include <bits/stdc++.h>

using namespace std;

using i32 = int32_t;
using i64 = long long;


#define int i64

using vi = vector<int>;
using pii = pair<int,int>;

void solve() {
	int n;
	cin >> n;
	vector<int> p(n + 1);
	p[1] = 0;

	queue<int> q;
	int t = -1;
	for(int i = 2; i < n; i ++){
		cout << "? 1 " << i << endl << flush;
		int r;
		cin >> r;
		if(r == 0) {
			p[i] = 1;
			q.push(i);
			t = i;
			break;
		} else {
			p[i] = 0;
			q.push(i);
		}
	}
	
	for(int i = t + 1; i < n; i ++){
		while(true) {
			int x = q.front();
			q.pop();
			cout << "? " << x << " " << i << endl << flush;
			int r;
			cin >> r;
			if(r) continue;
			p[i] = x;
			q.push(i);
			break;
		}
	}
	cout << "!";
	for(int i = 1; i < n; i ++)
		cout << " " << p[i];
	cout << endl << flush;
	return;
}


i32 main(){
	int T;
	cin >> T;
	while(T --)
		solve();
	return 0;
}
posted @ 2024-11-03 16:51  PHarr  阅读(13)  评论(0编辑  收藏  举报