luogu-1908 逆序对 离散化+树状数组
题目链接:https://www.luogu.org/problem/show?pid=P1908
题意
简单的求逆序对
思路
用树状数组来做逆序对
对于过大的数字来讲,用离散化处理即可
比赛的时候没有想到离散化啊,笨
还有一点,如果有重复数字出现的话,可以考虑用一个vis数组存下对应元素出现的次数,计数时减掉就好
代码
#include <cstdio>
#include <cstring>
#include <algorithm>
#define lowbit(x) ((x)&(-x))
using namespace std;
const int maxn=40000;
struct Item{
int val, id;
bool operator < (const Item &a) const{
return val>a.val;
}
}item[maxn+5];
int n, vis[maxn], tree[maxn+5];
int sum(int x){
int ans=0;
for (int i=x; i>0; i-=lowbit(i))
ans+=tree[i];
return ans;
}
void add(int x, int val){
for (int i=x; i<=n; i+=lowbit(i))
tree[i]+=val;
}
int main(void){
scanf("%d", &n);
for (int i=0; i<n; i++){
scanf("%d", &item[i].val);
item[i].id=i+1;
}
sort(item, item+n);
int ans=0;
for (int i=0; i<n; i++){
ans+=sum(item[i].id);
add(item[i].id, 1);
}printf("%d\n", ans);
return 0;
}
| Time | Memory |
|---|---|
| 112ms | 2273KB |
luogu-1908 逆序对 离散化+树状数组的更多相关文章
- AcWing 107. 超快速排序(归并排序 + 逆序对 or 树状数组)
在这个问题中,您必须分析特定的排序算法----超快速排序. 该算法通过交换两个相邻的序列元素来处理n个不同整数的序列,直到序列按升序排序. 对于输入序列9 1 0 5 4,超快速排序生成输出0 1 4 ...
- 洛谷P1908 逆序对 (树状数组+离散化)
模板题,树状数组加上离散化求逆序对. 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 ...
- 【Luogu】P3157动态逆序对(树状数组套主席树)
题目链接 md第一道在NOILinux 下用vim做的紫题.由于我对这个操作系统不是很熟悉,似乎有什么地方搞错了,md调死.(我还打了两遍代码,调了两个小时) 但是这道题并不难,就是树状数组套上主席树 ...
- Day2:T4求逆序对(树状数组+归并排序)
T4: 求逆序对 A[I]为前缀和 推导 (A[J]-A[I])/(J-I)>=M A[j]-A[I]>=M(J-I) A[J]-M*J>=A[I]-M*I 设B[]=A[]-M*( ...
- P1908 逆序对-(树状数组)
https://www.luogu.org/problem/P1908 比较喜欢线段树,懒得用树状数组(只会套模板,位运算的精髓没有领悟到),一直没有记录树状数组代码,又得捡回来,趁这道题记录一下模板 ...
- 剑指 Offer 51. 数组中的逆序对 + 归并排序 + 树状数组
剑指 Offer 51. 数组中的逆序对 Offer_51 题目描述 方法一:暴力法(双层循环,超时) package com.walegarrett.offer; /** * @Author Wal ...
- 【BZOJ 3295】动态逆序对 - 分块+树状数组
题目描述 给定一个1~n的序列,然后m次删除元素,每次删除之前询问逆序对的个数. 分析:分块+树状数组 (PS:本题的CDQ分治解法见下一篇) 首先将序列分成T块,每一块开一个树状数组,并且先把最初的 ...
- Bzoj 3295: [Cqoi2011]动态逆序对 分块,树状数组,逆序对
3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2886 Solved: 924[Submit][Stat ...
- bzoj1831 逆序对 (dp+树状数组)
注意到,所有的-1应该是一个不降的序列,否则不会更优那就先求出来不是-1的的逆序对个数,然后设f[i][j]表示第i个-1放成j的前i个-1带来的最小逆序对数量这个可以树状数组来求 #include& ...
随机推荐
- BZOJ 2844 高斯消元 线性基
思路: //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm> using ...
- POJ 3261 后缀数组+二分
思路: 论文题- 二分+对后缀分组 这块一开始不用基数排序 会更快的(其实区别不大) //By SiriusRen #include <cstdio> #include <cstri ...
- Python学习之基本概念
1.Python是一种解释型语言.Python解释器通过“一次执行一条语句”的方式执行程序的. 2.Python用空白来组织程序,不像R等用大括号. 3.# 是Python的注释符号. 4.变量是按引 ...
- Pharmaceutical的同学们都看过来,关于补码运算的复习相关内容
虽然是全英文的课程,这次总结内容不用英文了. 一般在计算机原理中,对两个操作数进行运算会使用C作为进位的标志位,而V作为溢出的标志位. 一般我们学完计算机原理,都知道正数的原码反码补码都一样,而问题都 ...
- MyBatis数据持久化(六)resultMap使用
resultMap是MyBatis最强大也是最重要的特性,使用它我们可以將查询结果或者sql输入条件和一个HashMap的key进行映射,大大简化了sql查询条件的输入,而且使得数据的查询变得非常灵活 ...
- 使用xshell连接本地虚拟机中的Linux问题
xshell 连接虚拟机中Linux报错: Could not connect to '192.168.8.120' (port 22):Connection failed. 原因:虚拟机中Linux ...
- php报错权限设置
<?php //禁用错误报告 error_reporting(0); //报告运行时错误 error_reporting(E_ERROR | E_WARNING | E_PARSE); //报告 ...
- No mapping found for HTTP request with URI [/spring_liu/hello.do] in DispatcherServlet with name 'SpringMVC'
控制台一直报No mapping found for HTTP request with URI [/spring_liu/hello.do] in DispatcherServlet with na ...
- Site Isolation Design Document
This design document covers technical information about how Site Isolation is built. For a general ...
- SP10628 COT - Count on a tree 主席树
Code: #include<cstdio> #include<cstring> #include<algorithm> #include<string> ...