逆序对__归并排序__树状数组 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 题目就是让我们求连线后交点的个数 很容易想到将左端点从小到大排序,如果左端点相同则右端点从小到大排序 那么答案即为逆序对的个数 ...
随机推荐
- Linux NTP服务配置 for Oracle RAC
安装Oracle 11g RAC时,我们需要配置ntp服务.在使用虚拟机的情况下对于时钟同步方式的配置有很多种方式,可以使用vmware自带的时钟同步功能,也可以直接将本地的一个节点用作时间服务器.本 ...
- ActiveMQ 配置jdbc主从
使用 jdbc 方式配置主从模式,持久化消息存放在数据库中. 在同一时刻,只有一个 master broker,master 接受客户端的连接,slave 不接受连接.当 master 因为关机而下线 ...
- jetty隐藏版本号教程
一.查看版本号 直接访问端口不像apache/tomcat/nginx会直接有版本号 但实际查看返回http头时还是带着版本号 二.隐藏版本号操作 编缉$JETTY_HOME/start.ini将je ...
- zabbix3.4.7版本饼图显示问题
问题描述 最近使用zabbix3.4.7版本,发现监控Linux的主机关联系统自带的Template OS Linux模版之后,磁盘空间饼图显示有问题,出现空白,如图所示 查看之后,确定为自带的Lem ...
- React中禁止chrome填充密码表单
当 input 的 type="password" 时,chrome浏览器会以 type="password" 为标识记住输入的用户名和密码, 如果chrome ...
- 技术宅学习Linux系统还是很有前途的
老实说,我之所以入了Linux的坑,纯粹只是为了追我现在的男朋友,也就是技术宅.如果不是为了追我男朋友的话,我估计我这辈子都不会去接触linux.好吧,今天写一写过往事情,也是为了怀念当初追男友的一些 ...
- Annotation方式配置AOP
package com.xk.spring.kp04_aop.aop.s02_annotation; public interface IStudentService { public void sa ...
- IO多路复用,select、poll、epoll 编程主要步骤
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- 继承,C++
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- :适配器模式:Adapter
#ifndef __ADAPTER_H__ #define __ADAPTER_H__ #include <iostream> using namespace std; class Duc ...