手画几下序列的变换后发现逆序对数是恒定的,故只需对第 $0$ 年求逆序对即可.
树状数组会 $TLE$ 的很惨,需要用到归并排序来求逆序对.
其实就是省掉了一个离散化的时间,估计能比树状数组快一半的时间.

#include <cstdio>
#include <algorithm>
#include <cstring>
#define N 2000004
#define ll long long
#define setIO(s) freopen(s".in", "r" , stdin)
using namespace std;
namespace IO
{
char *p1,*p2,buf[100000];
#define nc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
int readint() {int x=0; char c=nc(); while(c<48) c=nc(); while(c>47) x=(((x<<2)+x)<<1)+(c^48),c=nc(); return x;}
ll readll() {ll x=0; char c=nc(); while(c<48) c=nc(); while(c>47) x=(((x<<2)+x)<<1)+(c^48),c=nc(); return x;}
};
ll re = 0;
int a[N], bu[N], n ;
void solve(int l , int r)
{
if(l == r) return ;
int mid = (l + r) >> 1, tmp = 0, tl = l, tr = mid + 1;
solve(l, mid), solve(mid + 1, r);
while(tl <= mid && tr <= r)
{
if(a[tl] <= a[tr]) bu[++ tmp] = a[tl], ++tl;
else bu[++ tmp] = a[tr], ++tr , re += (ll) mid - tl + 1;
}
while(tl <= mid) bu[++ tmp] = a[tl], ++tl;
while(tr <= r) bu[++tmp] = a[tr], ++tr;
for(int i = 1; i <= tmp ; ++ i) a[l + i - 1] = bu[i];
}
int main()
{
using namespace IO;
// setIO("input");
int i , j;
ll oo;
n = readint();
for(i = 1 ; i <= n ; ++ i) a[i] = readint();
oo = readll();
solve(1, n), printf("%lld\n", re);
return 0;
}

  

BZOJ 4769: 超级贞鱼 逆序对 + 归并排序的更多相关文章

  1. bzoj 4769: 超级贞鱼 -- 归并排序

    4769: 超级贞鱼 Time Limit: 1 Sec  Memory Limit: 128 MB Description 马达加斯加贞鱼是一种神奇的双脚贞鱼,它们把自己的智慧写在脚上——每只贞鱼的 ...

  2. 【BZOJ4769】超级贞鱼 归并排序求逆序对

    [BZOJ4769]超级贞鱼 Description 马达加斯加贞鱼是一种神奇的双脚贞鱼,它们把自己的智慧写在脚上——每只贞鱼的左脚和右脚上各有一个数.有一天,K只贞鱼兴致来潮,排成一列,从左到右第i ...

  3. ROJ 1166 超级贞鱼

    1166: 超级贞鱼 Time Limit: 1 Sec  Memory Limit: 128 MB [Submit][Status] 传送门 Description 马达加斯加贞鱼是一种神奇的双脚贞 ...

  4. 洛谷P1908 求逆序对 [归并排序]

    题目描述 猫猫TOM和小老鼠JERRY最近又较量上了,但是毕竟都是成年人,他们已经不喜欢再玩那种你追我赶的游 戏,现在他们喜欢玩统计.最近,TOM老猫查阅到一个人类称之为“逆序对”的东西,这东西是这样 ...

  5. [剑指offer]51-数组中的逆序对(归并排序)

    题目链接 https://www.nowcoder.com/questionTerminal/96bd6684e04a44eb80e6a68efc0ec6c5 题意 在数组中的两个数字,如果前面一个数 ...

  6. 洛谷 P1908 逆序对(归并排序解法)

    树状数组解法:https://www.cnblogs.com/lipeiyi520/p/10846927.html 题目描述 猫猫TOM和小老鼠JERRY最近又较量上了,但是毕竟都是成年人,他们已经不 ...

  7. 力扣Leetcode 面试题51. 数组中的逆序对 - 归并排序

    在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数. 示例 1: 输入: [7,5,6,4] 输出: 5 限制: 0 <= ...

  8. 剑指 Offer 51. 数组中的逆序对 + 归并排序 + 树状数组

    剑指 Offer 51. 数组中的逆序对 Offer_51 题目描述 方法一:暴力法(双层循环,超时) package com.walegarrett.offer; /** * @Author Wal ...

  9. 九度OJ 1348 数组中的逆序对 -- 归并排序

    题目地址:http://ac.jobdu.com/problem.php?pid=1348 题目描述: 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求 ...

随机推荐

  1. RPCVersionCapError: Requested message version, 4.17 is incompatible. It needs to be equal in major version and less than or equal in minor version as the specified version cap 4.11.

    [问题描述] RPCVersionCapError: Requested message version, 4.17 is incompatible. It needs to be equal in ...

  2. Python数据基础类型-列表

    1,列表的创建 list1 = ['hello', 'world', 1997, 2000] list2 = [1, 2, 3, 4, 5 ] list3 = ["a", &quo ...

  3. Linux学习大纲(高人整理)

    1.Linux初级 1.1 OS操作系统的原理 1.2 了解常用命令 开机关机 时间管理:date cal clock 1.3 目的结构.目的管理 树形结构 tree cd 1.4 文件管理.文件查找 ...

  4. ALS部署Spark集群入坑记

    [Stage 236:> (0 + 0) / 400]17/12/04 09:45:55 ERROR yarn.ApplicationMaster: User class threw excep ...

  5. 实现 RSA 算法之改进和优化(第三章)(老物)

    第三章 如何改进和优化RSA算法 这章呢,我想谈谈在实际应用出现的问题和理解. 由于近期要开始各种忙了,所以写完这章后我短时间内也不打算出什么资料了=- =(反正平时就没有出资料的习惯.) 在讲第一章 ...

  6. 单元测试 - tox 使用

    1. 问题一 $ tox -e pep8 -- testdemo.server pep8 installed: alembic==,amqp==,appdirs==,Babel==,beautiful ...

  7. CentOS下配置Apache HTTPS

    一.安装Apache支持SSL/TLS yum install mod_ssl openssl 二.创建证书 证书(Cerificate)的基本作用是将一个公钥和安全个体(个人.公司.组织等)的名字绑 ...

  8. Thymeleaf后台传值读取

    /** * 测试用Controller *  * @author  * @date 2019-08-15 */@Controller@RequestMapping("/danyu/test& ...

  9. MySQL数据库数据类型以及INT(M)的含义

    nt(M)我们先来拆分,int是代表整型数据那么中间的M应该是代表多少位了,后来查mysql手册也得知了我的理解是正确的,下面我来举例说明.   MySQL 数据类型中的 integer types ...

  10. fdisk磁盘挂载

    1.查看磁盘信息 fdisk –l 2.分区 fdisk /dev/vdb :h 帮助命令 :p 查看 :n 进行分区 e  extended  逻辑分区 p  primary partition ( ...