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

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



我是贪心做的,按题目所给的顺序比较每一行的,取大的,如果顺利比完,就输出YES,否则输出NO


#include<map>  
#include<set>  
#include<list>  
#include<stack>  
#include<queue>  
#include<vector>  
#include<cmath>  
#include<cstdio>  
#include<cstring>  
#include<iostream>  
#include<algorithm>  
  
using namespace std; 

struct peo
{
	char f[55];
	char s[55];
}arr[100010];

char cur[55];

int p[100010];

int main()
{
	int n;
	while(~scanf("%d", &n))
	{
		for(int i = 1; i <= n; i++)
		{
			scanf("%s%s", arr[i].f, arr[i].s);
		}
		for(int i = 1; i <= n; i++)
		{
			scanf("%d", &p[i]);
		}
		bool flag = true;
		if(strcmp(arr[p[n]].f, arr[p[n]].s) > 0)
			strcpy(cur, arr[p[n]].f);
		else
			strcpy(cur, arr[p[n]].s);
		for(int i = n - 1; i >= 1; i--)
		{
		 	if(strcmp(cur, arr[p[i]].f) > 0 || strcmp(cur, arr[p[i]].s) > 0)
		 	{
		 		if(strcmp(cur, arr[p[i]].f) > 0 && strcmp(cur, arr[p[i]].s) > 0)
		 		{
		 			if(strcmp(arr[p[i]].f, arr[p[i]].s) > 0)
		 				strcpy(cur, arr[p[i]].f);
	 				else
	 					strcpy(cur, arr[p[i]].s);
		 		}
		 		else if(strcmp(cur, arr[p[i]].f) > 0 && strcmp(cur, arr[p[i]].s) <= 0)
		 			strcpy(cur, arr[p[i]].f);
	 			else
	 				strcpy(cur, arr[p[i]].s);
	 		}
	 		
	 		else
	 		{
		 		flag = false;
		 		break;
		 	}
		}
		if(flag)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看rEADME.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看rEADME.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值