Problem:

Problem B. Colorful Drink

  In the Jambo Amusement Garden (JAG), you sell colorful drinks consisting of multiple color layers. This colorful drink can be made by pouring multiple colored liquids of different density from the bottom in order.

  You have already prepared several colored liquids with various colors and densities. You will receive a drink request with specified color layers. The colorful drink that you will serve must satisfy the following conditions.

  
You cannot use a mixed colored liquid as a layer. Thus, for instance, you cannot create a new liquid with a new color by mixing two or more different colored liquids, nor create a liquid with a density between two or more liquids with the same color by mixing them.

  
Only a colored liquid with strictly less density can be an upper layer of a denser colored liquid in a drink. That is, you can put a layer of a colored liquid with density $x$ directly above the layer of a colored liquid with density $y$ if $x Yes'. Otherwise, print 'No'.

Examples

Sample Input 1

2
white 20
black 10
2
black
white

Output for Sample Input 1

Yes

Sample Input 2

2
white 10
black 10
2
black
white

Output for Sample Input 2

No

Sample Input 3

2
white 20
black 10
2
black
orange

Output for Sample Input 3

No

Sample Input 4

3
white 10
red 20
white 30
3
white
red
white

Output for Sample Input 4

Yes

Sample Input 5

4
red 3444
red 3018
red 3098
red 3319
4
red
red
red
red

Output for Sample Input 5

Yes



Sample input:
2
white 20
black 10
2
black
white


Sample output:
Yes

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    d = defaultdict(lambda : [])
    for i in range(n):
        a,b = input().split()
        b = int(b)
        d[a].append(b)
    for i in d.keys():
        d[i].sort()
    m = I()
    s = 0
    for i in range(m):
        o = input()
        if not d[o]:
            print("No")
            return
        j = bisect.bisect_right(d[o],s)
        if j == len(d[o]):
            print("No")
            return
        s = d[o][j]
    print("Yes")
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
from functools import reduce
import sys
import math
import bisect
import random
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return list(map(float, sys.stdin.readline().split()))
def LS():return sys.stdin.readline().split()
def SR(n): return [list(sys.stdin.readline())[:-1] for i in range(n)]
def IR(n): return [int(sys.stdin.readline()) for i in range(n)]
def LIR(n): return [list(map(int, sys.stdin.readline().split())) for i in range(n)]
def LSR(n): return [list(map(list, sys.stdin.readline().split())) for i in range(n)]
_gcd = lambda x, y: _gcd(y, x%y) if (x%y) else y
_lcm = lambda x, y: x*y // _gcd(x, y)
def gcd(*numbers):
    return reduce(_gcd, numbers)
def lcm(*numbers):
    return reduce(_lcm, numbers)
sys.setrecursionlimit(1000000)
INF = float('inf')
mod = 1000000007
dire4 = [(1,0), (0,1), (-1,0), (0,-1)]
dire8 = [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]

def solve(dic):
    return



N = I()
dic = defaultdict(list)
for i in range(N):
    C, D = LS()
    D = int(D)
    dic[C].append(D)


for key in dic.keys():
    dic[key].sort()

M = I()
order = []
for i in range(M):
    #tmp = input()
    tmp = LS()[0]
    order.append(tmp)

flag = True
prev = mod

if N >= M :
    for layer in order[::-1] :
        while True:
            if len(dic[layer]) == 0 :
                flag = False
                break
            now = dic[layer].pop(-1)

            if now >= prev :
                continue
            else :
                prev = now
                break
else :
    flag = False


print("Yes" if flag else "No")

Submission status: Accepted

Solution code:
import sys
liquids={}
O=[]

N = int(input())
for i in range(N):
    C,D=(input().split())
    if C in liquids.keys():
        liquids[C].append(int(D))
    else:
        liquids[C]=[]
        liquids[C].append(int(D))
for i in liquids.keys():
    liquids[i]=list(set(liquids[i]))
    liquids[i].sort()

M = int(input())
if M > N:
    print("No")
    sys.exit()
for i in range(M):
    O.append(input())
    
nowdens=int(10**5+1)

for i in range(M):
    try:
        tmp=liquids[O[-(i+1)]]
        if len(tmp)==0:
            print("No")
            sys.exit()
        maxdens=tmp.pop()
        while(maxdens>=nowdens):
            if len(tmp)==0:
                print("No")
                sys.exit()
            maxdens=tmp.pop()
        nowdens=maxdens
    except KeyError:
        print("No")
        sys.exit()
    
print("Yes")