寒假2024/2/20

div4怎么只能出5个(我是🤡)

codeforce928

A - Vlad and the Best of Five

题意:

看A和B的数量谁多。

#include <bits/stdc++.h>
using namespace std;

#define int long long

void solve() {
	string s;
	cin >> s;

	int na = 0, nb = 0;
	for (auto it : s) {
		if(it == 'A') {
			na++;
		}
		else {
			nb++;
		}
	}

	if(na > nb) {
		cout << "A\n";
	}
	else {
		cout << "B\n";
	}
}

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);

	int T = 1;
	cin >> T;

	while(T--) {
		solve();
	}

	return 0;
}

B - Vlad and Shapes

题意:

看输入的是矩形还是三角形。

思路:

我们不用跟着题目走,直接看三角形和矩形的不同点,就是如果有1的话,每一行1的个数相同的是矩形,不同的是三角形。

#include <bits/stdc++.h>
using namespace std;

#define int long long

void solve() {
	int n;
	cin >> n;
	vector<int>v;
	for (int i = 0; i < n; i++) {
		int cnt = 0;
		string s;
		cin >> s;

		for (auto c : s) {
			if (c == '1') {
				cnt++;
			}
		}
		if(cnt)
			v.push_back(cnt);
	}

	if(v[0] == v[1]) {
		cout << "SQUARE\n";
	}
	else {
		cout << "TRIANGLE\n";
	}
}

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);

	int T = 1;
	cin >> T;

	while(T--) {
		solve();
	}

	return 0;
}

C - Vlad and a Sum of Sum of Digits

题意:

给n,把1 - n中各个数字替换成各个位数的和,然后把替换后的数字相加。

思路:

起初我在找规律,但发现没有什么规律,看了一眼范围,觉得暴力能过,就是预处理一下就行。

#include <bits/stdc++.h>
using namespace std;

#define int long long
map<int, int>mp;

void solve() {
	int x;
	cin >> x;
	cout << mp[x] << endl;
}

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);

	int cnt = 0;
	for (int i = 1; i <= 2e5; i++) {
		int res = 0, t = i;
		while(t) {
			res += t % 10;
			t /= 10;
		}
		cnt += res;
		mp[i] = cnt;
	}

	int T = 1;
	cin >> T;

	while(T--) {
		solve();
	}

	return 0;
}

D - Vlad and Division

题意:

把n个数分成m组,要求每一组里的数字,每一个二进制位都不能相同,求m的最小值。

思路:

比较裸的一道哈希,但是要判断两个数字能不呢能分到一组,看二进制位我想到了按位取反,但是按位取反会把前面没用的符号位啥的都取反,我没玩明白,自己写了一个31位按位取反函数,最后哈希就过了。

#include <bits/stdc++.h>
using namespace std;

#define int long long

int f(int x) {

	int res = 0;
	for (int i = 0; i < 31; i++) {
		if(((x >> i) & 1) == 0) {
			res |= (1 << i);
		}
	}

	return res;
}

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

	map<int, int>mp;
	int cnt = 0;
	for (int i = 0, t; i < n; i++) {
		cin >> t;
		if(mp[f(t)] != 0) {
			mp[f(t)]--;
		}
		else {
			cnt++;
			mp[t]++;
		}
	}

	cout << cnt << endl;
}

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	
	int T = 1;
	cin >> T;

	while(T--) {
		solve();
	}

	return 0;
}

E - Vlad and an Odd Ordering

题意:

给n,依次排列1-n中1倍的奇数,2倍的奇数,3倍的奇数,4倍的奇数····

给k,求排列的第k个数是什么。

思路:

模拟一下,我们发现3倍的奇数不存在,因为3 x 奇数 一定是奇数,一倍的奇数,前面已经出现过了,同理,5倍七倍都没有,六倍也没有,因为6 = 2 x 3,六倍的奇数也就是2倍的奇数,所以我们猜测一下,只有2的幂次方倍的奇数才有。

然后通过推理猜测我发现第m倍层的第k个数是m * (2 * k - 1)

有这个公式,我们可以推断出每一层的个数,通过二分得到哪一层,第几个,最后输出就行。

我感觉是个数学味很浓的题。

#include <bits/stdc++.h>
using namespace std;

#define int long long

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

	int cnt = 0;
	vector<int>v;
	v.push_back(0);
	
	for (int m = 1; cnt < n; m *= 2) {
		cnt += (n + m) / (2 * m);
		v.push_back(cnt);
	}

	int l = 0, r = v.size();
	while(l < r) {
		int mid = (l + r) >> 1;
		if(v[mid] < k) {
			l = mid + 1;
		}
		else {
			r = mid;
		}
	}

	int la = v[l - 1];
	int t = l;
	int m = 1;
	for (int i = 0; i < t - 1; i++) {
		m *= 2;
	}
	
	cout << m * (2 * (k - la) - 1) << endl;
}

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);

	int T = 1;
	cin >> T;

	while(T--) {
		solve();
	}

	return 0;
}
posted @ 2024-02-20 00:47  contiguous  阅读(27)  评论(0编辑  收藏  举报