P5149 会议座位

原题链接

题解

1.把字符串离散成数字(总不可能有重名的吧)
2.树状数组计算逆序数(比归并排序好写多了),即计算小于 \(i\) 的数的出现次数总和

code

#include<bits/stdc++.h>
#define lowbit(x) ((x)&(-x))
using namespace std;
unordered_map<string ,int > id;
int tree[100005]={0};
int n;
int query(int x)
{
    int sum=0;
    while(x)
    {
        sum+=tree[x];
        x-=lowbit(x);
    }
    return sum;
}

void update(int x)
{
    while(x<=n)
    {
        tree[x]++;
        x+=lowbit(x);
    }
}
int main()
{
    cin>>n;

    string s;
    for(int i=1;i<=n;i++)
    {
        cin>>s;
        id[s]=i;
    }

    int ans=0;
    for(int i=1;i<=n;i++)
    {
        cin>>s;
        update(id[s]);
        ans+=i-query(id[s]);
    }
    cout<<ans;
    return 0;
}

posted @ 2024-04-15 12:42  纯粹的  阅读(11)  评论(0)    收藏  举报