Codeforces Round 958 (Div. 2)(A~C)题

A. Split the Multiset

思路:

最优的策略是每次操作分出 k−1𝑘−1 个 1,然后考虑最后是否会剩下一个单独的 1。

代码:

#include<bits/stdc++.h>
using namespace std;
#define N 1000005
typedef long long ll;
typedef unsigned long long ull;
ll n, m, t, h, k;
ll a, b, c;
ll ans, num, sum1=0, sum, sum2=0, cnt, maxx, minn = 1e9;
ll dp[N], f1[N], f2[N];
ll mp[105][105];
bool flag, vis[N];
string s;
void solve() {
	ll n, k, sum = 0;
	cin >> n >> k;
	while (n > k) {
		n -= (k - 1);
		sum++;
	}
	if (n != 1) sum++;
	cout << sum << endl;
}
int main()
{
	cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}

B. Make Majority

思路:

首先对全 0 连续段进行操作使它们均变为一个 0,先尽可能减少 0 的个数,然后判断是否有 c1>c0𝑐1>𝑐0 即可.

代码:

#include<bits/stdc++.h>
using namespace std;
#define N 1000005
typedef long long ll;
typedef unsigned long long ull;
ll n, m, h, k;
ll a, b, c;
ll ans, num, sum1=0, sum, sum2=0, cnt, maxx, minn = 1e9;
ll dp[N], f1[N], f2[N];
ll mp[105][105];
bool flag, vis[N];
string s;
string t;
void solve() {
	ll n, k, sum = 0;
	cin >> n >> k;
	while (n > k) {
		n -= (k - 1);
		sum++;
	}
	if (n != 1) sum++;
	cout << sum << endl;
}
int main()
{
	cin >> k;
	while (k--) {
		s.clear();
		t.clear();
		cin >> n >> s;
		for (int i = 0; i < s.size(); ) {
			if (s[i] != '0') {
				t += s[i];
				i++;
			}
			else {
				int j = i + 1;
				t += s[i];

				while (j < s.size() && s[j] == '0') {
					j++;
				}
				i = j;
			}
		}
		ll ans1 = 0;
		ll ans2 = 0;
		for (auto it : t) {
			if (it == '0') {
				ans1++;
			}
			else {
				ans2++;
			}
		}
		if (ans2 > ans1) {
			cout << "Yes" << endl;
		}
		else {
			cout << "No" << endl;
		}
	}
	return 0;
}

C. Increasing Sequence with Fixed OR

思路:

要构造的数组严格递增,那么最后一个位置一定可以放n,为了保证严格单调递增,直接低位枚举n的二进制位即可,假设当前枚举到第i位时n的二进制位为1,那么把n-(1<<i)加入答案即可

代码:

#include<bits/stdc++.h>
using namespace std;
#define N 1000005
typedef long long ll;
typedef unsigned long long ull;
ll n, m, h, k;
ll a, b, c;
ll ans, num, sum1=0, sum, sum2=0, cnt, maxx, minn = 1e9;
ll dp[N], f1[N], f2[N];
ll mp[105][105];
bool flag, vis[N];
string s;
string t;
void solve() {
	cin >> n;
	ans = 0;
	if (n - (n & -n) == 0)
	{
		cout << 1 << endl << n << endl;
		return;
	}
	for (int i = 60; i >= 0; i--) {
		if ((n >> i) & 1) {
			++ans;
			f1[ans] = n - pow(2, i);
		}
	}
	cout << ans + 1 << endl;
	sort(f1 + 1, f1 + 1 + ans);
	for (int i = 1; i <= ans; i++)
		cout<< f1[i] << " ";
	cout << n << endl;
}
int main()
{
	cin >> k;
	while (k--) {
		solve();
	}
	return 0;
}
相关推荐
程序猿进阶1 小时前
如何在 Visual Studio Code 中反编译具有正确行号的 Java 类?
java·ide·vscode·算法·面试·职场和发展·架构
Eloudy1 小时前
一个编写最快,运行很慢的 cuda gemm kernel, 占位 kernel
算法
king_machine design1 小时前
matlab中如何进行强制类型转换
数据结构·算法·matlab
西北大程序猿1 小时前
C++ (进阶) ─── 多态
算法
无名之逆1 小时前
云原生(Cloud Native)
开发语言·c++·算法·云原生·面试·职场和发展·大学期末
头发尚存的猿小二1 小时前
树——数据结构
数据结构·算法
好蛊1 小时前
第 2 课 春晓——cout 语句
c++·算法
山顶夕景2 小时前
【Leetcode152】分割回文串(回溯 | 递归)
算法·深度优先·回溯
紫钺-高山仰止2 小时前
【Matlab】matlab 结构体使用方法
数据结构·算法·matlab
夜幕龙2 小时前
robomimic基础教程(三)——自带算法
人工智能·python·算法·机器人