codeforces472C
Design Tutorial: Make It Nondeterministic
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
3
gennady korotkevich
petr mitrichev
gaoyuan chen
1 2 3
NO
3
gennady korotkevich
petr mitrichev
gaoyuan chen
3 1 2
YES
2
galileo galilei
nicolaus copernicus
2 1
YES
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
NO
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
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的更多相关文章
随机推荐
- BZOJ1061 NOI2008 志愿者招募 线性规划、费用流
传送门 一道思路很妙的线性规划网络流 设\(X_i\)表示第\(i\)天需要的人数,\(P_i\)表示第\(i\)种人雇佣的个数 那么我们可以列出一系列式子 比如说样例就可以列出三个式子: \(P_1 ...
- mybatis百科-结果集映射类ResultMap
目录 1 成员变量 2 构造函数 3 其他函数 3.1 setter 和 getter 函数 4 静态内部类 4.1 成员变量 4.2 构造函数 4.3 建造者相关的函数 4.4 获取配置的构造方法参 ...
- jdk1.8之线程中断
在Core Java中有这样一句话:"没有任何语言方面的需求要求一个被中断的程序应该终止.中断一个线程只是为了引起该线程的注意,被中断线程可以决定如何应对中断 " 线程中断不会使线 ...
- java中scanner的正确用法
Scanner s = new Scanner(System.in); int choice = 0; if(s.hasNextInt()) { choice = s.nextInt(); } s.c ...
- Continued Fractions CodeForces - 305B (java+高精 / 数学)
A continued fraction of height n is a fraction of form . You are given two rational numbers, one is ...
- PAT 7-14 公路村村通
https://pintia.cn/problem-sets/1111189748004499456/problems/1111189831248850957 现有村落间道路的统计数据表中,列出了有可 ...
- Centos 6.x 升级到 7.x
Centos6.5跨越大版本升级到Centos7.4 - Linux学习与应用 - CSDN博客https://blog.csdn.net/whbttst/article/details/805348 ...
- IdentityServer4【Introduction】之包和项目构建
包和项目构建 IdentityServer包含了以下的nuget包: IdentityServer4 nuget | github 这个包包含了IdentityServer核心的组成部分,有对象模型, ...
- [转帖] SS, SP, BP 三个寄存器
SS, SP, BP 三个寄存器 https://blog.csdn.net/vspiders/article/details/55669265 这么看 计算机组成原理 还有 考试的很多题目非常有用啊 ...
- cpp11_thread线程
一.进程与线程 cpu一般有m核n线程的说法,那么该cpu只能同时运行n个线程(线程中没有sleep). #include <thread> #include <mutex> ...