BZOJ 4769: 超级贞鱼 逆序对 + 归并排序
2024-09-05 04:38:44
手画几下序列的变换后发现逆序对数是恒定的,故只需对第 $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;
}
最新文章
- 深入理解CSS过渡transition
- linux内核分析作业3:跟踪分析Linux内核的启动过程
- delphi 弹出选择目录窗口
- Android属性动画之ObjectAnimator控制
- zabbix 微信报警
- cygwin 的不同文件类型显示不同的颜色
- PHP实现Restful风格的API
- 自己用反射写的一个request.getParameter工具类
- 【EF】 proxy
- ZooKeeper 安装部署及hello world
- 网络操作与AFNetworking
- UVA 297 Quadtrees(四叉树建树、合并与遍历)
- 如何在windows下载和安装Apache
- 从UUID想到的
- 应对不同格式 轻松转换PDF、WORD、PPT、TXT常用文件
- WebDriver元素等待机制
- VC++ 实现程序重启
- RocketMQ安装教程
- mysql匿名登录 导致创建不了数据库
- OSPF详解