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;
}
相关推荐
linweidong1 天前
C++如何避免 ODR(One Definition Rule)冲突?
java·jvm·c++
朔北之忘 Clancy1 天前
第二章 分支结构程序设计(3)
c++·算法·青少年编程·竞赛·教材·考级·讲义
想逃离铁厂的老铁1 天前
Day42 >> 188、买卖股票的最佳时机IV + 309.最佳买卖股票时机含冷冻期 + 714.买卖股票的最佳时机含手续费
算法·leetcode·职场和发展
uoKent1 天前
构造析构综合练习
c++
wu_asia1 天前
方阵对角线元素乘积计算
数据结构·算法
想逃离铁厂的老铁1 天前
Day43 >> 300.最长递增子序列 + 674. 最长连续递增序列+ 718. 最长重复子数组
数据结构·算法
Yzzz-F1 天前
P6648 [CCC 2019] Triangle: The Data Structure [st表]
算法
FL16238631291 天前
[C++][cmake]基于C++在windows上onnxruntime+opencv部署yolo26-seg的实例分割onnx模型
c++·windows·opencv
LateFrames1 天前
泰勒级数:从 “单点” 到 “理论与实践的鸿沟”
学习·算法
武帝为此1 天前
【RC4加密算法介绍】
网络·python·算法