zoukankan      html  css  js  c++  java
  • 1523. K-inversions

    1523. K-inversions

    Time limit: 1.0 second Memory limit: 64 MB
    Consider a permutation a1, a2, …, an (all ai are different integers in range from 1 to n). Let us call k-inversion a sequence of numbers i1, i2, …, ik such that 1 ≤ i1 < i2 < … < ik ≤ n and ai1 > ai2 > … > aik. Your task is to evaluate the number of different k-inversions in a given permutation.

    Input

    The first line of the input contains two integers n and k (1 ≤ n ≤ 20000, 2 ≤ k ≤ 10). The second line is filled with n numbers ai.

    Output

    Output a single number — the number of k-inversions in a given permutation. The number must be taken modulo 109.

    Samples

    inputoutput
    3 2
    3 1 2
    
    2
    5 3
    5 4 3 2 1
    
    10
    
    Problem Author: Dmitry Gozman Problem Source: Dmitry Gozman Contest 1, Petrozavodsk training camp, January
    ************************************************************************************************
    树状数组(加快运算时间)
    ************************************************************************************************
     1 #include<iostream>
     2 #include<string>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<cmath>
     7 using namespace std;
     8 const int   N=20005;
     9 const  int MOD=1000000000;
    10 int a[N],dp[2][N];
    11 int c[N];
    12 int i,j,k,n;
    13 int sum;
    14 int lowbit(int x)//低位技术
    15 {
    16     return x&(-x);
    17 }
    18 void insert(int x,int v)//修改
    19  {
    20      for(;x<=n;x+=lowbit(x))
    21       c[x]=(c[x]+v)%MOD;
    22  }
    23  int query(int x)//查询
    24   {
    25       int suma=0;
    26       for(;x>0;x-=lowbit(x))
    27        suma=(suma+c[x])%MOD;
    28       return suma;
    29   }
    30   int main()
    31   {
    32       cin>>n>>k;
    33       for(i=1;i<=n;i++)
    34       {
    35           cin>>a[i];
    36           dp[1][i]=1;
    37       }
    38       int now=0;
    39       for(i=1;i<k;i++,now^=1)
    40        {
    41            memset(c,0,sizeof(c));
    42            sum=0;
    43            for(j=i;j<=n;j++)
    44             {
    45                 sum=(sum+dp[now^1][j])%MOD;//滚动数组
    46                 insert(a[j],dp[now^1][j]);//满足dp[i][j]=dp[i-1][i……n]
    47                 int  temp=query(a[j]);
    48                 if(sum<=temp)sum+=MOD;
    49                 dp[now][j]=sum-temp;
    50                 if(sum>MOD)sum-=MOD;
    51 
    52             }
    53        }
    54        sum=0;
    55        for(i=k;i<=n;i++)
    56         sum=(sum+dp[now^1][i])%MOD;
    57        if(sum>MOD)sum-=MOD;
    58        cout<<sum<<endl;
    59        return 0;
    60 
    61 
    62   }
    View Code
  • 相关阅读:
    Django框架之第三篇模板语法
    Django框架之第二篇
    Django框架第一篇基础
    【数学基础】【快速幂运算模板】
    【搜索】【广搜模板】
    【动态规划】【子序列模板】
    [置顶] 【ACM模板】——hello_chengdongni 随着姿势提升,不定期补充
    【搜索入门专题1】 hdu1242 J
    【搜索入门专题1】E
    【搜索入门专题1】hdu1253 【BFS】 F
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3247279.html
Copyright © 2011-2022 走看看