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. .net core 简单项目的创建

    1.linux 安装net coref https://www.microsoft.com/net/learn/get-started/linuxubuntu 2.创建目录 2.创建控制台项目 第一次 ...

  2. lwip TCP client & FreeRTOS 打开TCP 的 保活机制 LWIP_TCP_KEEPALIVE==1

    参考大神教程:http://blog.sina.com.cn/s/blog_62a85b950101aw8x.html   老衲五木 :http://blog.sina.com.cn/s/blog_6 ...

  3. RabbitMQ详解(三)------RabbitMQ的五种队列

    上一篇博客我们介绍了RabbitMQ消息通信中的一些基本概念,这篇博客我们介绍 RabbitMQ 的五种工作模式,这也是实际使用RabbitMQ需要重点关注的. 这里是RabbitMQ 官网中的相关介 ...

  4. 【commons】时间日期工具类——commons-lang3-time

    推荐参考:http://www.cnblogs.com/java-class/p/4845962.html https://blog.csdn.net/yihaoawang/article/detai ...

  5. FineUIMvc随笔(3)不能忘却的回发(__doPostBack)

    声明:FineUIMvc(基础版)是免费软件,本系列文章适用于基础版. 用户反馈 有网友在官方论坛抛出了这么一个问题,似乎对 FineUIMvc 中的浏览器端与服务器端的交互方式很有异议. 这里面的关 ...

  6. 【Java并发.5】基础构建模块

    本章会介绍一些最有用的并发构建模块,有丶东西(最后一小节,纯干货). 5.1 同步容器类 同步容器类包括 Vector 和 Hashtable ,这些类实现线程安全的方式是:将它们的状态封装起来,并对 ...

  7. 最新版XCoder 的使用方法

    1.项目中,新建一个类库.名字随意,我取名:XCoder 2.右键 > 管理nuget程序包:搜索 XCode 并安装 3.在项目中新建:data.project.xml 的xml文件,并写入数 ...

  8. MiniProfiler安装使用心得

    MiniProfiler简介: MVC MiniProfiler是Stack Overflow团队设计的一款对ASP.NET MVC的性能分析的小程序.可以对一个页面本身,及该页面通过直接引用.Aja ...

  9. LeetCode 961. N-Repeated Element in Size 2N Array

    In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeate ...

  10. Linux 下RPM打包制作流程

    原文地址:https://www.cnblogs.com/postgres/p/5726339.html 开始前的准备 安装rpmbuild软件包 yum -y install rpm-build 生 ...