bzoj4430 [Nwerc2015]Guessing Camels赌骆驼

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4430

【题解】

把每只骆驼在第一个人、第二个人、第三个人的位置找出来,然后做三维偏序即可。

排序+cdq分治+BIT

# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 2e5 + 10;
const int mod = 1e9+7;

# define RG register
# define ST static

int n, a[M], b[M], c[M], pb[M], pc[M];
ll ans = 0;

struct pa {
    int a, b, c, ans;
    pa() {}
    pa(int a, int b, int c, int ans) : a(a), b(b), c(c), ans(ans) {}
    friend bool operator <(pa a, pa b) {
        return a.b < b.b || (a.b == b.b && a.a < b.a) ||
               (a.b == b.b && a.a == b.a && a.c < b.c);
    }
}p[M], t[M];

struct BIT {
    int n, c[M];
    # define lb(x) (x&(-x))
    inline void set(int _n) {
        n = _n;
        memset(c, 0, sizeof c);
    }
    inline void edt(int x, int d) {
        for (; x<=n; x+=lb(x)) c[x] += d;
    }
    inline int sum(int x) {
        int ret = 0;
        for (; x; x-=lb(x)) ret += c[x];
        return ret;
    }
    inline int sum(int x, int y) {
        if(x > y) return 0;
        return sum(y) - sum(x-1);
     }
}T;

inline void solve(int l, int r) {
    if(l == r) return;
    int mid = l+r>>1, t1n = l-1, t2n = mid;
    for (int i=l; i<=r; ++i)
        if(p[i].a <= mid) t[++t1n] = p[i];
        else t[++t2n] = p[i];
    for (int i=l; i<=r; ++i) p[i] = t[i];
    int j=l;
    for (int i=mid+1; i<=r; ++i) {
        while(j<=mid && p[j].b <= p[i].b) T.edt(p[j].c, 1), j++;
        p[i].ans += T.sum(p[i].c);
    }
    for (int i=l; i<j; ++i) T.edt(p[i].c, -1);
    solve(l, mid);
    solve(mid+1, r);
}

int main() {
    cin >> n; T.set(n);
    for (int i=1; i<=n; ++i) scanf("%d", a+i);
    for (int i=1; i<=n; ++i) scanf("%d", b+i), pb[b[i]] = i;
    for (int i=1; i<=n; ++i) scanf("%d", c+i), pc[c[i]] = i;
    for (int i=1; i<=n; ++i) p[i] = pa(i, pb[a[i]], pc[a[i]], 0);
//    for (int i=1; i<=n; ++i) printf("%d %d %d\n", p[i].a, p[i].b, p[i].c);
    sort(p+1, p+n+1);
    solve(1, n);
    for (int i=1; i<=n; ++i) ans += p[i].ans;
    cout << ans;
    return 0;
}

时间: 2024-10-12 21:03:12

bzoj4430 [Nwerc2015]Guessing Camels赌骆驼的相关文章

bzoj 4430: [Nwerc2015]Guessing Camels赌骆驼

4430: [Nwerc2015]Guessing Camels赌骆 Description Jaap, Jan, and Thijs are on a trip to the desert after having attended the ACM ICPC World Finals 2015 in Morocco. The trip included a camel ride, and after returning from the ride, their guide invited th

【BZOJ】4430: [Nwerc2015]Guessing Camels赌骆驼

[题意]给定三个长度为n的排列,求在三个排列中顺序相同的数对个数. [算法]逆序对 [题解]很容易联想到NOIP火柴排队,涉及顺序问题显然和逆序对息息相关. 一个数对如果在三个排列中顺序不同,一定是1+2或2+1,也就是只在两数列之间顺序相同. 所以对三个数列两两求逆序对总数num,则不满足要求的数对一定会产生且仅产生两个逆序对,ans=n*(n-1)/2-num/2. #include<cstdio> #include<cstring> #include<algorithm

BZOJ 4430 Guessing Camels赌骆驼

[题意概述] 给出三个n的排列,求有多少个数对在三个排列中顺序相同 [题解] 考虑用补集转化的方法,答案为总对数-不满足的对数 一对数不满足条件,当且仅当这对数在两个排列中顺序相同,在另一个排列中的顺序不同. 统计两两之间不满足偏序条件的数对的个数,那么每对数都被统计了两次 #include<cstdio> #include<algorithm> #include<cstring> #define LL long long using namespace std; co

Guessing Camels (***)

Guessing Camels Jaap, Jan, and Thijs are on a trip to the desert after having attended the ACM ICPC World Finals 2015 in Morocco. The trip included a camel ride, and after returning from the ride, their guide invited them to a big camel race in the e

BZOJ 4430 Guessing Camels

Description Jaap, Jan, and Thijs are on a trip to the desert after having attended the ACM ICPC World Finals 2015 in Morocco. The trip included a camel ride, and after returning from the ride, their guide invited them to a big camel race in the eveni

越狱Season 1-Episode 13: End of the Tunnel

Season 1, Episode 13: End of the Tunnel -Fernando: The name is John Abruzzi. 名字是John Abruzzi A b r u z Z... A b r u z Z... I don't care what protocol is. protocol: 规程 care: 关心,在乎 管你什么规定 I just want to know if he's okay. 我只想知道他情况怎样 Hello? Hello? 喂? 喂?

uva 1521 - GCD Guessing Game(贪心)

题目链接:uva 1521 - GCD Guessing Game 题目大意:给定一个数N,现在又一个数x,在1~N之间,现在每次可以猜一个数a,返回gcd(x,a),问说最少猜几次可以确定x. 解题思路:其实就将1~N里面的素数都要考虑一遍,因为有一个N的限制,所以每次选出来的素数的积不大于N即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const

匈牙利命名法,骆驼命名法(camel),帕斯卡(Pascal)命名法(转)

一.匈牙利命名法      Windows 编程中用到的变量(还包括宏)的命名规则匈牙利命名法,这种命名技术是由一位能干的 Microsoft 程序员查尔斯·西蒙尼(Charles Simonyi) 提出的. 匈牙利命名法基本原则是:变量名=属性+类型+对象描述,通过在变量名前面加上相应的小写字母的符号标识作为前缀,标识出变量的作用域,类型等.这些符号可以多个同时使用,顺序是先m_(成员变量),再指针,再简单数据类型,再其他.例如:m_lpszStr, 表示指向一个以0字符结尾的字符串的长指针成

hdu 1292 &quot;下沙野骆驼&quot;ACM夏令营

"下沙野骆驼"ACM夏令营 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 786    Accepted Submission(s): 377 Problem Description 大家都知道,杭电计算机学院为了吸引更多的学生参与到程序设计竞赛中去,从2005年秋天,开始举行月赛,并一直坚持到了现在.事实表明,这项措施的效