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;
}
相关推荐
孤飞3 小时前
zero2Agent:面向大厂面试的 Agent 工程教程,从概念到生产的完整学习路线
算法
lclin_20204 小时前
VS2010兼容|C++系统全能监控工具(彩色界面+日志带单位+完整版)
c++·windows·系统监控·vs2010·编程实战
技术专家5 小时前
Stable Diffusion系列的详细讨论 / Detailed Discussion of the Stable Diffusion Series
人工智能·python·算法·推荐算法·1024程序员节
csdn_aspnet5 小时前
C# (QuickSort using Random Pivoting)使用随机枢轴的快速排序
数据结构·算法·c#·排序算法
鹿角片ljp5 小时前
最长回文子串(LeetCode 5)详解
算法·leetcode·职场和发展
广师大-Wzx6 小时前
一篇文章看懂MySQL数据库(下)
java·开发语言·数据结构·数据库·windows·python·mysql
paeamecium7 小时前
【PAT甲级真题】- Cars on Campus (30)
数据结构·c++·算法·pat考试·pat
UrSpecial7 小时前
从零实现C++轻量线程池
c++·线程池
chh5638 小时前
C++--模版初阶
c语言·开发语言·c++·学习·算法
RTC老炮8 小时前
带宽估计算法(gcc++)架构设计及优化
网络·算法·webrtc