CF - 472C. Design Tutorial: Make It Nondeterministic 贪心
1.题目描述:
2 seconds
256 megabytes
standard input
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: .
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.题意概述:
有n个人,每个人可以选择两种签名,问是否给定的顺序能否构成一个字典序排序的签名
3.解题思路:
照指定的顺序,每次贪心地选择符合条件的字典序的最小的字符串,若当前的字符串比下一个人的两个签名的字典序都大,则无法满足
4.AC代码:
#include <iostream>
#include <algorithm>
#include <string>
#define maxn 101000
using namespace std;
struct node
{int id;string fst, snd;
}p[maxn];int main()
{int n;scanf("%d", &n);for (int i = 1; i <= n; i++)cin >> p[i].fst >> p[i].snd;int temp, flag = 1;cin >> temp;string last = min(p[temp].fst, p[temp].snd);for (int i = 2; i <= n; i++){if (!flag)break;cin >> temp;string cur = min(p[temp].fst, p[temp].snd);if (last >= cur){cur = max(p[temp].fst, p[temp].snd);if (last >= cur)flag = 0;}last = cur;}if (flag)puts("YES");elseputs("NO");return 0;
}
CF - 472C. Design Tutorial: Make It Nondeterministic 贪心相关推荐
- codeforces C. Design Tutorial: Make It Nondeterministic
题意:每一个人 都有frist name 和 last name! 从每一个人的名字中任意选择 first name 或者 last name 作为这个人的编号!通过对编号的排序,得到每一个人 最终顺 ...
- cf:D. The Enchanted Forest【贪心 + 模拟 + 构造】
分析 找规律然后构造才是王道(分类讨论) 如果k小于等于n,说明不会来回走,所以不会影响增量,只需要找到最大的连续k个即可 可用accumulate操作,注意如果initial = 0的话,最后是会有 ...
- codeforce A. Design Tutorial: Learn from Math
题意:将一个数拆成两个合数的和, 输出这两个数!(这道题做的真是TMD水啊)开始的时候不知道composite numbers是啥意思,看了3遍才看懂.... 看懂之后又想用素数筛选法来做,后来决定单 ...
- 【CodeForces - 472A】Design Tutorial: Learn from Math (tricks,思维,数论,打表)
题干: One way to create a task is to learn from math. You can generate some random math statement or m ...
- codeforces D. Design Tutorial: Inverse the Problem
题意:给定一个矩阵,表示每两个节点之间的权值距离,问是否可以对应生成一棵树, 使得这棵树中的任意两点之间的距离和矩阵中的对应两点的距离相等! 思路:我们将给定的矩阵看成是一个图,a 到 b会有多条路径 ...
- codeforces B. Design Tutorial: Learn from Life
题意:有一个电梯,每一个人都想乘电梯到达自己想要到达的楼层! 从a层到b层的时间是|a-b|, 乘客上下电梯的时间忽略不计!问最少 需要多少的时间.... 这是一道神题啊,自己的思路不知不觉 ...
- cf 、B. Fridge Lockers(思维 + 贪心)
题意:有n个冰箱,然后m个锁链,每条锁链两头都连着冰箱,然后让你每个冰箱都至少连着两条锁链指向不同冰箱.买锁链的钱是两个冰箱重量的和,问最少花费多少钱可以满足,如果满足输出最小花费,否则,输出-1. ...
- CF - E95(div2) -- B. Negative Prefixes【贪心】
题意 给定一个序列,然后给定那些位置被锁定不能被移动,将序列可移动的数随机移动,移动后为a1, a2, a3-an; 设p1 = a1 ,p2 = a1 + a2 ,p为a的前缀和,k为前缀和中pi ...
- 【综合复健训练2021/1/18:题解】Comprehensive Training (CF 1000 ~ 1600)
Comprehensive Training (CF 1000 ~ 1600) 前言 A:贪心 1600 B:排序+贪心 1400 C:二分+贪心 1800 D:博弈 1200 F:DFS搜索 160 ...
- CF #717 题解
CF #717 题解 A - Tit or Tat 贪心地从前面拿数,加到最后一位即可. B - AGAGA XOOORRR 转化一下,是求是否能把序列分成若干段( ≥ 2 \ge 2 ≥2),每段的 ...
最新文章
- 数学之美——隐含马尔科夫模型
- a++浏览器_走进浏览器内部—剖析浏览器是如何工作的(上)
- CDH6.3.2界面配置hbase-site.xml的方法
- 9012年大厂面试题合集:Java技术栈为什么竞争越来越激烈?
- ArcGIS学习记录—KMZ KML与SHP文件互相转换
- OAF_VO系列1 - Accelerator Keys
- sql时间格式转换yyyymm_XML和实体类之间的转换
- N元语法模型的数据稀疏问题解决方法之一:Good-Turing平滑
- LNMP一键安装包 PHP自动升级脚本
- RecyclerView(六)设置下拉刷新
- typedef struct和struct定义结构体的区别
- JSP — 如何设置jsp中cookie的过期时间
- html动态绑定树形菜单,jQuery ztree实现动态树形多选菜单
- “模板”学习笔记(3)-----为啥函数模板不能重载
- php mysql数据库同步_教你怎样在两台MySQL数据库间实现同步
- 微信小程序提供 模板:template
- 色差仪确保番茄酱色彩一致性
- 数梦工场:我们帮你实现你驾驭数据的梦想
- 20145212 罗天晨 信息搜集与漏洞扫描
- javascript: webcam