733C - Epidemic in Monstropolis - CodeForces Solution


constructive algorithms dp greedy two pointers *1800

Please click on ads to support us..

Python Code:

import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache
import heapq
from locale import resetlocale
from pickle import FALSE
import random
import math
from collections import Counter
import sys
 
mod = 10**9+7
def inp():
    return(int(sys.stdin.readline()))
def inlt():
    return(list(map(int,sys.stdin.readline().split())))
def insr():
    s = sys.stdin.readline()[:-1]
    return(s)
def invr():
    return(map(int,input().split()))
def gcd(a,b):
    if b == 0:
        return a
    return gcd(b, a % b)
class UF:
    def __init__(self, size):
        self.parent = list(range(size))
        self.rank = [0] * size
        
    def find(self, x):
        if self.parent[x] == x:
            return x
        return self.find(self.parent[x])
    
    def union(self, x, y):
        px = self.find(x)
        py = self.find(y)
        if px == py:
            return
        if self.rank[px] > self.rank[py]:
            self.parent[py] = px
        elif self.rank[px] < self.rank[py]:
            self.parent[px] = py
        else:
            self.parent[px] = py
            self.rank[py] += 1

class BIT:
    def __init__(self, n):
        self.sums = [0] * (n+1)
    
    def update(self, i, delta):
        while i < len(self.sums):
            self.sums[i] += delta
            i += i & (-i)
    
    def query(self, i):
        res = 0
        while i > 0:
            res += self.sums[i]
            i -= i & (-i)
        return res

class NumArray:
    def __init__(self, nums):
        self.bit = BIT(len(nums))
        for i, num in enumerate(nums):
            self.bit.update(i+1, num)
        self.nums = [0] + nums

    def update(self, i, val):
        self.bit.update(i+1, val - self.nums[i+1])
        self.nums[i+1] = val

    def sumRange(self, i, j):
        return self.bit.query(j+1) - self.bit.query(i)

 
for v in range(1):
    n = inp()
    a = inlt()
    m = inp()
    b = inlt()
    
    idx = 0
    ok = True
    left = 0
    ans = []
    for x in b:
        val = 0
        start = idx 
        while val < x and idx < n:
            val += a[idx]
            idx += 1
        if val != x or (idx - start > 1 and min(a[start:idx]) == max(a[start:idx])):
            ok = False 
            break 
                center = start
        if start == idx:
            ok = False 
            break
        largest = max(a[start:idx])

                lp = 0
        rp = 0
        for i in range(start, idx):
            if i != start:
                if a[i] == largest and a[i-1] != largest:
                    ans.append(str(i - left+1)+ " L")
                    left += 1
                    center = i 
                    lp = 1
                    break
            if i != idx - 1:
                if a[i] == largest and a[i+1] != largest:
                    ans.append(str(i-left+1)+ " R")
                    center = i 
                    rp = 1 
                    break
        for j in range(start, center - lp):
            ans.append(str(center-left+1) +  " L")
            left += 1
        for j in range(center + rp, idx-1):
            ans.append(str(center - left+1) + " R")
        left += idx - center - 1 
    if idx != n:
        ok = False
    if not ok:
        print("NO")
        break 
    print("YES")
    for s in ans:
        print(s)




Comments

Submit
0 Comments
More Questions

2099. Find Subsequence of Length K With the Largest Sum
1608A - Find Array
416. Partition Equal Subset Sum
1446. Consecutive Characters
1618A - Polycarp and Sums of Subsequences
1618B - Missing Bigram
938. Range Sum of BST
147. Insertion Sort List
310. Minimum Height Trees
2110. Number of Smooth Descent Periods of a Stock
2109. Adding Spaces to a String
2108. Find First Palindromic String in the Array
394. Decode String
902. Numbers At Most N Given Digit Set
221. Maximal Square
1200. Minimum Absolute Difference
1619B - Squares and Cubes
1619A - Square String
1629B - GCD Arrays
1629A - Download More RAM
1629C - Meximum Array
1629D - Peculiar Movie Preferences
1629E - Grid Xor
1629F1 - Game on Sum (Easy Version)
2148. Count Elements With Strictly Smaller and Greater Elements
2149. Rearrange Array Elements by Sign
2150. Find All Lonely Numbers in the Array
2151. Maximum Good People Based on Statements
2144. Minimum Cost of Buying Candies With Discount
Non empty subsets