1.题目描述:
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 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: .
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).
If it is possible, output "YES", otherwise output "NO".
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
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.
2.题意概述:
每个人可以选择first name 或者 last name 当自己的账号,现在给出最终的字典序排位,问能不能实现
3.解题思路:
要尽可能的最终字典序排,那么对于每个人,肯定是尽可能地选择字典序小的name,开一个string记录当前i - 1个名字,那么每次选择就贪心地选择字典序小的名字,如果字典序小的不满足再选择字典序大的名字,如果都不满足则不能实现
4.AC代码:
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 100100
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e 2.718281828459
#define mod 32767
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
struct node
{
int id;
string first, last;
} p[maxn];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
int n;
while (~scanf("%d", &n))
{
for (int i = 1; i <= n; i++)
cin >> p[i].first >> p[i].last;
int tmp, flag = 1;
scanf("%d", &tmp);
string cur = min(p[tmp].first, p[tmp].last);
for (int i = 0; i < n - 1; i++)
{
scanf("%d", &tmp);
if (flag)
{
string temp = min(p[tmp].first, p[tmp].last);
if (cur >= temp)
{
if (cur >= max(p[tmp].first, p[tmp].last))
flag = 0;
else
cur = max(p[tmp].first, p[tmp].last);
}
else
cur = temp;
}
}
puts(flag ? "YES" : "NO");
}
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return 0;
}