题目传送门

 /*
题意:给了两行的数字,相同的数字连线,问中间交点的个数
逆序数:第一行保存每个数字的位置,第二行保存该数字在第一行的位置,接下来就是对它求逆序数
用归并排序或线段树求。想到了就简单了:)
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std; typedef long long ll;
const int MAXN = 1e5 + ;
const int INF = 0x3f3f3f3f;
int p[MAXN], a[MAXN];
int L[MAXN/+], R[MAXN/+];
ll ans; void Merge(int *a, int p, int q, int r)
{
int n1 = q - p + , n2 = r - q;
for (int i=; i<=n1; ++i) L[i] = a[p+i-];
for (int i=; i<=n2; ++i) R[i] = a[q+i];
L[n1+] = INF; R[n2+] = INF; int i = , j = ;
for (int k=p; k<=r; ++k)
{
if (L[i] <= R[j]) a[k] = L[i++];
else {a[k] = R[j++]; ans += n1 - i + ;}
}
} void MergeSort(int *a, int p, int r)
{
if (p < r)
{
int q = (p + r) / ;
MergeSort (a, p, q);
MergeSort (a, q + , r);
Merge (a, p, q, r);
}
} int main(void) //UVALive 6508 Permutation Graphs
{
// freopen ("I.in", "r", stdin); int t; scanf ("%d", &t);
while (t--)
{
int n; scanf ("%d", &n);
for (int i=; i<=n; ++i)
{
int x; scanf ("%d", &x); p[x] = i;
}
for (int i=; i<=n; ++i)
{
int x; scanf ("%d", &x); a[i] = p[x];
} ans = ;
MergeSort (a, , n);
printf ("%lld\n", ans);
} return ;
}

逆序数 UVALive 6508 Permutation Graphs的更多相关文章

  1. UVALive 6508 Permutation Graphs

    Permutation Graphs Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

  2. HDU 1394 Minimum Inversion Number(最小逆序数/暴力 线段树 树状数组 归并排序)

    题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS     Memory Limit: 32768 K Description The inve ...

  3. HDU-Minimum Inversion Number(最小逆序数)

    Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of ...

  4. 逆序数2 HDOJ 1394 Minimum Inversion Number

    题目传送门 /* 求逆序数的四种方法 */ /* 1. O(n^2) 暴力+递推 法:如果求出第一种情况的逆序列,其他的可以通过递推来搞出来,一开始是t[1],t[2],t[3]....t[N] 它的 ...

  5. HDU 1394 Minimum Inversion Number(线段树/树状数组求逆序数)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  6. Codeforces Gym 100463A Crossings 逆序数

    Crossings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463 Description ...

  7. [HDU POJ] 逆序数

    HDU 1394 Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3276 ...

  8. [HDU] 1394 Minimum Inversion Number [线段树求逆序数]

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  9. hdu 1394 zoj 1484 求旋转序列的逆序数(并归排序)

    题意:给出一序列,你可以循环移动它(就是把后面的一段移动到前面),问可以移动的并产生的最小逆序数. 求逆序可以用并归排序,复杂度为O(nlogn),但是如果每移动一次就求一次的话肯定会超时,网上题解都 ...

随机推荐

  1. Meteor模板

    Meteor模板使用三个顶级标签.前两个是 head 和 body 标签.这些标签和在普通的HTML中做的工作一样.第三个标签 template.这是我们将HTML连接到JavaScript的地方. ...

  2. Samba简单配置--匿名用户共享资料可读可写的实现

    http://e-mailwu.blog.163.com/blog/static/65104036200931893921923/ http://www.cnblogs.com/god_like_do ...

  3. linux迁移至固态硬盘全过程

    自从台式机上用上固态硬盘后,就再也受不了笔记本上的5400转的机械硬盘了,所以这次又买了块固态硬盘打算装到笔记本上. 笔记本里装的是Ubuntu 14.04 + Win7双系统,Win7主要偶尔运行一 ...

  4. Office EXCEL 2010如何启用宏编辑器,打开VB编辑器

    文件-选项-主选项卡,勾选开发工具 然后在开发工具中找到Visual Basic编辑器,打开代码

  5. 各项异性滤波简单介绍Anisotropic Filtering(AF)

    本文主要整理简绍来自互联网的各项异性滤波的知识. 原文链接:http://www.linuxgraphics.cn/graphics/using_anisotropic_texture_filteri ...

  6. 16款创建CSS3动画的jQuery插件

    jQuery插件是用来扩展jQuery原型对象的方法. 本文搜集了用来为你的站点创建CSS3动画的一些jQuery插件. 1. jQuery Smoove Smoove 简化了CSS3转换效果.使得页 ...

  7. java纯数字加密解密实例

    我们都知道,在用户加入信息时,一些比較敏感的信息,如身份证号,手机号,用户的登录password等信息,是不能直接明文存进数据库的.今天我们就以一个详细的样例来说明一下纯数字的java加密解密技术. ...

  8. 2016/3/21 面向对象: ①定义类 ②实例化对象 ③$this关键字 ④构造函数 ⑤析构函数 ⑥封装 ⑦继承

    一:定义类   二:实例化对象 //定义类 class Ren { var $name; var $sex; var $age; function Say() { echo "{$this- ...

  9. struts2 中 result type="stream"

    Stream result type是Struts2中比较有用的一个feature.特别是在动态生成图片和文档下载的情况下 1:图片验证码: Action类,action主要要提供一个获取InputS ...

  10. How to use filters in a GridPanel

    You can just link statically required files in your index.html <link rel="stylesheet" t ...