Colorful Drink
InterviewTime limit2sMemory limit512 MB
Given colored liquids with densities and a top-to-bottom list of requested color layers, decide whether one liquid per layer has strictly decreasing densities.
- Level
Medium6 of 10
- Topics
- Greedy, Binary search, Array, Sorting
- Solved
- No attempts yet
Problem
The Jambo Amusement Garden (JAG) sells colorful drinks made of several layers of color. These colorful drinks are made by pouring colored liquids of different densities from the bottom up.
You have already prepared several colored liquids of various colors and densities. You now receive drink requests that specify the color layers. The colorful drink you serve must satisfy the following conditions.
- A mixed colored liquid cannot be used as a layer. For example, you cannot create a new liquid with a new color by mixing two or more colored liquids of different colors, nor create a liquid with a density between two liquids of the same color by mixing them.
- Only a colored liquid with strictly lower density can be placed above a denser colored liquid. That is, you can put a layer of a colored liquid with density directly above the layer of a colored liquid with density if holds.
Your task is to write a program that determines whether a given request can be fulfilled with the prepared colored liquids under the above conditions.
Input
The input consists of a single test case in the following format.
$N$
$C_1$ $D_1$
$\vdots$
$C_N$ $D_N$
$M$
$O_1$
$\vdots$
$O_M$
The first line contains an integer (), the number of prepared colored liquids. The next lines contain and (). is a string of lowercase alphabets and denotes the color of the -th prepared colored liquid. The length of is between 1 and 20 inclusive. is an integer and denotes the density of the -th prepared colored liquid. The value of is between 1 and inclusive. The -nd line contains an integer (), the number of color layers in the drink request. The next lines contain (). is a string of lowercase alphabets and denotes the color of the -th layer from the top of the drink request. The length of is between 1 and 20 inclusive.
Output
If the requested colorful drink can be served using some of the prepared colored liquids, print Yes. Otherwise, print No.