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 andai1 > 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

input output
  1. 3 2
  2. 3 1 2
  1. 2
  1. 5 3
  2. 5 4 3 2 1
  1. 10

分析:类似于逆序对,求k次递减序列的个数,树状数组+dp更新,注意取模;

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <climits>
  7. #include <cstring>
  8. #include <string>
  9. #include <set>
  10. #include <map>
  11. #include <queue>
  12. #include <stack>
  13. #include <vector>
  14. #include <list>
  15. #define rep(i,m,n) for(i=m;i<=n;i++)
  16. #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
  17. #define mod 1000000000
  18. #define inf 0x3f3f3f3f
  19. #define vi vector<int>
  20. #define pb push_back
  21. #define mp make_pair
  22. #define fi first
  23. #define se second
  24. #define ll long long
  25. #define pi acos(-1.0)
  26. #define pii pair<int,int>
  27. #define Lson L, mid, rt<<1
  28. #define Rson mid+1, R, rt<<1|1
  29. const int maxn=2e4+;
  30. const int dis[][]={{,},{-,},{,-},{,}};
  31. using namespace std;
  32. ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
  33. ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
  34. int n,m,k,t,a[maxn],dp[][maxn],p[maxn];
  35. ll ans;
  36. void add(int x,int y)
  37. {
  38. for(int i=x;i<=n;i+=(i&(-i)))
  39. a[i]+=y,a[i]%=mod;
  40. }
  41.  
  42. int get(int x)
  43. {
  44. int res=;
  45. for(int i=x;i;i-=(i&(-i)))
  46. res+=a[i],res%=mod;
  47. return res;
  48. }
  49. int main()
  50. {
  51. int i,j;
  52. scanf("%d%d",&n,&k);
  53. rep(i,,n)scanf("%d",&p[i]),dp[][i]=;
  54. rep(i,,k)
  55. {
  56. memset(a,,sizeof a);
  57. rep(j,,n)
  58. {
  59. dp[i][j]=(get(n)-get(p[j])+mod)%mod;
  60. add(p[j],dp[i-][j]);
  61. }
  62. }
  63. rep(i,,n)ans=(ans+dp[k][i])%mod;
  64. printf("%lld\n",ans);
  65. //system("Pause");
  66. return ;
  67. }

ural1523 K-inversions的更多相关文章

  1. django模型操作

    Django-Model操作数据库(增删改查.连表结构) 一.数据库操作 1.创建model表        

  2. Inversions After Shuffle

    Inversions After Shuffle time limit per test 1 second memory limit per test 256 megabytes input stan ...

  3. 《算法导论》Problem 2-4 Inversions

    在Merge Sort的基础上改改就好了. public class Inversions { public static int inversions(int [] A,int p, int r) ...

  4. [Swift]LeetCode775. 全局倒置与局部倒置 | Global and Local Inversions

    We have some permutation Aof [0, 1, ..., N - 1], where N is the length of A. The number of (global) ...

  5. HDU 6318 - Swaps and Inversions - [离散化+树状数组求逆序数][杭电2018多校赛2]

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=6318 Problem Description Long long ago, there was an ...

  6. HDU 6318 Swaps and Inversions 思路很巧妙!!!(转换为树状数组或者归并求解逆序数)

    Swaps and Inversions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  7. PAT 1009. Triple Inversions (35) 数状数组

    Given a list of N integers A1, A2, A3,...AN, there's a famous problem to count the number of inversi ...

  8. HDU 多校对抗赛第二场 1010 Swaps and Inversions

    Swaps and Inversions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  9. Codeforces 513G1 513G2 Inversions problem [概率dp]

    转自九野:http://blog.csdn.net/qq574857122/article/details/43643135 题目链接:点击打开链接 题意: 给定n ,k 下面n个数表示有一个n的排列 ...

  10. Coursera Algorithms week3 归并排序 练习测验: Counting inversions

    题目原文: An inversion in an array a[] is a pair of entries a[i] and a[j] such that i<j but a[i]>a ...

随机推荐

  1. EntityFramework日志记录

    首先在应用启动时执行:DbInterception.Add(new LogFormatter());   然后加入如下类: #region [ EF的数据库执行日志记录 ] public class ...

  2. Android Studio 如何将包名按层级展示

    在project视图右上角有个“设置”的按钮,点开,然后将上图所圈部分去勾选就可以了.

  3. 使用queryperf对DNS服务器作压力测试

    一.querperf简介 当我们把DNS服务器配置好后,我们肯定会想测试一下DNS服务器的性能如何,上线后如果请求数够多服务器还能否响应?于是,我们可以使用软件模拟环境,对DNS服务器作评估性的测试. ...

  4. Mac上安装Tomcat

    参考文档:http://blog.csdn.net/j2ee_me/article/details/7928493 0.如果java没有被初始安装,所以你要自己去找Apple的安装程序这里来安装,或者 ...

  5. c#抓取网页内容乱码的解决方案

    写过爬虫的同学都知道,这是个很常见的问题了,一般处理思路是: 使用HttpWebRequest发送请求,HttpWebResponse来接收,判断HttpWebResponse中”Content-Ty ...

  6. iOS 10推送通知开发

    原文地址:Developing Push Notifications for iOS 10,译者:李剑飞 虽然通知经常被过度使用,但是通知确实是一种获得用户关注和通知他们需要更新或行动的有效方式.iO ...

  7. PHP开发之路之一--WAMP的安装和配置

    来到新公司,领导说后面一个web系统不用ASP.NET做了,用国外的一个Drupal进行二次开发.这个Drupal是基于PHP的一款开源CMS系统,那就必须要自学PHP咯~ 接下来说说正题吧: 一.安 ...

  8. JS-加载页面的时候自动选择刚才所选择option

    <body class="no-skin" onload="option_auto(${pd.PACK_SORT})"> <select na ...

  9. Spring Boot 系列教程15-页面国际化

    internationalization(i18n) 国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式. 它要求从产品中抽离所有地域语言,国家/地区和 ...

  10. Servlet与jsp间的传值问题

    Servlet与JSP 之间的传值有两种情况:JSP -> Servlet, Servlet -> JSP.通过对象 request和 session (不考虑 application)完 ...