ural1523 K-inversions
K-inversions
Memory limit: 64 MB
Input
Output
Samples
| input | output |
|---|---|
3 2 |
2 |
5 3 |
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的更多相关文章
- django模型操作
Django-Model操作数据库(增删改查.连表结构) 一.数据库操作 1.创建model表
- Inversions After Shuffle
Inversions After Shuffle time limit per test 1 second memory limit per test 256 megabytes input stan ...
- 《算法导论》Problem 2-4 Inversions
在Merge Sort的基础上改改就好了. public class Inversions { public static int inversions(int [] A,int p, int r) ...
- [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) ...
- HDU 6318 - Swaps and Inversions - [离散化+树状数组求逆序数][杭电2018多校赛2]
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=6318 Problem Description Long long ago, there was an ...
- HDU 6318 Swaps and Inversions 思路很巧妙!!!(转换为树状数组或者归并求解逆序数)
Swaps and Inversions Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 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 ...
- HDU 多校对抗赛第二场 1010 Swaps and Inversions
Swaps and Inversions Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- Codeforces 513G1 513G2 Inversions problem [概率dp]
转自九野:http://blog.csdn.net/qq574857122/article/details/43643135 题目链接:点击打开链接 题意: 给定n ,k 下面n个数表示有一个n的排列 ...
- 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 ...
随机推荐
- 一个小知识点强引用__strong 弱引用__weak
__weak的类型的指针是不会影响对象的释放 当系统释放后 会自动的指向nil: __Strong 至少有一个__Strong类型的指针是 对象不会释放
- c++ lower_bound upper_bound
lower_bound, first greater than or equal to upper_bound, first strickly greater
- 在Windows上安装Python
首先,从官网下载 最新版本 的Python 2.7.可通过 Python官网 的”Windows Installer”链接保证下载到的版本是最新的. Windows版本是MSI文件格式,双击它即可开始 ...
- Linux系统的信号详解
一.信号类型 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) ...
- c_select 调用参数说明
c_select 调用 1. select系统调用select系统调用是用来让我们的程序监视多个文件描述符的状态变化的.程序会停在select这里等待,直到被监视的文件描述符有某一个或多个发生了状态改 ...
- mxml日期显示使用
mxml代码: <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx= ...
- 清除number输入框的上下箭头
<input type="number"/> 在chrome,firefox,safari浏览器上输入框右侧会有上下箭头 方法1: <input type=&qu ...
- POJ 2368 巴什博奕
题目大意:给出n个按钮,每次最多可以按L个,按下最后一个按钮的人获胜.求使后手必定获胜的L的最小值(L>=2). 题目思路: 巴什博弈:只有一堆n个物品,两个人轮流从这堆物品中取物,规定每次至少 ...
- 实测switch支持的参数类型
@Test public void testSwitch() { switch (2) { case 1: System.out.println("int型:" + 1); bre ...
- easyui实现权限管理
在js中: function makeEasyTree(data){ if(!data) return []; var _newData = []; //最终返回结果 var _treeArray = ...