CF140B.New Year Cards

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

As meticulous Gerald sets the table, Alexander finished another post on Codeforces and begins to respond to New Year greetings from friends. Alexander has n friends, and each of them sends to Alexander exactly one e-card. Let us number his friends by numbers from 1 to n in the order in which they send the cards. Let's introduce the same numbering for the cards, that is, according to the numbering the i-th friend sent to Alexander a card number i.

Alexander also sends cards to friends, but he doesn't look for the new cards on the Net. He simply uses the cards previously sent to him (sometimes, however, he does need to add some crucial details). Initially Alexander doesn't have any cards. Alexander always follows the two rules:

  1. He will never send to a firend a card that this friend has sent to him.
  2. Among the other cards available to him at the moment, Alexander always chooses one that Alexander himself likes most.

Alexander plans to send to each friend exactly one card. Of course, Alexander can send the same card multiple times.

Alexander and each his friend has the list of preferences, which is a permutation of integers from 1 to n. The first number in the list is the number of the favorite card, the second number shows the second favorite, and so on, the last number shows the least favorite card.

Your task is to find a schedule of sending cards for Alexander. Determine at which moments of time Alexander must send cards to his friends, to please each of them as much as possible. In other words, so that as a result of applying two Alexander's rules, each friend receives the card that is preferred for him as much as possible.

Note that Alexander doesn't choose freely what card to send, but he always strictly follows the two rules.

当一丝不苟的杰拉尔德正在摆桌子时,亚历山大刚刚在 Codeforces 上发表了另一篇帖子,并开始回复朋友们的新年祝福。亚历山大有 nn 位朋友,每位朋友恰好向他发送一张电子贺卡。我们按贺卡发送的顺序,将这些朋友编号为 11 至 nn。同样地,我们也对贺卡进行相同编号:即第 ii 位朋友发送给亚历山大的贺卡编号为 ii。

亚历山大也会向朋友们回赠贺卡,但他并不上网寻找新贺卡;他只是重复使用此前收到的贺卡(尽管有时他确实需要补充一些关键细节)。初始时,亚历山大手中没有任何贺卡。亚历山大始终严格遵守以下两条规则:

  1. 他绝不会将某位朋友发给他的贺卡再回送给该朋友;
  2. 在当前他手中所有可选的贺卡中(即满足规则 1 的贺卡),亚历山大总是选择他自己最喜欢的那一张。

亚历山大计划向每位朋友恰好发送一张贺卡。当然,亚历山大可以将同一张贺卡多次发送给不同朋友。

亚历山大本人以及他的每位朋友都拥有一份偏好列表,该列表是整数 11 到 nn 的一个排列。列表中第一个数字表示其最偏爱的贺卡编号,第二个数字表示其次偏爱的贺卡编号,依此类推,最后一个数字表示其最不偏爱的贺卡编号。

你的任务是为亚历山大制定一份贺卡发送时间表:确定亚历山大应在哪些时刻向朋友们发送贺卡,以使每位朋友最终收到的贺卡尽可能符合其个人偏好。换言之,就是在严格遵循上述两条规则的前提下,使得每位朋友所收到的贺卡,在其偏好列表中的排名尽可能靠前。

请注意:亚历山大并非自由选择发送哪张贺卡,而是始终严格遵循上述两条规则。

输入格式

The first line contains an integer n (2 ≤ n ≤ 300) — the number of Alexander's friends, equal to the number of cards. Next n lines contain his friends' preference lists. Each list consists of n different integers from 1 to n. The last line contains Alexander's preference list in the same format.

第一行包含一个整数 nn(2≤n≤3002 \leq n \leq 300)—— 表示亚历山大的朋友数量,也等于卡片的数量。接下来的 nn 行包含他朋友们的偏好列表。每个列表由 nn 个互不相同的整数构成,取值范围为 11 到 nn。最后一行以相同格式给出亚历山大的偏好列表。

输出格式

Print n space-separated numbers: the i-th number should be the number of the friend, whose card Alexander receives right before he should send a card to the i-th friend. If there are several solutions, print any of them.

输出 n 个用空格分隔的数字:其中第 i 个数字应为亚历山大在向第 i 位朋友寄送明信片之前所收到的明信片所属的朋友编号。若存在多种解,输出任意一种即可。

输入输出样例

  • 输入#1

    4
    1 2 3 4
    4 1 3 2
    4 3 1 2
    3 4 2 1
    3 1 2 4

    输出#1

    2 1 1 4

说明/提示

In the sample, the algorithm of actions Alexander and his friends perform is as follows:

  1. Alexander receives card 1 from the first friend.
  2. Alexander sends the card he has received (at the moment he only has one card, and therefore it is the most preferable for him) to friends with the numbers 2 and 3.
  3. Alexander receives card 2 from the second friend, now he has two cards — 1 and 2.
  4. Alexander sends a card to the first friend. Despite the fact that Alexander likes card 1 more, he sends card 2 as he cannot send a friend the card sent by that very friend.
  5. Alexander receives card 3 from the third friend.
  6. Alexander receives card 4 from the fourth friend.
  7. Among the cards Alexander has number 3 is his favorite and he sends it to the fourth friend.

Note that Alexander can send cards to multiple friends at a time (in this case the second and the third one). Alexander can send card 3 to the fourth friend after he receives the third card or after he receives the fourth card (both variants are correct).

在样例中,亚历山大及其朋友们执行操作的算法如下:

  1. 亚历山大从第一位朋友处收到卡片 1。
  2. 亚历山大将他所收到的卡片(此时他仅有一张卡片,因此这张卡片对他而言最优先)发送给编号为 2 和 3 的朋友。
  3. 亚历山大从第二位朋友处收到卡片 2,此时他拥有两张卡片:1 和 2。
  4. 亚历山大向第一位朋友发送一张卡片。尽管亚历山大更喜欢卡片 1,但他发送的是卡片 2,因为他不能将某位朋友送来的卡片再回送给该朋友。
  5. 亚历山大从第三位朋友处收到卡片 3。
  6. 亚历山大从第四位朋友处收到卡片 4。
  7. 在亚历山大当前拥有的卡片中,编号为 3 的卡片是他最喜欢的,因此他将卡片 3 发送给第四位朋友。

注意:亚历山大可同时向多位朋友发送卡片(本例中即第二位和第三位朋友)。亚历山大可在收到第三张卡片后,或在收到第四张卡片后,将卡片 3 发送给第四位朋友(两种方案均正确)。

输入解题思路,AI测评打分。不知道怎么写?

首页