#P8186. [USACO22FEB] Redistributing Gifts S

[USACO22FEB] Redistributing Gifts S

题目描述

Farmer John has NN gifts labeled 1N1\cdots N for his NN cows, also labeled 1N1\cdots N (1N500)(1\le N\le 500). Each cow has a wishlist, which is a permutation of all NN gifts such that the cow prefers gifts that appear earlier in the list over gifts that appear later in the list.

FJ was lazy and just assigned gift ii to cow ii for all ii. Now, the cows have gathered amongst themselves and decided to reassign the gifts such that after reassignment, every cow ends up with the same gift as she did originally, or a gift that she prefers over the one she was originally assigned.

For each ii from 11 to NN, compute the most preferred gift cow ii could hope to receive after reassignment.

输入格式

The first line contains NN. The next NN lines each contain the preference list of a cow. It is guaranteed that each line forms a permutation of 1N1\cdots N.

输出格式

Please output NN lines, the ii-th of which contains the most preferred gift cow ii could hope to receive after reassignment.

题目大意

题目描述

FJ 有 NN 个礼物给他的 NN 头奶牛,这 NN 个礼物和 NN 头奶牛都被标记为 1N(1N500)1 \dotsm N (1 \le N \le 500) 。 每头奶牛都有一个愿望单,记录着一个含有 NN 个礼物的排列。比起在愿望单中出现更晚的礼物,奶牛更喜欢先出现在愿望单中的礼物。

因为 FJ 太懒了,他直接把 ii 号礼物分配给了 ii 号奶牛。现在,奶牛们聚在了一起,决定重新分配礼物,以便在重新分配后,每头奶牛都能得到跟原来一样,或是它更喜欢的礼物。

对于每个 iiii11NN),计算出重新分配后, ii 号奶牛可能拿到的最好的礼物(这个奶牛经过重新分配后能拿到的最喜欢的礼物)。

输入格式

第一行输入 NN 。之后 NN 行每行包含一个奶牛的愿望单。保证这 NN 行都是 1N1 \dotsm N 的排列

输出格式

输出 NN 行,第 ii 行输出重新分配后 ii 号奶牛可能得到的最好礼物。

样例解释

在这个例子中,有两种可能的分配方案

  • 最初的方案:一号奶牛得到一号礼物,二号奶牛得到二号礼物,三号奶牛得到三号礼物,四号奶牛得到四号礼物
  • 重新分配后的方案:一号奶牛得到一号礼物,二号奶牛得到三号礼物,三号奶牛得到二号礼物,四号奶牛得到四号礼物。可以发现一号和四号奶牛都拿不到比FJ分配的更好的礼物。不过二号和三号都可以。

数据范围

  • 232 \sim 3 号测试点满足 N8N \le 8
  • 4114 \sim 11 号测试点没有别的限制

tzyt 翻译

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

提示

【样例解释】

In this example, there are two possible reassignments:

  • The original assignment: cow 1 receives gift 1, cow 2 receives gift 2, cow 3 receives gift 3, and cow 4 receives gift 4.
  • Cow 1 receives gift 1, cow 2 receives gift 3, cow 3 receives gift 2, and cow 4 receives gift 4. Observe that both cows 1 and 4 cannot hope to receive better gifts than they were originally assigned. However, both cows 2 and 3 can.

【数据范围】

  • Test cases 2-3 satisfy N8N\le 8.

  • Test cases 4-11 satisfy no additional constraints.