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的更多相关文章
随机推荐
- Mybatis学习总结(七)——调用存储过程
一.返回select结果集 1.创建存储过程 DELIMITER // DROP PROCEDURE IF EXISTS proc_queryUser; CREATE PROCEDURE proc_q ...
- python推导式创建序列
推导式创建序列 推导式是一个或多个迭代器快速创建序列的一种方式.可以将循环和条件判断结合,简化代码.几个推导式注意符号的使用,比如小括号,方括号,大括号等等. 列表推导式 列表推导式生成列表对象,语法 ...
- .net ElasticSearch-Sql 扩展类【原创】
官方提供的是java sdk,并支持jdbc方式的查询结果输出;但是却没有.net sdk的支持. 开发 ElasticSearch-Sql 第三方开源项目的.net sdk,未来集成入bsf框架.( ...
- ASP.NET Core中代码使用X509证书,部署到IIS上后报错:System cannot find the specified file 的解决办法(转载)
问: I am trying to embrace the mysteries of SSL communication and have found a great tutorial on this ...
- 有关Web常用字体的研究?
Windows自带字体: 黑体:SimHei 宋体:SimSun 新宋体:NSimSun 仿宋:FangSong 楷体:KaiTi 仿宋GB2312:FangSongGB2312 楷体GB2312:K ...
- web安全:通俗易懂,以实例讲述破解网站的原理及如何进行防护!如何让网站变得更安全。
本篇以我自己的网站为例来通俗易懂的讲述网站的常见漏洞,如何防止网站被入侵,如何让网站更安全. 要想足够安全,首先得知道其中的道理. 本文例子通俗易懂,主要讲述了 各种漏洞 的原理及防护,相比网上其它的 ...
- 从HelloWorld开始学习.NET Core
1.首先创建一个项目文件夹,如E:\CoreProjects 使用cmd命令进入到新建的文件夹中 2.创建一个HelloWorld项目 命令:dotnet new console -o hellowo ...
- python之socket模块详解--小白博客
主要是创建一个服务端,在创建服务端的时候,主要步骤如下:创建socket对象socket——>绑定IP地址和端口bind——>监听listen——>得到请求accept——>接 ...
- H5 37-背景缩写
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Consecutive Subsequence CodeForces - 977F (map优化DP)·
You are given an integer array of length nn. You have to choose some subsequence of this array of ma ...