HDU - 1394 Minimum Inversion Number(线段树求逆序数---点修改)
题意:给定一个序列,求分别将前m个数移到序列最后所得到的序列中,最小的逆序数。
分析:m范围为1~n,可得n个序列,求n个序列中最小的逆序数。
1、将序列从头到尾扫一遍,用query求每个数字之前有多少个大于该数字的数,方法如下。
(1)将已经扫过的数字所对应的位置标记,通过query求该数字之后有多少个数被标记过
(2)该数字之后所有被标记的数字,都是在该数字之前出现过的(i<j),而这些数字又大于该数字(ai>aj),因此该数字之后所有的标记和就是该数字之前比该数字大的数的个数。
2、sum为初始序列的逆序和,从前往后依次将每个数字移到序列最后。
假设当前把a[i]移到序列最后,则
后一个序列B的逆序和=前一个序列A的逆序和 - a[i] + (n - 1 - a[i])。
eg:假设前一个序列A为3 6 9 0 8 5 7 4 2 1,则a[i]为3。
a[i]是序列A的第一个数,a[i]后面的数中有a[i]个数小于a[i](分别为0,2,1),因此把a[i]移到最后,会减少a[i]个逆序数。
而a[i]后面的数中有 (n - 1 - a[i])个数大于a[i],因此把a[i]移到最后,会增加 (n - 1 - a[i])个逆序数。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 5000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int a[MAXN];
int cnt[MAXN << 2];
void update(int k, int id, int L, int R){
if(L == R){
++cnt[id];
}
else{
int mid = L + (R - L) / 2;
if(k <= mid) update(k, id << 1, L, mid);
else update(k, id << 1 | 1, mid + 1, R);
cnt[id] = cnt[id << 1] + cnt[id << 1 | 1];
}
}
int query(int l, int r, int id, int L, int R){
if(l <= L && R <= r){
return cnt[id];
}
int mid = L + (R - L) / 2;
int ans = 0;
if(l <= mid) ans += query(l, r, id << 1, L, mid);
if(r > mid) ans += query(l, r, id << 1 | 1, mid + 1, R);
return ans;
}
int main(){
int n;
while(scanf("%d", &n) == 1){
memset(cnt, 0, sizeof cnt);
int sum = 0;
for(int i = 0; i < n; ++i){
scanf("%d", &a[i]);
sum += query(a[i] + 1, n, 1, 1, n);
update(a[i] + 1, 1, 1, n);
}
int ans = sum;
for(int i = 0; i < n; ++i){
sum = sum - a[i] + (n - 1 - a[i]);
ans = min(ans, sum);
}
printf("%d\n", ans);
}
return 0;
}
HDU - 1394 Minimum Inversion Number(线段树求逆序数---点修改)的更多相关文章
- [HDU] 1394 Minimum Inversion Number [线段树求逆序数]
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
- hdu - 1394 Minimum Inversion Number(线段树水题)
http://acm.hdu.edu.cn/showproblem.php?pid=1394 很基础的线段树. 先查询在更新,如果后面的数比前面的数小肯定会查询到前面已经更新过的值,这时候返回的sum ...
- HDU 1394 Minimum Inversion Number 线段树
题目: http://acm.hdu.edu.cn/showproblem.php?pid=1394 没看到多组输入,WA了一万次...... 其实很简单,有人暴力过得,我感觉归并排序.二叉排序树求逆 ...
- HDU 1394 Minimum Inversion Number(线段树 或 树状数组)
题目大意:给出从 0 到 n-1 的整数序列,A0,A1,A2...An-1.可将该序列的前m( 0 <= m < n )个数移到后面去,组成其他的序列,例如当 m=2 时,得到序列 A2 ...
- HDU_1394_Minimum Inversion Number_线段树求逆序数
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- hdu 13394 Minimum Inversion Number 线段树
题意: 首先给你一个长度为n的序列v,你需要首先找出来逆序对(i<j && v[i]>v[j]) 然后把这个序列的最后一个元素放在第一个位置上,其他元素都向后移动一位. 一 ...
- hdu 1394 Minimum Inversion Number (树状数组求逆序对)
The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that ...
- HDU 1394 Minimum Inversion Number (树状数组)
题目链接 Problem Description The inversion number of a given number sequence a1, a2, ..., an is the numb ...
- HDU 1394 Minimum Inversion Number(树状数组/归并排序实现
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
随机推荐
- 第1节 Scala基础语法:14、15、list集合练习
package cn.itcast.collect /** *作业题 */object ListTest { def main(args: Array[String]): Unit = { //创建一 ...
- 使用注解的形式搭建一个springMVC框架
1.创建SpringMVC的配置文件springmvc-servlet <?xml version="1.0" encoding="UTF-8"?> ...
- 设计模式课程 设计模式精讲 2-3 UML类图讲解自上而下
1 内容简析(易错加粗) 2 uml类图展示 1 内容简析(易错加粗) 1.1 设计模式中的对象关系 依赖关系 虚线箭头 组合关系 实心菱形 聚合关系 空心菱形(想象成盘子,盛东西,盘子的多,另一方少 ...
- Pytorch model saving and loading 模型保存和读取
It is really useful to save and reload the model and its parameters during or after training in deep ...
- vue-cli 初始化项目时开发环境中的跨域问题
最近刚刚完成自己的毕业设计(基于Vue的信息资讯展示与管理平台),于是想整理一下过程遇到的一些问题. 项目基于Vue开发,使用 Vue-cli 初始化项目文件目录时默认占用8080端口,而我又想使用 ...
- java并发:原子类之AtomicLong
原子类之AtomicLong java线程中的操作,需要满足原子性.可见性等原则,比如i++这样的操作不具备原子性, A线程读取了i,另一个线程执行i++,A线程再执行i++就会引发线程安全问题 推荐 ...
- Linux centosVMware vim 编辑模式、vim命令模式、vim实践
一.编辑模式.命令模式 在一般模式下输入:或/可进入命令模式.在该模式下可进行走索某个字符或字符串,也可保存.替换.退出.显示行号等. /word:在光标之后查找一个字符串word,按n向后继续搜索 ...
- JDBC--处理事务
1.事务是指一组逻辑操作单元,使数据从一种状态转换到另一种状态. 2.事务的四个属性: --1)原子性:事务是一个不可分割的工作单位,事务中的操作要么都发生要么都不发生: --2)一致性:事务必须是数 ...
- @Controller 和 @RestController 的区别
@Controller和@RestController的区别? 官方文档:@RestController is a stereotype annotation that combines @Respo ...
- mutiset的简单介绍转载
原文链接:https://blog.csdn.net/sodacoco/article/details/84798621 c++语言中,multiset是<set>库中一个非 ...