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;
}
相关推荐
int型码农25 分钟前
数据结构第八章(一) 插入排序
c语言·数据结构·算法·排序算法·希尔排序
利刃大大31 分钟前
【在线五子棋对战】二、websocket && 服务器搭建
服务器·c++·websocket·网络协议·项目
UFIT38 分钟前
NoSQL之redis哨兵
java·前端·算法
喜欢吃燃面39 分钟前
C++刷题:日期模拟(1)
c++·学习·算法
SHERlocked9342 分钟前
CPP 从 0 到 1 完成一个支持 future/promise 的 Windows 异步串口通信库
c++·算法·promise
怀旧,1 小时前
【数据结构】6. 时间与空间复杂度
java·数据结构·算法
积极向上的向日葵1 小时前
有效的括号题解
数据结构·算法·
GIS小天1 小时前
AI+预测3D新模型百十个定位预测+胆码预测+去和尾2025年6月7日第101弹
人工智能·算法·机器学习·彩票
_Itachi__1 小时前
LeetCode 热题 100 74. 搜索二维矩阵
算法·leetcode·矩阵
不忘不弃1 小时前
计算矩阵A和B的乘积
线性代数·算法·矩阵