CSU 2070 Seating Chart

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37957829/article/details/79966002

2070: Seating Chart

题目直通车:Seating Chart

题目大意:翻译完之后发现是先用map映射,离散化,再对序列求逆序数。

解题思路:映射成1到n的整数,离散化之后,用O(nlogn) 的方法求逆序数和,有线段树,归并排序等方法,这里采用归并排序即可。最后的答案是long long类型,稍微注意一下。

#include<cstdio>
#include<iostream>
#include<math.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<deque>
#include<stack>
#include<vector>
#include<stdlib.h>
#include<string.h>

using namespace std;
const int maxn = 1e5+10;
string str;
int a[maxn];
int arr[maxn];
int temp[maxn];
long long counts;
map<string,int> ma;
void fun2(int first,int mid,int last)
{
    int p1=first,p2=mid+1;
    int cur = 0;
    while(p1<=mid&&p2<=last)
    {
        if(arr[p1]<arr[p2])
            temp[cur++] = arr[p1++];
        else{
            temp[cur++] = arr[p2++];
            counts+=mid-p1+1;
        }
    }
    while(p1<=mid)
    {
        temp[cur++] = arr[p1++];
    }
    while(p2<=last)
    {
        temp[cur++] = arr[p2++];
    }
    for(int i=0;i<cur;i++)
    {
        arr[first++] = temp[i];
    }
}

void fun(int first,int last)
{
    if(first==last)
        return;
    int mid = (last+first)/2;            //修改@1
    fun(first,mid);
    fun(mid+1,last);
    fun2(first,mid,last);
}

int main()
{

    //freopen("in.txt","r",stdin);
    //ios::sync_with_stdio(false);
    int n;
    while(cin>>n && n!=0)
    {
        counts=0;
        memset(a,0,sizeof(a));
        ma.clear();
        for(int i=0; i<n; i++)
        {
            cin>>str;
            ma[str]=i+1;
        }
        for(int i=0;i<n;i++)
        {
            cin>>str;
            arr[i] = ma[str];
        }
        fun(0,n-1);
        cout<<counts<<endl;
    }
    return 0;
}
阅读更多

没有更多推荐了,返回首页