火柴排队

Posted ainiyuling

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了火柴排队相关的知识,希望对你有一定的参考价值。

题面

树状数组求逆序对

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 100010;
const int maxm = 99999997;
struct MyStruct

    int data;
    int loc;
a[maxn],b[maxn];
int e[maxn], n, c[maxn];
int inline readint()

    int x = 0;
    char c = getchar();
    while (c<‘0‘ || c>‘9‘) c = getchar();
    while (c >= ‘0‘&&c <= ‘9‘)
    
        x = x * 10 + c - ‘0‘;
        c = getchar();
    
    return x;

int lowbit(int x)

    return x&-x;

void add(int x,int t)

    while (x <= n)
    
        e[x] += t;
        e[x] %= maxm;
        x += lowbit(x);
    

int sum(int x)

    int s = 0;
    while(x)
    
        s += e[x];
        s %= maxm;
        x -= lowbit(x);
    
    return s;

bool cmp(MyStruct x, MyStruct y)

    return x.data < y.data;

int main()

    n = readint();
    for (int i = 1; i <= n; i++)
    
        a[i].data = readint();
        a[i].loc = i;
    
    for (int i = 1; i <= n; i++)
    
        b[i].data = readint();
        b[i].loc = i;
    
    sort(a + 1, a + n + 1, cmp);
    sort(b + 1, b + n + 1, cmp);
    for (int i = 1; i <= n; i++)
    
        c[a[i].loc] = b[i].loc;
    
    int ans = 0;
    for (int i = 1; i <= n; i++)
    
        add(c[i], 1);
        ans += i - sum(c[i]);
        ans %= maxm;
    
    printf("%d", ans);
    return 0;

  

以上是关于火柴排队的主要内容,如果未能解决你的问题,请参考以下文章

codevs 3286 火柴排队

火柴排队

NOIP2013火柴排队

rqnoj(PID737 / 火柴排队 match)

1966 火柴排队

COdevs 3286 火柴排队