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;
}
相关推荐
载数而行5204 小时前
QT的五类布局
c++·qt·学习
故事和你914 小时前
sdut-程序设计基础Ⅰ-实验五一维数组(8-13)
开发语言·数据结构·c++·算法·蓝桥杯·图论·类和对象
载数而行5204 小时前
QT的QString类
c++·qt·学习
像污秽一样4 小时前
算法与设计与分析-习题4.2
算法·排序算法·深度优先·dfs·bfs
Storynone5 小时前
【Day20】LeetCode:39. 组合总和,40. 组合总和II,131. 分割回文串
python·算法·leetcode
bu_shuo5 小时前
Visual C++2010学习版(全国计算机等级二级考试版)安装记录
c++·cpp·visual c++·计算机二级
明明如月学长6 小时前
AI 更新太快学不过来?我用OpenClaw打造专属AI学习工作流
算法
黎阳之光6 小时前
【黎阳之光:以无线专网与视频孪生,赋能智慧广电与数字中国】
算法·安全·智慧城市·数字孪生
刀法如飞7 小时前
Agentic AI时代,程序员必备的算法思想指南
人工智能·算法·agent
刀法如飞7 小时前
Agentic AI时代程序员必备算法思想详解(附实战案例)
算法·ai编程·编程开发·agentic