CF 115 题解

这篇博客包含三道计算机编程竞赛题目:A. Computer Game、B. Groups 和 C. Delete Two Elements。每道题都给出了代码实现,并且涉及数组操作、条件判断和数学计算。A题要求检查两个字符串是否有相同位置的1;B题需要找到可以将数组分成两组的策略;C题则涉及双指针和集合操作来确定删除两个元素后的平均值。文章适合有一定编程基础的读者进行算法练习和理解。
摘要由CSDN通过智能技术生成

A. Computer Game

Problem - A - Codeforces (Unofficial mirror site, accelerated for Chinese users)

/*//
Problem: A. Computer Game
Contest: CF115
URL: https://codeforces.ml/contest/1598/problem/A
/*/
#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
using namespace std;
const int inf = 0x3f3f3f3f;
const ll ll_inf = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1e9 + 7;
const int maxn = 1e5 + 5;

int main(int argc, char const *argv[]) {
	ios::sync_with_stdio(false);
	cin.tie(0);     cout.tie(0);

	int T;
	cin >> T;
	while (T --) {
		int n;
		string s1, s2;
		cin >> n >> s1 >> s2;
		bool ok = true;
		for (int i = 0; i < n; i ++) {
			if (s1[i] == '1' && s2[i] == '1') {
				ok = false;
				break;
			}
		}
		if (ok) {
			puts("YES");
		} else {
			puts("NO");
		}
	}
	return 0;
}

 B. Groups

Problem - B - Codeforces (Unofficial mirror site, accelerated for Chinese users)

/*//
Problem:  B. Groups
Contest:  CF115
URL: https://codeforces.ml/contest/1598/problem/B
/*/
#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
using namespace std;
const int inf = 0x3f3f3f3f;
const ll ll_inf = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1e9 + 7;
const int maxn = 1e5 + 5;

int main(int argc, char const *argv[]) {
	ios::sync_with_stdio(false);
	cin.tie(0);     cout.tie(0);
	int T;
	cin >> T;
	while (T --) {
		int n;
		cin >> n;
		vector<int > a[n + 1];
		for (int i = 1; i <= n; i ++) {
			for (int j = 1; j <= 5; j ++ ) {
				int tmp;
				cin >> tmp;
				a[i].push_back(tmp);
			}
		}
		vector<int > b[5];
		for (int i = 0; i <= 4; i ++) {
			for (int j = 1; j <= n; j ++) {
				if (a[j][i] == 1) {
					b[i].push_back(j);
				}
			}
		}
		// for(int i = 0; i <= 4; i ++) {
		// 	for(int j = 0; j < b[i].size(); j ++) {
		// 		cout <<b[i][j] << " ";
		// 	}
		// 	cout << endl;
		// }
		vector<int > ok;
		for (int i = 0; i <= 4;  i++) {
			if (b[i].size() >= n / 2) {
				ok.push_back(i);
			}
		}
		// for(int i = 0; i < ok.size(); i ++) {
		// 	cout <<ok[i] << " ";
		// }
		// cout << "ok" << endl;
		if (ok.size() < 2) {
			cout << "NO" << endl;
		} else {
			bool flag = false;
			for (int i = 0; i < ok.size(); i ++) {
				for (int j = 0; j < ok.size(); j ++) {
					bool flag1 = true;
					if (j == i) {
						continue;
					}
					// cout << ok[i] << " " << ok[j] << " " << "ok" <<endl;
					for (int k = 1; k <= n; k ++) {
						if (!a[k][ok[i]] && !a[k][ok[j]]) {
							flag1 = false;
							break;
						}
					}
					if (flag1) {
						flag = true;
					}
				}
			}
			if (flag) {
				cout << "YES" << endl;
			} else {
				cout << "NO" << endl;
			}
		}
	}
	return 0;
}

C. Delete Two Elements

Problem - C - Codeforces (Unofficial mirror site, accelerated for Chinese users)

/*//
Problem: C. Delete Two Elements
Contest: CF115
URL: https://codeforces.ml/contest/1598/problem/C
/*/
#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
using namespace std;
const int inf = 0x3f3f3f3f;
const ll ll_inf = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1e9 + 7;
const int maxn = 1e5 + 5;

int main(int argc, char const *argv[]) {
	ios::sync_with_stdio(false);
	cin.tie(0);     cout.tie(0);
	int T;
	cin >> T;
	while (T --) {
		int n;
		cin >> n;
		double sum = 0;
		double k = 0;
		vector<double > a(n + 1);
		map<double, int > vis;
		for (int i = 1; i <= n; i ++) {
			cin >> a[i];
			vis[a[i]] ++;
			sum += a[i];
		}
		k = sum * 1.0 / n;
		ll ans = 0;
		for (int i = 1; i <= n; i ++) {
			vis[a[i]] --;
			double d = (double)(a[i] - k);
			if (vis[k - d]) {
				ans += vis[k - d];
			}
		}
		cout << ans << endl;
	}

	return 0;
}

这个题当时没有写出来, 看都没有看懂, 后面看一下 ,就是一个交互题, 就是测试数据难弄一些 其他就是一个巧妙的 1 ——  n 推算, 用 + i 和+ 1 确定每一位数就好。

/*//
Problem:
Contest:
URL:
/*/
#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
using namespace std;
const int inf = 0x3f3f3f3f;
const ll ll_inf = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1e9 + 7;
const int maxn = 1e5 + 5;

int main(int argc, char const *argv[]) {
	ios::sync_with_stdio(false);
	cin.tie(0);     cout.tie(0);
	int n;
	cin >> n;
	vector<int > p(n + 1);
	for (int i = 2; i <= n; i ++) {
		cout << "? ";
		for (int j = 1; j < n; j ++) {
			cout << i << " ";
		}
		cout << 1 << endl;
		int idx;
		cin >> idx;
		p[idx] = 1 - i;
	}

	for (int i = 2; i <= n; i ++) {
		cout << "? ";
		for (int j = 1; j < n; j ++) {
			cout << 1 << ' ';
		}
		cout << i << endl;
		int idx;
		cin >> idx;
		p[idx] = i - 1;
	}
	int min_c = inf;
	for(int i = 1; i <= n; i ++) {
		min_c = min(min_c, p[i]);
	}
	p[n] = abs(min_c) + 1;
	for (int i = n-1; i >= 1; i --) {
		p[i] = p[n] + p[i];
	}
	cout << "! ";
	for(int i = 1; i <= n; i ++) {
		cout << p[i] << " ";
	}
	cout << endl;
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值