codeforces472C

Design Tutorial: Make It Nondeterministic

 CodeForces - 472C 

A way to make a new task is to make it nondeterministic or probabilistic. For example, the hard task of Topcoder SRM 595, Constellation, is the probabilistic version of a convex hull.

Let's try to make a new task. Firstly we will use the following task. There are npeople, sort them by their name. It is just an ordinary sorting problem, but we can make it more interesting by adding nondeterministic element. There are n people, each person will use either his/her first name or last name as a handle. Can the lexicographical order of the handles be exactly equal to the given permutation p?

More formally, if we denote the handle of the i-th person as hi, then the following condition must hold: .

Input

The first line contains an integer n (1 ≤ n ≤ 105) — the number of people.

The next n lines each contains two strings. The i-th line contains strings fi and si(1 ≤ |fi|, |si| ≤ 50) — the first name and last name of the i-th person. Each string consists only of lowercase English letters. All of the given 2n strings will be distinct.

The next line contains n distinct integers: p1, p2, ..., pn (1 ≤ pi ≤ n).

Output

If it is possible, output "YES", otherwise output "NO".

Examples

Input
3
gennady korotkevich
petr mitrichev
gaoyuan chen
1 2 3
Output
NO
Input
3
gennady korotkevich
petr mitrichev
gaoyuan chen
3 1 2
Output
YES
Input
2
galileo galilei
nicolaus copernicus
2 1
Output
YES
Input
10
rean schwarzer
fei claussell
alisa reinford
eliot craig
laura arseid
jusis albarea
machias regnitz
sara valestin
emma millstein
gaius worzel
1 2 3 4 5 6 7 8 9 10
Output
NO
Input
10
rean schwarzer
fei claussell
alisa reinford
eliot craig
laura arseid
jusis albarea
machias regnitz
sara valestin
emma millstein
gaius worzel
2 4 9 6 5 7 1 3 8 10
Output
YES

Note

In example 1 and 2, we have 3 people: tourist, Petr and me (cgy4ever). You can see that whatever handle is chosen, I must be the first, then tourist and Petr must be the last.

In example 3, if Copernicus uses "copernicus" as his handle, everything will be alright.

sol:每个字符串都要在小于前一个的条件下尽可能的小,按照这个规律一直O(n)扫一遍就可以了,其实还有比较两个字符串大小的复杂的

#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
    ll s=0;
    bool f=0;
    char ch=' ';
    while(!isdigit(ch))
    {
        f|=(ch=='-'); ch=getchar();
    }
    while(isdigit(ch))
    {
        s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
    }
    return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
    if(x<0)
    {
        putchar('-'); x=-x;
    }
    if(x<10)
    {
        putchar(x+'0');    return;
    }
    write(x/10);
    putchar((x%10)+'0');
    return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('
')
const int N=100005;
int n,Pos[N];
struct Record
{
    string Xing,Ming;
}Name[N];
string Choose[N];
int main()
{
    int i,Pos;
    R(n);
    for(i=1;i<=n;i++)
    {
        cin>>Name[i].Xing>>Name[i].Ming;
    }
    R(Pos);
    Choose[1]=min(Name[Pos].Xing,Name[Pos].Ming);
    for(i=2;i<=n;i++)
    {
        R(Pos);
//        cout<<max(Name[Pos].Xing,Name[Pos].Ming)<<' '<<Choose[i-1]<<endl;
        if(max(Name[Pos].Xing,Name[Pos].Ming)<Choose[i-1]) return 0*puts("NO");
        if(min(Name[Pos].Xing,Name[Pos].Ming)<Choose[i-1])
        {
            Choose[i]=max(Name[Pos].Xing,Name[Pos].Ming);
        }
        else Choose[i]=min(Name[Pos].Xing,Name[Pos].Ming);
    }
    puts("YES");
    return 0;
}
/*
input
3
gennady korotkevich
petr mitrichev
gaoyuan chen
1 2 3
output
NO

input
3
gennady korotkevich
petr mitrichev
gaoyuan chen
3 1 2
output
YES

input
10
rean schwarzer
fei claussell
alisa reinford
eliot craig
laura arseid
jusis albarea
machias regnitz
sara valestin
emma millstein
gaius worzel
1 2 3 4 5 6 7 8 9 10
output
NO

input
10
rean schwarzer
fei claussell
alisa reinford
eliot craig
laura arseid
jusis albarea
machias regnitz
sara valestin
emma millstein
gaius worzel
2 4 9 6 5 7 1 3 8 10
output
YES
*/
View Code
原文地址:https://www.cnblogs.com/gaojunonly1/p/10651272.html