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=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n,Pos[N];
struct Record
{
string Xing,Ming;
}Name[N];
string Choose[N];
int main()
{
int i,Pos;
R(n);
for(i=;i<=n;i++)
{
cin>>Name[i].Xing>>Name[i].Ming;
}
R(Pos);
Choose[]=min(Name[Pos].Xing,Name[Pos].Ming);
for(i=;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-]) return *puts("NO");
if(min(Name[Pos].Xing,Name[Pos].Ming)<Choose[i-])
{
Choose[i]=max(Name[Pos].Xing,Name[Pos].Ming);
}
else Choose[i]=min(Name[Pos].Xing,Name[Pos].Ming);
}
puts("YES");
return ;
}
/*
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
*/

codeforces472C的更多相关文章

随机推荐

  1. djongo:Django和MongoDB连接器

    在Django项目中使用MongoDB作为后端数据库,且不改变Django的ORM框架.实现Django用户管理程序对MongoDB数据库中文件的增加和修改. 用法 1.pip install djo ...

  2. iOS开发之CoreImage

    CoreImage是iOS中的一个图像处理框架,提供了强大高效的图像处理功能,可以通过调用简单的API来使用框架所带的各种滤镜对图像进行处理. CoreImgae的三个重要组成部分:1.CIFiter ...

  3. mysql中添加索引

    创建普通索引 alter table xxxxxx add index index_name(xxxxxxx) 表名↑                                        列 ...

  4. pip install报错Can't roll back cryptography; was not uninstalled

    当使用pip install或者pip install --upgrade报错 Can't roll back cryptography; was not uninstalled 可以进行以下的尝试: ...

  5. FIFO队列算法的C程序实现

    头文件:Queue.h #ifndef _Queue_H #define _Queue_H typedef struct QueueDef_ //队列对象定义 { u16 front; //队列头部 ...

  6. DAG也许是真正的区块链3.0

    从15年开始,区块链概念被单拎出来,这之前区块链还只是比特币技术里的一个数据结构,中本村白皮书里把block和chain连一起的时候也只是a chain of blocks .随着以太坊去中心化计算机 ...

  7. Java多线程核心技术(四)Lock的使用

    本文主要介绍使用Java5中Lock对象也能实现同步的效果,而且在使用上更加方便. 本文着重掌握如下2个知识点: ReentrantLock 类的使用. ReentrantReadWriteLock ...

  8. tensorflow-gpu安装的一些注意

    按正确的顺序安装,严格安装特定的版本 1,下载和安装严格版本的cuda和cuDnn,其他版本的不干活.比如要求9.0你就不能装9.1.https://www.tensorflow.org/instal ...

  9. .net Core 调用微信Jsapi接口,H5解析二维码

    项目里需要用到扫描二维码,自己实现,不会. 找到了两种解决方案: 通过reqrcode.js,这是一个前端解析二维码内容的js库.如果二维码比较清晰,用这种效果也不错 调用微信扫一扫功能,这种效果很好 ...

  10. UVA 10791 -唯一分解定理的应用

    #include<iostream> #include<stdio.h> #include<algorithm> #include<string.h> ...