Cow Laundry(POJ-2188)

Problem 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

题意:n 条绳子挂在上下两栏,然后分别输入 i 号挂钩上下分别连接的线的编号,每次只能交换上、下两栏中任意栏的相邻两绳子,问最小交换多少次后能让上下连接的线的编号相同

思路:实质是求逆序对的个数,可以利用归并排序来写,时间复杂度为 O(n*logn)

由于 n 只有 1000,因此不需要用归并排序,直接双重循环 O(n*n) 暴力求即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 5000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;
struct Node{
    int x,y;
    bool operator < (const Node &rhs) const{
        if(x==rhs.x)
            return y<rhs.y;
        return x<rhs.x;
    }
}node[N];
int n;
int a[N];
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d%d",&node[i].x,&node[i].y);

    for(int i=1;i<=n;++i)
		for(int j=1;j<=n;++j)
			if(node[j].x==node[i].y)
				a[j]=i;

    int res=0;
    for(int i=1;i<=n;i++){
        for(int j=i+1;j<=n;j++){
            if(a[i]>a[j])
                res++;
        }
    }
    printf("%d\n",res);

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值