首页 > 其他分享 >POJ2188

POJ2188

时间:2023-02-04 11:04:55浏览次数:49  
标签:slot wire bottom int lines POJ2188 clothes


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对数字,第一个数字是规定位置,第二个数字是现在的位置,每个数字可以和相邻数字进行交换,求最少交换几次可以变到规定位置。

先找到当前位置对应的规定位置,大的应该在下面,找到每个数后有几个比自己小的,全部的和就是要变换的次数。

AC代码:
 

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define maxn 1000005
using namespace std;
int a[1010];
ll n,m;
int i,j,k,l;
struct node
{
int x,y;
}d[1010];
int main()
{
while(~scanf("%d",&n))
{
for(i=0;i<n;i++)
scanf("%d %d",&d[i].x,&d[i].y);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(d[i].x==d[j].y)
a[j]=i;
}
}
int cnt=0;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[j]<a[i])
cnt++;
}
}
printf("%d\n",cnt);
}
return 0;
}

 

标签:slot,wire,bottom,int,lines,POJ2188,clothes
From: https://blog.51cto.com/u_15952369/6036923

相关文章