1566C - MAX-MEX Cut - CodeForces Solution


bitmasks constructive algorithms dp greedy *1000

Please click on ads to support us..

Python Code:

def max_mex(arr):
	ptr=0
	tot=0
	while ptr<len(arr[0]):
		if arr[0][ptr]!=arr[1][ptr]:
			tot+=2
			ptr+=1
		else:
			if arr[0][ptr]=="1" and ptr+1<len(arr[0]):
				if arr[0][ptr+1]!=arr[1][ptr+1]:
					ptr+=1
				elif arr[0][ptr+1]=="1":
					ptr+=1
				else:
					tot+=2
					ptr+=2
			elif arr[0][ptr]=="0" and ptr+1<len(arr[0]):
				if arr[0][ptr+1]!=arr[1][ptr+1]:
					tot+=1
					ptr+=1
				elif arr[0][ptr+1]=="1":
					ptr+=2
					tot+=2
				else:
					tot+=1
					ptr+=1
			else:
				tot+=1 if arr[0][ptr]=="0" else 0
				ptr+=1
	return tot
t=input()
for _ in range(int(t)):
	length=input()
	row_1=input()
	row_2=input()
	arr=[row_1,row_2]
	print(max_mex(arr))

C++ Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define lowbit(x) (x & (-x))
 
ll fastPow(ll a,ll n,ll mod){ll ans=1; a%=mod; while(n){ if(n&1) ans=(ans*a)%mod; a=(a*a)%mod; n>>=1;} return ans;}
int gcd(int a,int b){if(b) while((a%b)&&(b%=a));return a+b;}
int lcm(int a,int b){return a*b/gcd(a,b);}
int read(){int x=0,y=1;char s=getchar();while(s<'0'||s>'9'){if(s=='-')y=-1;s=getchar();}while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}return x*y;}

const int N=100005;
int a[N],b[N],c[N],n;

void solve(){
	cin>>n;
	int cnt=0,tmp=-1;
	string s;
	cin>>s;
	for(int i=1;i<=n;i++){
        a[i]=s[i-1]-'0';
	}
	cin>>s;
	for(int i=1;i<=n;i++){
        b[i]=s[i-1]-'0';
	}
	for(int i=1;i<=n;i++){
		c[i]=a[i]+b[i];
		if(c[i]==1){
			cnt=cnt+2;
			if(tmp==0)
			    cnt++;
			tmp=-1;
		}
		else if(tmp==-1)
		    tmp=c[i];
		else{
			if(tmp!=c[i]){
				cnt+=2;
				tmp=-1;
			}
			else{
				if(tmp==0)
				    cnt++;
				tmp=c[i];
			}
		}
	}
	if(tmp==0)
	    cnt++;
	cout<<cnt<<"\n"; 
	return;
}

int main(){
    //ios::sync_with_stdio(0);
	//cin.tie(0);
	int t;
	cin>>t;
	while(t--)
	    solve();
	return 0;
}


Comments

Submit
0 Comments
More Questions

Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal