zoukankan      html  css  js  c++  java
  • hdoj 1394 Minimum Inversion Number

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394

    解题思路:由于数据较小,可直接暴力求逆序数,也可用归并排序求逆序数。本文采用的解法为线段树(单点更新)

      1 /**************************************************************************
      2 user_id: SCNU20102200088
      3 problem_id: hdoj 1394
      4 problem_name: Minimum Inversion Number
      5 **************************************************************************/
      6 
      7 #include <algorithm>
      8 #include <iostream>
      9 #include <iterator>
     10 #include <iomanip>
     11 #include <sstream>
     12 #include <fstream>
     13 #include <cstring>
     14 #include <cstdlib>
     15 #include <climits>
     16 #include <bitset>
     17 #include <string>
     18 #include <vector>
     19 #include <cstdio>
     20 #include <cctype>
     21 #include <ctime>
     22 #include <cmath>
     23 #include <queue>
     24 #include <stack>
     25 #include <list>
     26 #include <set>
     27 #include <map>
     28 using namespace std;
     29 
     30 //线段树
     31 #define lson l,m,rt<<1
     32 #define rson m+1,r,rt<<1|1
     33 
     34 //手工扩展栈
     35 #pragma comment(linker,"/STACK:102400000,102400000")
     36 
     37 const double EPS=1e-9;
     38 const double PI=acos(-1.0);
     39 const double E=2.7182818284590452353602874713526;  //自然对数底数
     40 const double R=0.5772156649015328606065120900824;  //欧拉常数:(1+1/2+...+1/n)-ln(n)
     41 
     42 const int x4[]={-1,0,1,0};
     43 const int y4[]={0,1,0,-1};
     44 const int x8[]={-1,-1,0,1,1,1,0,-1};
     45 const int y8[]={0,1,1,1,0,-1,-1,-1};
     46 
     47 typedef long long LL;
     48 
     49 typedef int T;
     50 T max(T a,T b){ return a>b? a:b; }
     51 T min(T a,T b){ return a<b? a:b; }
     52 T gcd(T a,T b){ return b==0? a:gcd(b,a%b); }
     53 T lcm(T a,T b){ return a/gcd(a,b)*b; }
     54 
     55 ///////////////////////////////////////////////////////////////////////////
     56 //Add Code:
     57 const int maxn=5005;
     58 int sum[maxn<<2];
     59 
     60 void pushup(int rt){
     61     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
     62 }
     63 
     64 void build(int l,int r,int rt){
     65     if(l==r){
     66         sum[rt]=0;
     67         return ;
     68     }
     69     int m=(l+r)>>1;
     70     build(lson);
     71     build(rson);
     72     pushup(rt);
     73 }
     74 
     75 void update(int p,int l,int r,int rt){
     76     if(l==r){
     77         sum[rt]=1;
     78         return ;
     79     }
     80     int m=(l+r)>>1;
     81     if(p<=m) update(p,lson);
     82     else update(p,rson);
     83     pushup(rt);
     84 }
     85 
     86 int query(int L,int R,int l,int r,int rt){
     87     if(L<=l && R>=r) return sum[rt];
     88     int m=(l+r)>>1,ret=0;
     89     if(L<=m) ret+=query(L,R,lson);
     90     if(R>m) ret+=query(L,R,rson);
     91     return ret;
     92 }
     93 ///////////////////////////////////////////////////////////////////////////
     94 
     95 int main(){
     96     std::ios::sync_with_stdio(false);
     97     //freopen("in.txt","r",stdin);
     98     //freopen("out.txt","w",stdout);
     99     ///////////////////////////////////////////////////////////////////////
    100     //Add Code:
    101     int n,i,a[maxn];
    102     while(scanf("%d",&n)!=EOF){
    103         build(0,n-1,1);
    104         int cnt=0;
    105         for(i=0;i<n;i++){
    106             scanf("%d",&a[i]);
    107             cnt+=query(a[i],n-1,0,n-1,1);
    108             update(a[i],0,n-1,1);
    109         }
    110         int ans=cnt;
    111         for(i=0;i<n;i++){
    112             cnt+=n-a[i]-a[i]-1;
    113             ans=min(ans,cnt);
    114         }
    115         printf("%d
    ",ans);
    116     }
    117     ///////////////////////////////////////////////////////////////////////
    118     return 0;
    119 }
    120 
    121 /**************************************************************************
    122 Testcase:
    123 Input:
    124 10
    125 1 3 6 9 0 8 5 7 4 2
    126 Output:
    127 16
    128 **************************************************************************/
  • 相关阅读:
    SQL中 SET QUOTED_IDENTIFIER OFF语句的作用
    System.Data.SQLite
    关于SQL Server的SET ANSI_NULLS的问题
    压缩解压缩下载多个文件
    提示源文件路径不存在
    Jquery颜色选择器
    MVC中使用DropDownListFor
    jquery ajax 跨域请求
    纪念帖:博客开张,发帖留念!
    解压缩:解压之后中文文件名乱码
  • 原文地址:https://www.cnblogs.com/linqiuwei/p/3337224.html
Copyright © 2011-2022 走看看