当前你的浏览器版本过低,网站已在兼容模式下运行,兼容模式仅提供最小功能支持,网站样式可能显示不正常。
请尽快升级浏览器以体验网站在线编辑、在线运行等功能。

建议使用的浏览器:

谷歌Chrome 火狐Firefox Opera浏览器 微软Edge浏览器 QQ浏览器 360浏览器 傲游浏览器

2188:Cow Laundry

题目描述
The cows have erected clothes lines with N (1 <= N <= 1000) wires upon which they can dry their clothes after washing them. Having no opposable thumbs, they have thoroughly botched the job. Consider this clothes line arrangement with four wires:

Wire slot: 1 2 3 4
--------------- <-- the holder of the wires
\ \ / /
\ \/ /
\ /\ /
\ / \ / <-- actual clothes lines
/ \
/ \ / \
/ \/ \
/ /\ \
/ / \ \
--------------- <-- the holder of the wires
Wire slot: 1 2 3 4

The wires cross! This is, of course, unacceptable.

The cows want to unscramble the clothes lines with a minimum of hassle. Even their bovine minds can handle the notion of "swap these two lines". Since the cows have short arms they are limited to swapping adjacent pairs of wire endpoints on either the top or bottom holder.
In the diagram above, it requires four such swaps in order to get a proper arrangement for the clothes line:

1 2 3 4
------------- <-- the holder of the wires
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
------------- <-- the holder of the wires
1 2 3 4

Help the cows unscramble their clothes lines. Find the smallest number of swaps that will get the clothes line into a proper arrangement.

You are supplied with clothes line descriptions that use integers to describe the current ordering of the clothesline. The lines are uniquely numbered 1..N according to no apparent scheme. You are given the order of the wires as they appear in the N connecting slots of the top wire holder and also the order of the wires as they appear on the bottom wire holder.
输入解释
* Line 1: A single integer: N

* Lines 2..N+1: Each line contains two integers in the range 1..N. The first integer is the wire ID of the wire in the top wire holder; the second integer is the wire ID of the wire in the bottom holder. Line 2 describes the wires connected to top slot 1 and bottom slot 1, respectively; line 3 describes the wires connected to top and bottom slot 2, respectively; and so on.

输出解释
* Line 1: A single integer specifying the minimum number of adjacent swaps required to straighten out the clothes lines.
输入样例
4
4 1
2 3
1 4
3 2
输出样例
4

该题目是Virtual Judge题目,来自 北京大学POJ

题目来源 USACO 2003 Fall Orange

源链接: POJ-2188

最后修改于 2020-10-29T06:26:04+00:00 由爬虫自动更新

共提交 0

通过率 --%
时间上限 内存上限
1000 65536