逆序对__归并排序__树状数组 Inversions SGU - 180
Input
Output
Sample test(s)
Input
2 3 1 5 4
Output
#include <cstdio>
#include <iostream>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm> #define sf scanf
#define pf printf
#define fp(x) freopen((x), "r", stdin) typedef long long ll; using namespace std; const int maxn = 1e5; int qarr[maxn];
ll qans; //long long 不然会over void merge(int arr[], int left, int right, int tarr[])
{
if (left>=right) return ;
int m = (left + right) >> ;
merge(arr, left, m, tarr);
merge(arr, m+, right, tarr);
int i, j, cnt;
i = left;
j = m + ;
cnt = ;
while (i<=m && j<=right) {
if (arr[i] <= arr[j])
tarr[cnt++] = arr[i++];
else {
tarr[cnt++] = arr[j++];
qans += (m - i + );
} }
while (i<=m) tarr[cnt++] = arr[i++];
while (j<=right) tarr[cnt++] = arr[j++];
for (i=; i<cnt; ++i) arr[left++] = tarr[i];
} void mergesort(int arr[], int left, int right)
{
int *p = new int[right-left+];
merge(arr, left, right, p);
} int main()
{
int n;
sf("%d", &n); for (int i=; i<=n; ++i) sf("%d", &qarr[i]); mergesort(qarr, , n);
cout << qans << endl; return ;
}
#include <cstdio>
#include <iostream>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm> #define sf scanf
#define pf printf
#define fp(x) freopen((x), "r", stdin) typedef long long ll; using namespace std; const int maxn = 1e5; struct nobe{
int id;
int val;
int ls;
bool operator < (const nobe &a) const {
if (val != a.val) return val < a.val;
return id < a.id;
}
}te[maxn]; int qsum[maxn];
ll qans; bool cmp(const nobe &a, const nobe &b)
{
return a.id < b.id;
} inline int lowbit(int id)
{
return id&-id;
} int update(int id, int _maxn)
{
while (id <= _maxn) {
qsum[id] += ;
id+=lowbit(id);
}
} int getsum(int id)
{
int res = ;
while (id) {
res += qsum[id];
id -= lowbit(id);
}
return res;
} int main()
{
int n;
sf("%d", &n); for (int i=; i<=n; ++i) {
sf("%d", &te[i].val);
te[i].id = i;
}
sort(te+, te++n);
int lst = te[].val;
int cnt = ;
for (int i=; i<=n; ++i) te[i].val = i;
sort(te+, te++n, cmp);
for (int i=; i<=n; ++i) {
update(te[i].val, n);
qans += i - - getsum(te[i].val-);
}
cout << qans << endl; return ;
}
逆序对__归并排序__树状数组 Inversions SGU - 180的更多相关文章
- BZOJ 3295 [CQOI2011]动态逆序对 (三维偏序CDQ+树状数组)
题目大意: 题面传送门 还是一道三维偏序题 每次操作都可以看成这样一个三元组 $<x,w,t>$ ,操作的位置,权值,修改时间 一开始的序列看成n次插入操作 我们先求出不删除时的逆序对总数 ...
- P1966 火柴排队——逆序对(归并,树状数组)
P1966 火柴排队 很好的逆序对板子题: 求的是(x1-x2)*(x1-x2)的最小值: x1*x1+x2*x2-2*x1*x2 让x1*x2最大即可: 可以证明将b,c数组排序后,一一对应的状态是 ...
- [luogu3157][bzoj3295][CQOI2011]动态逆序对【cdq分治+树状数组】
题目描述 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计整个序列的逆序 ...
- 用归并排序或树状数组求逆序对数量 poj2299
题目链接:https://vjudge.net/problem/POJ-2299 推荐讲解树状数组的博客:https://blog.csdn.net/int64ago/article/details/ ...
- Ultra-QuickSort(归并排序+离散化树状数组)
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 50517 Accepted: 18534 ...
- nyoj322 sort 归并排序,树状数组
Sort 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 You want to processe a sequence of n distinct integers b ...
- 康拓展开 & 逆康拓展开 知识总结(树状数组优化)
康拓展开 : 康拓展开,难道他是要飞翔吗?哈哈,当然不是了,康拓具体是哪位大叔,我也不清楚,重要的是 我们需要用到它后面的展开,提到展开,与数学相关的,肯定是一个式子或者一个数进行分解,即 展开. 到 ...
- poj 2299 Ultra-QuickSort(归并排序,树状数组,线段树)
Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...
- poj3067 Japan 树状数组求逆序对
题目链接:http://poj.org/problem?id=3067 题目就是让我们求连线后交点的个数 很容易想到将左端点从小到大排序,如果左端点相同则右端点从小到大排序 那么答案即为逆序对的个数 ...
随机推荐
- Java类型中ParameterizedType,GenericArrayType,TypeVariabl,WildcardType详解
(1). 和反射+泛型有关的接口类型 java.lang.reflect.Type:java语言中所有类型的公共父接口 java.lang.reflect.ParameterizedType java ...
- Android面试优雅地介绍自己的项目
本文转载:m.blog.csdn.net/iamsamzhang/article/details/51916584 先说句题外话,很多公司16年秋招的内推已经开始啦,我目前知道的在北京有网易.百度和微 ...
- python-time,datetime
1.time模块:time.time() # 返回当前时间的时间戳(1970纪元后经过的浮点秒数)time.localtime([ sec ]) # 接收时间戳(默认为当前时间),返回struct_t ...
- hdu 2065 "红色病毒"问题(快速幂求模)
n=1 --> ans = 2 = 1*2 = 2^0(2^0+1) n=2 --> ans = 6 = 2*3 = 2^1(2^1+1) n=3 --> ans = 20 ...
- 使用ES6的reduce函数,根据key去重
最近很着迷于ES6的函数,让代码变得更优雅.ES6里的reduce函数,平时用的不是特别多,真正用起来发现还是挺好用的. 想要实现的效果为: 原数组: let rawArr = [{id:'123'} ...
- linux系统管理 计划任务
一次性计划任务 命令: at 语法: at [-f 文件名] 时间 绝对计时方法 HH:MM yyyy-MM-dd 相对计时方法 now + n minutes now+n hours now + n ...
- learning ddr mode register MR0
- python logging模块,升级print调试到logging。
简介: 我们在写python程序的时候,很多时候都有bug,都是自己写的,自己造的孽,又的时候报错又是一堆,不知道是那部分出错了. 我这初学者水平,就是打print,看哪部分执行了,哪部分没执行,由此 ...
- Cracking The Coding Interview5.2
//Given a (decimal - e.g. 3.72) number that is passed in as a string, print the binary representatio ...
- tfs 2017 使用
安装完成之后,创建一个项目管理. 初始化代码库 然后下载代理 (服务器)并设置.下载代理需要FQ才可以下载成功. 想要支持 netcore2.0 必须在代理服务器上安装 vs2017 跟netcor ...