【CODEFORCES】 C. Design Tutorial: Make It Nondeterministic

原创 2015年07月09日 11:34:42

C. Design Tutorial: Make It Nondeterministic
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 permutationp?

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
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
2
galileo galilei
nicolaus copernicus
2 1
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
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.



这题就是一道简单的贪心,按照P[I]的顺序处理,记录前一个取得是FIRST NAME 还是LAST NAME ,优先取字典序小的那个,排序的时候注意下如果两个串前面全部相等的话,长的字典序大。

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

char f[100005][55],l[100005][55];
int n,p[100005],q[100005];

int com(char a[],char b[])
{
    int l1=strlen(a),l2=strlen(b);
    for (int i=0;i<min(l1,l2);i++)
        if (a[i]>b[i])
    {
        return 1;
        break;
    }
    else if (a[i]<b[i])
    {
        return 0;
        break;
    }
    if (l1>l2) return 1;
    else return 0;
}

int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++) scanf("%s%s",&f[i],&l[i]);
    for (int i=1;i<=n;i++) scanf("%d",&p[i]);
    for (int i=1;i<=n;i++) q[p[i]]=i;
    int k,kk,last=0,j;
    for (int i=1;i<=n;i++)
    {
      k=p[i]; kk=p[i-1];
      if (i==1)
      {
          j=com(f[k],l[k]);
          if (j) last=1;

          else last=0;
      }
      else if (last)
      {
          j=com(f[k],l[k]);
          if (j && com(l[kk],l[k])==0) last=1;
          else if (j && com(l[kk],f[k])==0) last=0;
          else if (!j && com(l[kk],f[k])==0) last=0;
          else if (!j && com(l[kk],l[k])==0) last=1;
          else
          {
              cout <<"NO"<<endl;
              return 0;
          }
      }
      else
      {
          j=com(f[k],l[k]);
          if (j && com(f[kk],l[k])==0) last=1;
          else if (j && com(f[kk],f[k])==0) last=0;
          else if (!j && com(f[kk],f[k])==0) last=0;
          else if (!j && com(f[kk],l[k])==0) last=1;
          else
          {
              cout <<"NO"<<endl;
              return 0;
          }
      }
    }
    cout <<"YES"<<endl;
    return 0;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Codeforces 472C - Design Tutorial: Make It Nondeterministic(模拟)

题意 每个人可以选择first name 或者 last name 当自己的账号,现在给出最终的字典序排位,问能不能实现 思路 模拟一下。 lst用于记录上一个人选择的...

【codeforces】 472C Design Tutorial: Make It Nondeterministic

简单贪心。。。。

Codeforces Round #270——C. Design Tutorial: Make It Nondeterministic

C. Design Tutorial: Make It Nondeterministic time limit per test 2 seconds memory limit per t...

Codeforces 472C Design Tutorial: Make It Nondeterministic【贪心】

C. Design Tutorial: Make It Nondeterministic time limit per test 2 seconds memory limit per t...

CF - 472C. Design Tutorial: Make It Nondeterministic 贪心

1.题目描述: C. Design Tutorial: Make It Nondeterministic time limit per test 2 seconds m...

【codeforces】 472D Design Tutorial: Inverse the Problem

Let's think it in the following way: for the minimal length edge, it must belong the the tree, ..., ...

Codeforces 472D Design Tutorial: Inverse the Problem【MST+SPFA+思维】

D. Design Tutorial: Inverse the Problem time limit per test 2 seconds memory limit per test ...

codeforces472D Design Tutorial: Inverse the Problem 最小生成树+dfs

题意:

Codeforces Round #270 G. Design Tutorial: Increase the Constraints 分块+FFT

我们如果要求两个串的哈密顿距离我们可以这样把第二个串反向,然后字母为1的地方系数为1,字母为0的系数为-1,所以我们可以算出x-y=a[n-1](x是相同的点数,y是不同的点数)有因为x+y=n所以y...

【CODEFORCES】 B. Design Tutorial: Learn from Life

B. Design Tutorial: Learn from Life time limit per test 1 second memory limit per test 2...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)