CF1861B Two Binary Strings

去洛谷看我的博客

思路

最近熬夜打 CF,视力下降了。没看到题目里给了第一位和最后一位必定是 0 0 0 1 1 1,导致想了半天。

考虑枚举字符串的位置 i i i

首先如果遇到了两个串第 i i i 位都是 1 1 1,那么可以直接覆盖中间的片段,一定能成功。

如果遇到不同的位置,考虑找到最近的 0 0 0 与第一位的 0 0 0 做操作,选最近的是为了减少损失相同 1 1 1 的可能。如果找不到就肯定无解。

AC code

#include<bits/stdc++.h>
using namespace std;
int T,n,flag,xz;
char s1[5005],s2[5005];
int main()
{
	scanf("%d",&T);
	while(T--)
	{
		scanf("%s%s",s1+1,s2+1),n=strlen(s1+1),flag=0;
		for(int i=1;i<=n;++i)
		{
			if(s1[i]=='1'&&s2[i]=='1'){break;}
			if(s1[i]!=s2[i])
			{
				int ff=0;
				for(int j=i+1;j<n;++j)
					if(s1[j]==s2[j]&&s1[j]=='0')
					{
						ff=1,i=j;break;
					}
				if(!ff)
				{
					flag=1;break;
				}
			}
		}
		if(flag) printf("NO\n");
		else printf("YES\n");
	}
	return 0;
}
 
To merge two binary max - heaps, one common approach is as follows: ### Algorithm 1. Combine the elements of the two binary max - heaps into a single array. 2. Convert this combined array into a new binary max - heap. This can be done using the heapify operation. The heapify operation starts from the last non - leaf node and works its way up to the root, ensuring that the max - heap property is satisfied at each step. Here is a Python code example to illustrate the process: ```python def heapify(arr, n, i): largest = i l = 2 * i + 1 r = 2 * i + 2 if l < n and arr[l] > arr[largest]: largest = l if r < n and arr[r] > arr[largest]: largest = r if largest != i: arr[i], arr[largest] = arr[largest], arr[i] heapify(arr, n, largest) def build_max_heap(arr): n = len(arr) for i in range(n // 2 - 1, -1, -1): heapify(arr, n, i) def merge_max_heaps(heap1, heap2): combined = heap1 + heap2 build_max_heap(combined) return combined ``` ### Complexity Analysis - **Time Complexity**: - Combining the two heaps into a single array takes $O(m + n)$ time, where $m$ and $n$ are the number of elements in the two binary max - heaps respectively. - Building a max - heap from an array using the heapify operation has a time complexity of $O(m + n)$. The heapify operation on an array of size $N$ has a time complexity of $O(N)$ because the sum of the heights of all the nodes in a complete binary tree of $N$ nodes is $O(N)$. So, overall, the time complexity of merging two binary max - heaps is $O(m + n)$. - **Space Complexity**: - A new array is created to store the combined elements of the two heaps. So, the space complexity is $O(m + n)$ as well.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值