博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 1394 Minimum Inversion Number(线段树)
阅读量:7084 次
发布时间:2019-06-28

本文共 2194 字,大约阅读时间需要 7 分钟。

 

线段树求逆序数,先求出初始数组的逆序数(n logn),然后o(1)推出其他逆序数。因为输入为0--n-1,所以建树从0开始。

code:

#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <
string>
#include <iostream>
#include <sstream>
#include <
set>
#include <queue>
#include <stack>
#include <fstream>
#include <iomanip>
#include <bitset>
#include <list>
#include <ctime>
using 
namespace std ;
#define SET(arr, what)  memset(arr, what, sizeof(arr))
#define FF(i, a)        for(i=0; i<a; i++)
#define SD(a)           scanf("%d", &a)
#define SSD(a, b)       scanf("%d%d", &a, &b)
#define SF(a)           scanf("%lf", &a)
#define SS(a)           scanf("%s", a)
#define SLD(a)          scanf("%lld", &a)
#define PF(a)           printf("%d\n", a)
#define PPF(a, b)       printf("%d %d\n", a, b)
#define SZ(arr)         (int)a.size()
#define SWAP(a,b)       a=a xor b;b= a xor b;a=a xor b;
#define read            freopen("in.txt", "r", stdin)
#define write            freopen("out.txt", "w", stdout)
#define MAX             1<<30
#define ESP             1e-5
#define lson            l, m, rt<<1|1
#define rson            m+1, r, (rt<<1)+2
template<
class T> inline T sqr(T a){
return a*a;}
template<
class T> inline 
void AMin(T &a,T b){
if(a==-
1||a>b)a=b;}
template<
class T> inline 
void AMax(T &a,T b){
if(a<b)a=b;}
template<
class T> inline T Min(T a,T b){
return a>b?b:a;}
template<
class T> inline T Max(T a,T b){
return a>b?a:b;}
const 
int maxn = 
5010 ;
int sum[maxn<<
2] ;
int data[maxn] ;
void build(
int l, 
int r, 
int rt){
    sum[rt] = 
0 ;
    
if(l==r)    
return ;
    
int m = (l + r) >> 
1 ;
    build(lson) ;
    build(rson) ;
}
void update(
int x, 
int l, 
int r, 
int rt){
    sum[rt] ++ ;
    
if(l==r)    
return ;
    
int m = (l + r) >> 
1 ;
    
if(x<=m) update(x, lson) ;
    
else  update(x, rson) ;
}
int query(
int L, 
int R, 
int l, 
int r, 
int rt){
    
if(L<=l&&r<=R)  
return sum[rt] ;
    
int m = (l + r) >> 
1 ;
    
int ret = 
0 ;
    
if(L<=m) ret += query(L, R, lson) ;
    
if(R>m)  ret += query(L, R, rson) ;
    
return ret ;
}
int main(){
    
int n, i, j, ini, ans ;
    
while(~SD(n)){
        build(
0, n-
1
0) ;
        ini = 
0 ;
        FF(i, n){
            SD(data[i]) ;
            ini += query(data[i], n, 
0, n-
1
0) ;
            update(data[i], 
0, n-
1
0) ;
        }
        ans = ini ;
        FF(i, n){
            ini += n - data[i] - data[i] - 
1 ;
            ans = Min(ini, ans) ;
        }
        PF(ans) ;
    }
    
return 
0 ;
}

转载于:https://www.cnblogs.com/xiaolongchase/archive/2012/08/20/2647069.html

你可能感兴趣的文章
Samba用户管理机制
查看>>
解决securecrt连接centos使用VIM编辑中文时乱码
查看>>
Windows server 2008R2域的安装和访问
查看>>
spring boot简单实现rest服务
查看>>
linux内核编译安装
查看>>
在CentOS/RHEL/Scientific Linux 6 & 7 上安装Telnet
查看>>
linux中hash命令:显示、添加或清除哈希表
查看>>
理解MySQL复制(Replication)——经典文献
查看>>
安卓手机recovery下刷补丁提示:“can't open /sdcard/update.zip(bad)”
查看>>
shared_ptr源码分析
查看>>
AOJ 2164 Revenge of the Round Table 题解《挑战程序设计竞赛》
查看>>
What's new in JSF 2.2
查看>>
eclipse 一直buildingWorkSpace 卡死解决
查看>>
Hyper-V导入Ubuntu虚拟机后发现网卡eth0丢失的解决办法
查看>>
Web server和php结合的三种模式
查看>>
Linux中的LDAP认证
查看>>
数组竟然可以这样定义
查看>>
Hyperledger Fabric 链码(智能合约)基本操作
查看>>
再学 GDI+[77]: 区域(6) - GetRegionScans - 获取区域中的所有矩形
查看>>
学习 TList 类的实现[7]
查看>>