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

    题意:很水的用线段树求逆序对的题,时间复杂度在nlogn适合初学者,数据较小不用离散化,直接敲即可;

    #include<algorithm>
    #include<iostream>
    #include<map>
    #include<set>
    #include<vector>
    #include<stack>
    #include<queue>
    #include<cstring>
    #include<cstdio>
    #define N 10005
    #define INF 0x3f3f3f3f
    using namespace std;
    typedef struct node{
    	int x;int y;int date;
    }node;
    node a[5005*4];
    void built(int root,int first,int end){
    	if(first==end){
    		a[root].x=first;a[root].y=end;a[root].date=0;
    		return ;
    	}
    	int mid=(first+end)/2;
    	built(root*2,first,mid);
    	built(root*2+1,mid+1,end);
    	a[root].x=a[root*2].x;a[root].y=a[root*2+1].y;a[root].date=0;
    }
    void U(int root,int first,int end,int e){
    	if(first==end){
    		a[root].date=1;
    		return ;
    	}
    	int mid=(first+end)/2;
    	if(e<=mid)  U(root*2,first,mid,e);
    	else U(root*2+1,mid+1,end,e);
    	a[root].date=a[root*2].date+a[root*2+1].date;
    }
    int sum=0;
    void Q(int root,int first,int end,int l,int r){
    	if(first>=l&&end<=r){
    		sum+=a[root].date;
    		return ;
    	}
    	int mid=(first+end)/2;
    	if(l<=mid)  Q(root*2,first,mid,l,r);
    	if(r>mid)  Q(root*2+1,mid+1,end,l,r);
    }
    int b[5005];
    int c[5005];
    int main(){
    	int n;
    	while(scanf("%d",&n)==1){
    		for(int i=1;i<=n;i++){
    			scanf("%d",&b[i]);
    		}
    		int ans=0;
    		built(1,0,n-1);
    		for(int i=1;i<=n;i++){
    			sum=0;
    			if(b[i]==0){
    				U(1,0,n-1,0);
    			}
    			else{
    				Q(1,0,n-1,0,b[i]-1);
    				ans+=(b[i]-sum);
    				U(1,0,n-1,b[i]);
    			}
    		}
    		c[1]=ans;
    		//cout<<ans<<endl; 
    		for(int i=2;i<=n;i++){
    			c[i]=c[i-1]-b[i-1]-b[i-1]+n-1;
    		}
    		int p=INF;
    		for(int i=1;i<=n;i++){
    			p=min(p,c[i]);
    		}
    		printf("%d
    ",p);
    } 
      return 0;
    }


  • 相关阅读:
    margin问题
    IE6里面子集尺寸大的会把父亲撑大
    第一个元素<flout>写了,想在他的旁边加一个元素.IE6会出现缝隙. 不要用margin撑开,要用flout
    兼容性,float
    HTML5的兼容问题以及调用js文件的方法
    表单
    表格的编写,课程表
    SmartThreadPool
    C# 多线程的等待所有线程结束的一个问题
    DataTable保存与读取 stream
  • 原文地址:https://www.cnblogs.com/wang9897/p/7624403.html
Copyright © 2011-2022 走看看