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. json的fromjson的方法使用。可以在volley中进行使用

    Gson提供了fromJson()方法来实现从Json相关对象到Java实体的方法. 在日常应用中,我们一般都会碰到两种情况,转成单一实体对象和转换成对象列表或者其他结构. 先来看第一种: 比如jso ...

  2. zendframework3

    1.开发时关闭cache,正式上线后打开cache application config file (config/application.config.php),  disable this cac ...

  3. Django 已生成数据时怎么查询数据库

    数据库已写好时,怎样查询数据库 1.输入命令:python manage.py inspectdb  > model1.py     注:>重定向 到model1.py

  4. 20175110 王礼博 exp4恶意代码分析

    目录 1.基础知识 2.系统运行监控 3.恶意软件分析 4.基础问题回答 5.实践总结与体会 1. 基础知识 1.1 恶意代码的概念与分类 定义:指故意编制或设置的.对网络或系统会产生威胁或潜在威胁的 ...

  5. SQL基础系列(3)-变量、函数、存储过程等

    1.    变量 定义变量 DECLARE @a INT 赋值 PRINT @a ) --select 赋值 SELECT @name='zcx' PRINT @name SELECT @name=F ...

  6. [算法]素数筛法(埃氏筛法&线性筛法)

    目录 一.素数筛的定义 二.埃氏筛法(Eratosthenes筛法) 三.线性筛法 四.一个性质 一.素数筛的定义 给定一个整数n,求出[1,n]之间的所有质数(素数),这样的问题为素数筛(素数的筛选 ...

  7. Python爬虫某招聘网站的岗位信息

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:阿尔法游戏 PS:如有需要Python学习资料的小伙伴可以加点击下方链 ...

  8. C - N皇后问题 DFS

    在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上. 你的任务是,对于给定的N,求出有多少种合法的放置方法. Inpu ...

  9. cmd命令行中查看、修改、删除与添加环境变量

    注意:只在当前窗口生效!! 1.查看当前所有可用的环境变量:输入 set 即可查看. set 2.查看某个环境变量:输入 “set 变量名”即可 set python 3.修改环境变量 :输入 “se ...

  10. MRCTF Ezpop_Revenge小记

    前言 一道typecho1.2的反序列化,顺便记录一下踩的坑 www.zip获得源码,结构大致如下 flag.php需要ssrf,如果成功会写入session 拿到源码直接去网上先找了一下有没有现成的 ...