Codeforces Round #270 1003
C. Design Tutorial: Make It Nondeterministic
time limit per test
2 secondsmemory limit per test
256 megabytesinput
standard inputoutput
standard outputA 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 n people, 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".
Sample test(s)
Input
3gennady korotkevich
petr mitrichev
gaoyuan chen
1 2 3
Output
NOInput
3gennady korotkevich
petr mitrichev
gaoyuan chen
3 1 2
Output
YES1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 using namespace std; 7 8 int n; 9 pair<string,int> mp[200060]; 10 11 int main() 12 { 13 scanf("%d",&n); 14 for(int i = 1;i<=(n<<1);i++) 15 { 16 cin>>mp[i].first; 17 mp[i].second = (i+1)/2; 18 } 19 sort(mp+1,mp+2*n+1); 20 int rk; 21 int j = 1; 22 int ct = 0; 23 for(int i = 1;i<=n;i++) 24 { 25 scanf("%d",&rk); 26 for(;j<=2*n;j++) 27 { 28 if(mp[j].second==rk) 29 { 30 ct++; 31 break; 32 } 33 } 34 } 35 if(ct==n) puts("YES"); 36 else puts("NO"); 37 return 0; 38 }