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. Linq下有一个非常实用的SelectMany方法,很多人却不会用

    在平时开发中经常会看到有些朋友或者同事在写代码时会充斥着各种for,foreach,这种程式代码太多的话阅读性特别差,而且还显得特别累赘,其实在FCL中有很多帮助我们提高阅读感的方法,而现实中很多人不 ...

  2. 中阶d03 JDBC 使用

    1.首先在数据库中创建表 2.安装mysql驱动 java开发环境中导入jdbc连接mysql的jar包 mysql-connector-java-5.1.7-bin.jar 下载地址:https:/ ...

  3. hadoop(七)集群配置同步(hadoop完全分布式四)|9

    前置配置:rsync远程同步|xsync集群分发(hadoop完全分布式准备三)|9 1. 分布式集群分配原则 部署分配原则 说明Namenode和secondarynamenode占用内存较大,建议 ...

  4. MyBatis-Plus使用小结

    官网: https://mybatis.plus/ https://gitee.com/baomidou/mybatis-plus https://github.com/baomidou/mybati ...

  5. dubbo(三):负载均衡实现解析

    dubbo作为分布式远程调用框架,要保证的点很多,比如:服务注册与发现.故障转移.高性能通信.负载均衡等等! 负载均衡的目的是为了特定场景下,能够将请求合理地平分到各服务实例上,以便发挥所有机器的叠加 ...

  6. Julia的基本知识

    知识来源 1.变量.整数和浮点数 Julia和Matllab挺像的,基本的变量,数值定义都差不多,所以就没必要记录了. 2.数学运算 3.函数

  7. Salesforce学习 | 系统管理员Admin如何添加用户

    作为世界排名第一的CRM云计算软件,不管的是500强还是中小企业,越来越多的公司都选择使用Salesforce来分享客户信息,管理和开发具有更高收益的客户关系.Salesforce Administr ...

  8. Video tagging systems based on DNNs

    Need: With the ever-growth large-scale video in the mobile phone, so what will everyone get from the ...

  9. python_ck01(虚拟环境管理)

    拖拖拉拉的毛病还是依旧如初... 断断续续坚持三天总算把虚拟环境管理部分的内容给看完了. 对三天的知识点进行梳理,方便以后回顾. ①虚拟环境安装 用pip install + 包名的方式安装,涉及到的 ...

  10. 2016NOIP普及组T2回文日期

    回文日期 分类:枚举,函数 [题目描述] 日常生活中,通过年.月.日这三个要素可以表示出一个唯一确定的日期. 牛牛习惯用8位数字表示一个日期,其中,前4位代表年份,接下来2位代表月份,最后2位代表日期 ...