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
3 2
3 1 2
2
5 3
5 4 3 2 1
10

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

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000000
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=2e4+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t,a[maxn],dp[][maxn],p[maxn];
ll ans;
void add(int x,int y)
{
for(int i=x;i<=n;i+=(i&(-i)))
a[i]+=y,a[i]%=mod;
} int get(int x)
{
int res=;
for(int i=x;i;i-=(i&(-i)))
res+=a[i],res%=mod;
return res;
}
int main()
{
int i,j;
scanf("%d%d",&n,&k);
rep(i,,n)scanf("%d",&p[i]),dp[][i]=;
rep(i,,k)
{
memset(a,,sizeof a);
rep(j,,n)
{
dp[i][j]=(get(n)-get(p[j])+mod)%mod;
add(p[j],dp[i-][j]);
}
}
rep(i,,n)ans=(ans+dp[k][i])%mod;
printf("%lld\n",ans);
//system("Pause");
return ;
}

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. win10快捷键大全

    win10快捷键大全大家可以来了解一下,今天小编带来了win10常用快捷键,很多朋友喜欢使用快捷键来操作电脑,那么Windows10系统有哪些新的快捷键呢• 贴靠窗口:Win +左/右> Win ...

  2. IO模式设置网络编程常见问题总结—IO模式设置,阻塞与非阻塞的比较,recv参数对性能的影响—O_NONBLOCK(open使用)、IPC_NOWAIT(msgrcv)、MSG_DONTWAIT(re

    非阻塞IO 和阻塞IO: 在网络编程中对于一个网络句柄会遇到阻塞IO 和非阻塞IO 的概念, 这里对于这两种socket 先做一下说明:       基本概念: 阻塞IO:: socket 的阻塞模式 ...

  3. overlay

    http://dockone.io/article/237 http://blog.cloud66.com/docker-with-overlayfs-first-impression/ http:/ ...

  4. js监听键盘方向键事件

    <SCRIPT language=javascript> document.onkeydown = chang_page; function chang_page() { || ) loc ...

  5. selenium2.0集成测试案例

    webDriver模拟点击对web工程测试还是挺方便的. package suite; import java.util.concurrent.TimeUnit; import org.junit.A ...

  6. 在Android studio中进行NDK开发

     在Android studio中进行NDK开发  分类: Android平台 软硬件环境 ubuntu kylin 14.04 红米note增强版 Android studio 0.8.6 ndk ...

  7. 添加一个Application Framework Service

    如何添加一个Application Framework Service(without native code)? 1.本文参照AlarmManagerService实现一个简单的Applicatio ...

  8. (一)Python之猜数游戏

    猜数游戏由简如深的编码学习过程: 3次机会: print('------------------我爱鱼C工作室------------------')count=0while count < 3 ...

  9. JDBC连接(MySql)数据库步骤,以及查询、插入、删除、更新等十一个处理数据库信息的功能

    主要内容:  JDBC连接数据库步骤. 一个简单详细的查询数据的例子. 封装连接数据库,释放数据库连接方法. 实现查询,插入,删除,更新等十一个处理数据库信息的功能.(包括事务处理,批量更新等) 把十 ...

  10. android 界面悬浮框实现

    // 定义浮动窗口布局 private View mFloatLayout; // 定义浮动窗口布局对象 private WindowManager.LayoutParams wmParams; // ...