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
  1. 3
    gennady korotkevich
    petr mitrichev
    gaoyuan chen
    1 2 3
Output
  1. NO
Input
  1. 3
    gennady korotkevich
    petr mitrichev
    gaoyuan chen
    3 1 2
Output
  1. YES
Input
  1. 2
    galileo galilei
    nicolaus copernicus
    2 1
Output
  1. YES
Input
  1. 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
  1. NO
Input
  1. 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
  1. 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)扫一遍就可以了,其实还有比较两个字符串大小的复杂的

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef int ll;
  4. inline ll read()
  5. {
  6. ll s=;
  7. bool f=;
  8. char ch=' ';
  9. while(!isdigit(ch))
  10. {
  11. f|=(ch=='-'); ch=getchar();
  12. }
  13. while(isdigit(ch))
  14. {
  15. s=(s<<)+(s<<)+(ch^); ch=getchar();
  16. }
  17. return (f)?(-s):(s);
  18. }
  19. #define R(x) x=read()
  20. inline void write(ll x)
  21. {
  22. if(x<)
  23. {
  24. putchar('-'); x=-x;
  25. }
  26. if(x<)
  27. {
  28. putchar(x+''); return;
  29. }
  30. write(x/);
  31. putchar((x%)+'');
  32. return;
  33. }
  34. #define W(x) write(x),putchar(' ')
  35. #define Wl(x) write(x),putchar('\n')
  36. const int N=;
  37. int n,Pos[N];
  38. struct Record
  39. {
  40. string Xing,Ming;
  41. }Name[N];
  42. string Choose[N];
  43. int main()
  44. {
  45. int i,Pos;
  46. R(n);
  47. for(i=;i<=n;i++)
  48. {
  49. cin>>Name[i].Xing>>Name[i].Ming;
  50. }
  51. R(Pos);
  52. Choose[]=min(Name[Pos].Xing,Name[Pos].Ming);
  53. for(i=;i<=n;i++)
  54. {
  55. R(Pos);
  56. // cout<<max(Name[Pos].Xing,Name[Pos].Ming)<<' '<<Choose[i-1]<<endl;
  57. if(max(Name[Pos].Xing,Name[Pos].Ming)<Choose[i-]) return *puts("NO");
  58. if(min(Name[Pos].Xing,Name[Pos].Ming)<Choose[i-])
  59. {
  60. Choose[i]=max(Name[Pos].Xing,Name[Pos].Ming);
  61. }
  62. else Choose[i]=min(Name[Pos].Xing,Name[Pos].Ming);
  63. }
  64. puts("YES");
  65. return ;
  66. }
  67. /*
  68. input
  69. 3
  70. gennady korotkevich
  71. petr mitrichev
  72. gaoyuan chen
  73. 1 2 3
  74. output
  75. NO
  76.  
  77. input
  78. 3
  79. gennady korotkevich
  80. petr mitrichev
  81. gaoyuan chen
  82. 3 1 2
  83. output
  84. YES
  85.  
  86. input
  87. 10
  88. rean schwarzer
  89. fei claussell
  90. alisa reinford
  91. eliot craig
  92. laura arseid
  93. jusis albarea
  94. machias regnitz
  95. sara valestin
  96. emma millstein
  97. gaius worzel
  98. 1 2 3 4 5 6 7 8 9 10
  99. output
  100. NO
  101.  
  102. input
  103. 10
  104. rean schwarzer
  105. fei claussell
  106. alisa reinford
  107. eliot craig
  108. laura arseid
  109. jusis albarea
  110. machias regnitz
  111. sara valestin
  112. emma millstein
  113. gaius worzel
  114. 2 4 9 6 5 7 1 3 8 10
  115. output
  116. YES
  117. */

codeforces472C的更多相关文章

随机推荐

  1. Mybatis学习总结(七)——调用存储过程

    一.返回select结果集 1.创建存储过程 DELIMITER // DROP PROCEDURE IF EXISTS proc_queryUser; CREATE PROCEDURE proc_q ...

  2. python推导式创建序列

    推导式创建序列 推导式是一个或多个迭代器快速创建序列的一种方式.可以将循环和条件判断结合,简化代码.几个推导式注意符号的使用,比如小括号,方括号,大括号等等. 列表推导式 列表推导式生成列表对象,语法 ...

  3. .net ElasticSearch-Sql 扩展类【原创】

    官方提供的是java sdk,并支持jdbc方式的查询结果输出;但是却没有.net sdk的支持. 开发 ElasticSearch-Sql 第三方开源项目的.net sdk,未来集成入bsf框架.( ...

  4. 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 ...

  5. 有关Web常用字体的研究?

    Windows自带字体: 黑体:SimHei 宋体:SimSun 新宋体:NSimSun 仿宋:FangSong 楷体:KaiTi 仿宋GB2312:FangSongGB2312 楷体GB2312:K ...

  6. web安全:通俗易懂,以实例讲述破解网站的原理及如何进行防护!如何让网站变得更安全。

    本篇以我自己的网站为例来通俗易懂的讲述网站的常见漏洞,如何防止网站被入侵,如何让网站更安全. 要想足够安全,首先得知道其中的道理. 本文例子通俗易懂,主要讲述了 各种漏洞 的原理及防护,相比网上其它的 ...

  7. 从HelloWorld开始学习.NET Core

    1.首先创建一个项目文件夹,如E:\CoreProjects 使用cmd命令进入到新建的文件夹中 2.创建一个HelloWorld项目 命令:dotnet new console -o hellowo ...

  8. python之socket模块详解--小白博客

    主要是创建一个服务端,在创建服务端的时候,主要步骤如下:创建socket对象socket——>绑定IP地址和端口bind——>监听listen——>得到请求accept——>接 ...

  9. H5 37-背景缩写

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 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 ...