• 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
  • 相关阅读:
    curl crt
    test with nmap
    C#查询XML解决“需要命名空间管理器”问题
    Dapper实用教程
    javascript 计算两个日期的差值
    Glib学习笔记(二)
    安装osquery笔记
    Golang多线程简单斗地主
    PHP扩展开发之Zephir
    zabbix 安装记录
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3247279.html
Copyright © 2020-2023  润新知