HDU 1394 Minimum Inversion Number tree array & amp; line segment tree

Source: Internet
Author: User
Tags cmath

The question gives you a series, and each time the last number is raised to the beginning, until the original first number reaches the last one, each operation will generate a new series, this sequence has a value in the descending order and asks the minimum value in the descending order.


The number of reverse orders is best thought of as a tree array, which is very fast. Pay attention to the impact of bringing the last number up on the number of reverse orders,



#include
 
  #include
  
   #include
   
    #include#include
    
     #include
     
      #include
      
       #include
       
        #include
        #include
         
          #include
          
           #include
           
            #include
            
             #include
             
              #define ll long long#define LL __int64#define eps 1e-8#define inf 0xfffffff//const LL INF = 1LL<<61;using namespace std;//vector
              
                > G;//typedef pair
               
                 P;//vector
                
                  > ::iterator iter;////map
                 
                  mp;//map
                  
                   ::iterator p;int n;int c[10000 + 5];int num[10000 + 5];void init() {memset(c,0,sizeof(c));memset(num,0,sizeof(num));}int lowbit(int x) {return x&(-x);}void add(int i,int val) {while(i <= n) {c[i] += val;i += lowbit(i);}}int get_sum(int i) {int sum = 0;while(i > 0) {sum += c[i];i -= lowbit(i);}return sum;}int main() {while(scanf("%d",&n) == 1) {init();int ans = 0;for(int i=1;i<=n;i++) {scanf("%d",&num[i]);num[i]++;add(num[i],1);ans += (i - get_sum(num[i]));}int minn = ans;for(int i=n;i>1;i--) {ans = ans + num[i] + num[i] - n - 1;minn = min(ans,minn);}printf("%d\n",minn);}return 0;}
                  
                 
                
               
              
             
            
           
          
         
       
      
     
    
   
  
 

Line Segment tree:



#include
 
  #include
  
   #include
   
    #include#include
    
     #include
     
      #include
      
       #include
       
        #include
        #include
         
          #include
          
           #include
           
            #include
            
             #include
             
              #define ll long long#define LL __int64#define eps 1e-8#define inf 0xfffffff//const LL INF = 1LL<<61;using namespace std;//vector
              
                > G;//typedef pair
               
                 P;//vector
                
                  > ::iterator iter;////map
                 
                  mp;//map
                  
                   ::iterator p;const int N = 10000 + 5;int num[N];typedef struct Node { int a; int l,r;};Node tree[N * 4];void init() { memset(tree,0,sizeof(tree)); memset(num,0,sizeof(num));}void cal(int id) { tree[id].a = min(tree[id<<1].a,tree[id<<1|1].a);}void build(int l,int r,int id) { tree[id].l = l; tree[id].r = r; tree[id].a = 0; if(l == r) return ; int mid = (l + r)/2; build(l,mid,id<<1); build(mid+1,r,id<<1|1);}void updata(int w,int id) { if(tree[id]. l == w && tree[id].r == w) { tree[id].a = 1;return; } int mid = (tree[id].l + tree[id].r)/2; if(w <= mid) updata(w,id<<1); else updata(w,id<<1|1); tree[id].a = tree[id<<1].a + tree[id<<1|1].a;}int query(int l,int r,int id) { if(l <= tree[id].l && r >= tree[id].r)return tree[id].a; int mid = (tree[id].l + tree[id].r)/2; int ans1 = 0,ans2 = 0; if(l <= mid) ans1 = query(l,r,id<<1); if(r > mid) ans2 = query(l,r,id<<1|1); return ans1 + ans2;}int main() { int n; while(scanf("%d",&n) == 1) { init(); build(1,n,1); int ans = 0; for(int i=0;i
                   
                    

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.