Codeforces Round 958 (Div. 2)补题

文章目录

A题 (拆分多集)

本题在赛时卡的时间比较久,把这题想复杂了,导致WA了两次。后来看明白之后就是将n每次转换成k-1个1,到最后分不出来k-1个1直接一次就能分完,即结果加一;

cpp 复制代码
#include <bits/stdc++.h>
#define int long long 
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define fi first
#define se second
#define PII pair <int,int>
#define ALL(x) x.begin(),x.end()
#define lowbit(x) (x&(-x))
using namespace std;
const int N = 1e6+5;
int a[N],b[N];

void solve () {
	int n,k;
	cin>>n>>k;
	if(n==1) {
		cout<<0<<'\n';return;
	}
	int pos=0;
	while (n>k) {
		n-=k-1;
		pos++;
	} 
	cout<<pos+1<<'\n';
}

signed main () {
	IOS;
	int T =1;
	cin>>T;
	while(T--) solve ();
	return 0;
}

B题(获得多数票)

题意就是给出一段字符串,字符串里面只包含0,1。然后可以找到 [ l , r ] [l,r] [l,r]区间里面,将区间里面的字符串变成一个字符,这个字符是0或1,到底是哪个,图片中有详细描述。

思路:将字符串里面连续的0全部转化成一个0,再比较0,1个数

cpp 复制代码
#include <bits/stdc++.h>
#define int long long 
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define fi first
#define se second
#define PII pair <int,int>
#define ALL(x) x.begin(),x.end()
#define lowbit(x) (x&(-x))
using namespace std;
const int N = 1e6+5;
int a[N],b[N];

void solve () {
	int n;cin>>n;
	string s;cin>>s;
	int cnt=0,ans=0;int j;
	for (int i=0;s[i]!='\0';) {
		if (s[i]=='0') {
			cnt++;
			j=i;
			while (s[j]=='0')j++;
			i=j;
		}
		else {
			i++;ans++;
		}
	} 
	if (cnt>=ans)cout<<"NO"<<'\n';
	else cout<<"YES"<<'\n';	
}

signed main () {
	IOS;
	int T =1;
	cin>>T;
	while(T--) solve ();
	return 0;
}

C题(固定 OR 的递增序列)

题意:给一个数字n,写出一个序列,要求递增,并且两项之间或运算为n

思路:利用二进制的特点,将0,1填入,最后逆序输出

cpp 复制代码
#include <bits/stdc++.h>
#define int long long 
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define fi first
#define se second
#define PII pair <int,int>
#define ALL(x) x.begin(),x.end()
#define lowbit(x) (x&(-x))
using namespace std;
const int N = 1e6+5;
int a[N],b[N];

void solve () {
	int n;cin>>n;
	if (n==1||n==2) {
		cout<<1<<'\n'<<1<<'\n';return ;
	}a[0]=n;
	int k=0;
	int t=n,pos=1;
	while (t) {
		if (t&1) {
			a[++k]=n-pos;
		}
		pos*=2;t/=2;
	}
	if (a[k]==0) {
		cout<<1<<'\n'<<n<<'\n';return;
	}
	cout<<k+1<<'\n';
	for (int i=k;i>=0;i--) {
		cout<<a[i]<<' ';
	}
	cout<<'\n';
}

signed main () {
	IOS;
	int T =1;
	cin>>T;
	while(T--) solve ();
	return 0;
}
相关推荐
2301_763472463 分钟前
C++20概念(Concepts)入门指南
开发语言·c++·算法
阿猿收手吧!19 分钟前
【C++】std::promise原理与实战解析
c++
abluckyboy1 小时前
Java 实现求 n 的 n^n 次方的最后一位数字
java·python·算法
园小异1 小时前
2026年技术面试完全指南:从算法到系统设计的实战突破
算法·面试·职场和发展
m0_706653231 小时前
分布式系统安全通信
开发语言·c++·算法
Zach_yuan1 小时前
深入浅出 JSONCpp
linux·服务器·网络·c++
寻寻觅觅☆1 小时前
东华OJ-基础题-104-A == B ?(C++)
开发语言·c++
lightqjx2 小时前
【C++】unordered系列的封装
开发语言·c++·stl·unordered系列
天天爱吃肉82182 小时前
跟着创意天才周杰伦学新能源汽车研发测试!3年从工程师到领域专家的成长秘籍!
数据库·python·算法·分类·汽车
alphaTao2 小时前
LeetCode 每日一题 2026/2/2-2026/2/8
算法·leetcode