zoukankan      html  css  js  c++  java
  • HDU1394

    题目链接:https://vjudge.net/problem/HDU-1394

    题目分析:先用线段树求出第一个数组的逆序数,其他的数组的逆序数可以用公式直接求出

         用线段树求出数组逆序数的思路:把数组a[]上的元素逐个插入线段树,以元素的大小作为插入位置,则在其插入位置的右方的叶子数即为数组中的这个元素之前的比这个元素本身大的数。一开始初始化逆序数为0,然后每次插入都把元素右方的叶子数加上去,最后就可以得到数组的逆序数了。

    代码:

     1 #include <cstdio>
     2 #include <algorithm>
     3 using namespace std;
     4 
     5 const int maxn=5000+5;
     6 int sum[maxn<<2];
     7 
     8 void PushUp(int rt){
     9     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
    10 }
    11 void build(int l,int r,int rt){
    12     if(l==r){
    13         sum[rt]=0;
    14         return;
    15     }
    16     int m=(l+r)>>1;
    17     build(l,m,rt<<1);
    18     build(m+1,r,rt<<1|1);
    19     PushUp(rt);
    20 }
    21 void update(int p,int l,int r,int rt){
    22     if(l==p&&r==p){
    23         sum[rt]=1;
    24         return;
    25     }
    26     int m=(l+r)>>1;
    27     if(p<=m)    update(p,l,m,rt<<1);
    28     else    update(p,m+1,r,rt<<1|1);
    29     PushUp(rt);
    30 }
    31 int query(int L,int R,int l,int r,int rt){
    32     if(l>L&&R>=r){
    33         return sum[rt];
    34     }
    35     int m=(l+r)>>1;
    36     int ret=0;
    37     if(L<m)   ret+=query(L,R,l,m,rt<<1);
    38     if(R>m)    ret+=query(L,R,m+1,r,rt<<1|1);
    39     return ret;
    40 }
    41 int main(){
    42     int N;
    43     int a[maxn];
    44 
    45     while(scanf("%d",&N)==1){
    46         for(int i=0;i<N;i++)
    47             scanf("%d",&a[i]);
    48         build(0,N-1,1);
    49         int ans=0;
    50         for(int i=0;i<N;i++){
    51             update(a[i],0,N-1,1);
    52             ans+=query(a[i],N-1,0,N-1,1);
    53         }
    54         int temp=ans;
    55         for(int i=0;i<N;i++){
    56             temp=temp-a[i]+(N-1-a[i]);
    57             ans=min(ans,temp);
    58         }
    59         printf("%d
    ",ans);
    60     }
    61     return 0;
    62 }
    View Code
    “这些年我一直提醒自己一件事情,千万不要自己感动自己。大部分人看似的努力,不过是愚蠢导致的。什么熬夜看书到天亮,连续几天只睡几小时,多久没放假了,如果这些东西也值得夸耀,那么富士康流水线上任何一个人都比你努力多了。人难免天生有自怜的情绪,唯有时刻保持清醒,才能看清真正的价值在哪里。”
  • 相关阅读:
    struts2文件上传报错
    简述算法和程序的区别并举例说明
    JAVA中TreeMap集合筛选字母及每一个字符出现的次数
    Myeclipse2014破解步骤
    修改ubuntu的终端提示符
    gcc 引用math.h头文件,编译出现undefined reference to `pow‘等错误时,需要加参数lm.
    几篇文章
    gdb调试gcc出现:Missing separate debuginfos, use: debuginfoinstall glibcx.i686
    【达内C++学习培训学习笔记系列】C语言之三循环语句和数组
    code::block之spell checker配置
  • 原文地址:https://www.cnblogs.com/Blogggggg/p/6985557.html
Copyright © 2011-2022 走看看