Cow Laundry

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 1376 Accepted: 886

Description

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.

Input

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

Output

  • Line 1: A single integer specifying the minimum number of adjacent swaps required to straighten out the clothes lines.

    Sample Input

4

4 1

2 3

1 4

3 2

Sample Output

4

Source

USACO 2003 Fall Orange

找到规律的话就是求有多少逆序对,还是比较好想的,但是要处理的话,这个逆序是相对于开始状态,不是另一端点,代码比较简单,思维题,签到题)

#include<iostream>
#include<map>
using namespace std;
int ob[1005];
int oj[1005];
map<int,int>w;
int main()
{
int n;
w.clear();
cin>>n;
for(int i=0;i<n;i++){
cin>>ob[i]>>oj[i];
w.insert(make_pair(ob[i],i));
}
int ans=0;
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(w[oj[i]]>w[oj[j]]) ans++;
cout<<ans<<endl;
}

POJ 2188 Cow Laundry的更多相关文章

  1. POJ 3045 Cow Acrobats (贪心)

    POJ 3045 Cow Acrobats 这是个贪心的题目,和网上的很多题解略有不同,我的贪心是从最下层开始,每次找到能使该层的牛的风险最小的方案, 记录风险值,上移一层,继续贪心. 最后从遍历每一 ...

  2. poj 3348 Cow 凸包面积

    Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8122   Accepted: 3674 Description ...

  3. POJ 3660 Cow Contest / HUST 1037 Cow Contest / HRBUST 1018 Cow Contest(图论,传递闭包)

    POJ 3660 Cow Contest / HUST 1037 Cow Contest / HRBUST 1018 Cow Contest(图论,传递闭包) Description N (1 ≤ N ...

  4. POJ 3176 Cow Bowling(dp)

    POJ 3176 Cow Bowling 题目简化即为从一个三角形数列的顶端沿对角线走到底端,所取得的和最大值 7 * 3 8 * 8 1 0 * 2 7 4 4 * 4 5 2 6 5 该走法即为最 ...

  5. POJ 2184 Cow Exhibition【01背包+负数(经典)】

    POJ-2184 [题意]: 有n头牛,每头牛有自己的聪明值和幽默值,选出几头牛使得选出牛的聪明值总和大于0.幽默值总和大于0,求聪明值和幽默值总和相加最大为多少. [分析]:变种的01背包,可以把幽 ...

  6. POJ 2375 Cow Ski Area(强连通)

    POJ 2375 Cow Ski Area id=2375" target="_blank" style="">题目链接 题意:给定一个滑雪场, ...

  7. Poj 3613 Cow Relays (图论)

    Poj 3613 Cow Relays (图论) 题目大意 给出一个无向图,T条边,给出N,S,E,求S到E经过N条边的最短路径长度 理论上讲就是给了有n条边限制的最短路 solution 最一开始想 ...

  8. POJ 3660 Cow Contest

    题目链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  9. poj 1985 Cow Marathon

    题目连接 http://poj.org/problem?id=1985 Cow Marathon Description After hearing about the epidemic of obe ...

随机推荐

  1. 登录窗口java

    这次代码是登录窗口的制作. 主要的方面是是包括,用户名.密码.验证码.以及输入数据所需要的文本框,对于验证码可以通过点击验证码进行修改.同时对于验证码的前景色和背景色同时都得到修改. 点击注册(这里还 ...

  2. MTK Android Driver :Key

    MTK Android Driver :Key 1.按键配置(根据原理图):DCT(Driver Customization Tool): ..\mediatek\custom\prj\kernel\ ...

  3. wireshark抓包实战(一),抓包原理

    一.什么样的"包"能被wireshark抓住呢? 1.本机 即直接抓取进出本机网卡的流量包.这种情况下,wireshark会绑定本机的一块网卡. 2.集线器 用于抓取流量泛洪,冲突 ...

  4. linux中忘记了mysql的root用户的密码怎么办?

    1.vim /etc/my.cnf skip-grant-tables  #取消此行的注释 2.重启mysql systemctl restart mysqld 3.mysql 登陆mysql mys ...

  5. Linux命文件与目录属性

    一.linux系统中文件标志 d ===> 目录 - ===> 文件 l ===> 连接文件 b ===> 可供存储设备文件 c ===> 串形端口设备文件(鼠标,键盘) ...

  6. <E> 泛型

    /* * 使用集合存储自定义对象并遍历 * 由于集合可以存储任意类型的对象,当我们存储了不同类型的对象,就有可能在转换的时候出现类型转换异常, * 所以java为了解决这个问题,给我们提供了一种机制, ...

  7. Linux CentOS7 开通端口外网端口访问权限

    一.查看系统防火墙状态(如果返回 running 代表防火墙启动正常)firewall-cmd --state二.开启端口外网访问1.添加端口 返回 success 代表成功(--permanent永 ...

  8. Python爬虫系列(二):requests基础

    1.发送请求: import requests # 获取数据#r是一个 response 对象.包含请求返回的内容r = requests.get('https://github.com/timeli ...

  9. Linux下删除大量文件效率对比

    来自公众号:马哥Linux运维 今天我们来测试一下Linux下面删除大量文件的效率. 首先建立50万个文件 $ test   for i in $(seq 1 500000);do echo text ...

  10. Linux c++ vim环境搭建系列(3)——Ubuntu18.04.4编译安装youcompleteme

    3. youcompleteme编译安装 参考网址: https://github.com/ycm-core/YouCompleteMe#linux-64-bit 建议不要用这个博客的方法: http ...