1658A - Marin and Photoshoot - CodeForces Solution


constructive algorithms math

Please click on ads to support us..

Python Code:

 
import atexit, io, sys, math, heapq, collections

def get_int(): return int(sys.stdin.readline().strip())
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def get_list(): return list(map(int, sys.stdin.readline().strip().split()))
def get_string(): return sys.stdin.readline().strip()

def write_int(a): sys.stdout.write(str(a)+"\n")
def write_str(a): sys.stdout.write(a+"\n")
def write_list(arr): sys.stdout.write(" ".join(map(str, arr)) + "\n")

def swap(arr, a, b):
    arr[a]^=arr[b]
    arr[b]^=arr[a]
    arr[a]^=arr[b]

def int_len(a): return math.floor(math.log(a,10))+1

def isPowerOfTwo(x): return x and (not(x & (x-1)))

def Sieve(n):
    prime = [True for i in range(n+1)]
    p = 2
    while (p * p <= n):
        if (prime[p] == True):
            for i in range(p * p, n+1, p):
                prime[i] = False
        p += 1
    res = []
    for i in range(2,len(prime)):
        if (prime[i]):
            res.append(i)
    return res

def binarySearch (arr, l, r, x):
    if r >= l:
  
        mid = l + (r - l) // 2
        if arr[mid] == x:
            return mid
        elif arr[mid] > x:
            return binarySearch(arr, l, mid-1, x)
        else:
            return binarySearch(arr, mid + 1, r, x)
    else:
        return -1
def search(arr,x): return binarySearch(arr,0,len(arr)-1,x)

def toString(A): return''.join(A)

def toArray(str): return str.split()

def arrSum(Arr):
    Sum =0
    for i in range (len(Arr)):
        Sum+=Arr[i]
    return Sum

def computeXOR(n) :
 
             
        if n % 4 == 0 :
        return n
 
        if n % 4 == 1 :
        return 1
 
        if n % 4 == 2 :
        return n + 1
 
        return 0
 

def find_gcd(x, y):
     
    while(y):
        x, y = y, x % y
     
    return x
         
def isSubSequence(string1, string2, m, n):
        if m == 0:
        return True
    if n == 0:
        return False
 
            if string1[m-1] == string2[n-1]:
        return isSubSequence(string1, string2, m-1, n-1)
 
        return isSubSequence(string1, string2, m, n-1)

def sum(a,b):
    if (b.find('-')!=-1):
        return "-1"
    L1= len(a)
    L2= len(b)
    res =""
    if (L1<L2):
        a='0'*(L2-L1)+a
    elif (L2<L1):
        b='0'*(L1-L2)+b
    for i in range(0,max(L1,L2)):
        s=a[i]
        t=b[i]
        p=int(a[i])+int(b[i])
        res+=str(p)
    return res

def isPalin(s):
    return s == s[::-1]

    
def nextPermutation( nums):
    i=len(nums)-1
    flag=True
    while (i>=1):
        if nums[i]>nums[i-1]:
            i-=1
            flag=False
            break
        i-=1

    if i==0 and flag:
        nums.reverse()
    else:
        diff=10000000
        index=i
        j=i
        while(j<len(nums)):
            if (nums[j]>nums[i] and diff>=nums[j]-nums[i]):
                index=j
                diff=nums[j]-nums[i]
            j+=1
        temp=nums[index]
        nums[index]=nums[i]
        nums[i]=temp
        nums[i+1:]=reversed(nums[i+1:])   
    return nums

def pairs(arr, size, n):
 
    mpp = {}
 
    for i in range(size):
        if arr[i] in mpp.keys():
             mpp[arr[i]] += 1
        else:
             mpp[arr[i]] = 1
 
    for i in range(size):
         if n + arr[i] in mpp.keys():
                        return True
     
        return False


    
N= get_int()
while N>0:
    n = get_int()
    S= get_string()
    res = 0
    i=0
    while i<len(S):
        if S[i]=='0':
            j=i+1
            while j<len(S):
                if S[j]=='0':
                    if j-i==1:
                        res+=2
                    elif j-i==2:
                        res+=1
                    break
                j+=1
            i=j
            continue
        i+=1
    print(res)

    N-=1






    




C++ Code:

#include <bits/stdc++.h>

using namespace std;

void solve(){
    int n;cin>>n;
    string st;cin>>st;
    int count=0;
    vector<int> vt;
    for(int i=0;i<n;i++){
        if(st[i]=='0') vt.push_back(i); 
    }
    for(int i=0;i<(int)vt.size()-1;i++){
        if((vt[i+1]-vt[i])==1) count=count+2; 
        if((vt[i+1]-vt[i])==2) count++;
    }
    cout<<count<<"\n";
}

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


Comments

Submit
0 Comments
More Questions

771. Jewels and Stones
1512. Number of Good Pairs
672. Richest Customer Wealth
1470. Shuffle the Array
1431. Kids With the Greatest Number of Candies
1480. Running Sum of 1d Array
682. Baseball Game
496. Next Greater Element I
232. Implement Queue using Stacks
844. Backspace String Compare
20. Valid Parentheses
746. Min Cost Climbing Stairs
392. Is Subsequence
70. Climbing Stairs
53. Maximum Subarray
1527A. And Then There Were K
1689. Partitioning Into Minimum Number Of Deci-Binary Numbers
318. Maximum Product of Word Lengths
448. Find All Numbers Disappeared in an Array
1155. Number of Dice Rolls With Target Sum
415. Add Strings
22. Generate Parentheses
13. Roman to Integer
2. Add Two Numbers
515. Find Largest Value in Each Tree Row
345. Reverse Vowels of a String
628. Maximum Product of Three Numbers
1526A - Mean Inequality
1526B - I Hate 1111
1881. Maximum Value after Insertion