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 @   纯粹的  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
点击右上角即可分享
微信分享提示