逆序对__归并排序__树状数组 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 题目就是让我们求连线后交点的个数 很容易想到将左端点从小到大排序,如果左端点相同则右端点从小到大排序 那么答案即为逆序对的个数 ...
随机推荐
- Segment set(线段并查集)
Segment set Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total S ...
- php-fpm占用cpu和内存过高100% 解决办法
参考网站: https://www.fujieace.com/php/php-fpm.html https://www.fujieace.com/php/pm-max_children-2.html ...
- springmvc 自定义view支持json和jsonp格式数据返回
1.如果controlloer上用@ResponseBody注解,则用<mvc:message-converter>里面配置的json解析器进行解析 <mvc:annotation- ...
- JDK自带的keytool证书工具详解
一.生成证书 keytool -genkey -alias tomcat -keyalg RSA -keystore D:/tomcat.keystore -keypass 123456 -store ...
- Java Web(十) 分页功能
分页 分页的使用非常普遍,现在一步步的把分页功能实现出来,先看看已经写好的效果: 该页面的所有数据都存放在一个javaBean对象(PageBean)里,每次访问该页面时,Serlvet就会把page ...
- js 敏感词过滤
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...
- Cracking The Coding Interview 3.6
// Write a program to sort a stack in ascending order. You should not make any assumptions about how ...
- FPGA中IBERT核的应用(转)
https://wenku.baidu.com/view/50a12d8b9ec3d5bbfd0a74f7.html (必看) 摘要 IBERT即集成式比特误码率测试仪,是Xilinx专门用于具 ...
- 玩转X-CTR100 l STM32F4 l 定时器时间测量
我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ] 本文介绍X-CTR100控制器 使用处理器内部硬件定 ...
- shell怎么判断两个文件内容是否相同
#cat diff_two_file#/bin/sbinfile1=/mnt/mmc/test/aafile2=/mnt/mmc/test/bbdiff $file1 $file2 > /dev ...