Problem H: Seating Chart
Description
Bilbo's birthday is coming up, and Frodo andSam are in charge of all the party planning! Theyhave invited all the hobbits of Middle Earth tothe party, and everyone will be sitting in a single row at anextremely long dining table.
However, due to poor communication, Frodo andSam have each independently put together a
seating chart for all the hobbits at thedining table. Help Frodo and Sam and out how similar theirseating charts are by counting the total numberof distinct pairs of hobbits who appear in different orders in thetwo charts.
Input
The input file will contain multiple testcases. Each test case begins with a single line containingan integer N (1<=N<=100,000) indicating the numberof hobbits. The next two lines represent Frodo's and Sam's seatingcharts, respectively. Each seating chart is specified as a singleline of N unique alphabetical strings;the set of strings in each line are guaranteed to be identical. Theend-of-input is denoted by a line containing the number0.
Output
For each input test case, output a singleinteger denoting, out of the N
Sample Input
Sample Output
1 3归并排序:
),问了学长才知道,原来还有#include
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
int a[100010],T[100010];
char tt[100000];
int N;
long long cnt;
void merge_sort(int *A,int x,int y,int *T)
{
if(y-x>1)
{
int m=x+(y-x)/2;
int p=x,q=m,i=x;
merge_sort(A,x,m,T);
merge_sort(A,m,y,T);
while(p<m||q<y)
{
if(q>=y||p<m&&A[p]<=A[q]) T[i++]=A[p++];
else
{
T[i++]=A[q++];
cnt+=m-p;
}
}
for(i=x; i<y; i++) A[i]=T[i];
}
}
int main()
{
map<string,int> h;
while(scanf("%d",&N),N)
{
int i;
for(i=0; i<N; i++)
{
scanf("%s",tt);
h[tt]=i;
}
for(i=0; i<N; i++)
{
scanf("%s",tt);
a[i]=h[tt];
}
cnt=0;
merge_sort(a,0,N,T);
printf("%lld\n",cnt);
}
return 0;
}
本文介绍了一种使用归并排序算法解决特定问题的方法,该问题是计算两个不同座位安排中出现的不同顺序的霍比特人对的数量。文章详细解释了归并排序的过程,并通过示例代码展示了如何将字符串映射到整数以提高效率。
1454

被折叠的 条评论
为什么被折叠?



