逆序对__归并排序__树状数组 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 题目就是让我们求连线后交点的个数 很容易想到将左端点从小到大排序,如果左端点相同则右端点从小到大排序 那么答案即为逆序对的个数 ...
随机推荐
- redis的文件事件
redis的文件事件:即与io相关的事件. /* File event structure */ typedef struct aeFileEvent { int mask; /* one of AE ...
- Ubuntu中的在文件中查找和替换命令
分类: 9.Linux技巧2009-09-29 13:40 1429人阅读 评论(0) 收藏 举报 ubuntujdbc 1.查找 find /home/guo/bin -name /*.txt | ...
- mysql 语句根据身份证查询年龄,地址,性别
select case left(idcard,2) when '11' then '北京市'when '12' then '天津市'when '13' then '河北省'when '14' th ...
- VS Code 常用快捷键
VS Code 常用快捷键 1.注释: 单行注释:ctrl+/, 注释后再按取消 取消单行注释:alt+shift+A 注释后再按取消 2.移动行 向上移动一行:alt+up 向下移动一行:alt+d ...
- liunx 随笔集
Linux 安装时 Customize Now(自定义选包)选包如下 base system -> base , compatibility libraries,debugging Tool ...
- C++各种类继承关系的内存布局
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- java接口和抽象类的区别和作用(功能、用途、好处)
Java接口: 总结了4点关于JAVA中接口存在的意义: 1.重要性:在Java语言中, abstract class 和interface 是支持抽象类定义的两种机制.正是由于这两种机制的存在,才赋 ...
- Cracking The Coding Interview 1.3
//原文: // // Design an algorithm and write code to remove the duplicate characters in a string withou ...
- linux 删除日志
https://jingyan.baidu.com/album/c1a3101e73129ade656deb9d.html?picindex=2 里面的 ls -s 可以看到目录 https://zh ...
- Centos7部署kubernetes Proxy(七)
1.配置kube-proxy使用LVS(三个节点都装上去) [root@linux-node1 ssl]# yum install -y ipvsadm ipset conntrack [root@l ...