第一眼能看出来是个dp

O(\(n^3\)) 暴力应该很好想 dp[i][j] = \(\sum_{k=1}^i [a[k] < a[i]] *dp[k][j-1]\)

发现dp[i][j] 为前面小于它的数长度为j-1的总和, 用树状数组前缀和优化一下搞成\(O(n^2log_n)\)

先离散化, dp时先枚举位置,再枚举上升序列的长度

树状数组要开二维哦, 打代码的时候发现dp数组可以用树状数组直接代替(小小的空间优化)

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1005;
const int P = 1e9+7;
int read(void) {
int x = 0;
char c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)) {
x = (x << 3) + (x << 1) + c - '0';
c = getchar();
}
return x;
}
int T, n, m;
int a[N];
struct node{
int pos, val;
bool operator < (const node &i) const {
if (val == i.val) return pos < i.pos;
return val < i.val;
}
}p[N];
bool cmp(node i,node j) {
return i.pos < j.pos;
}
long long d[N][N];
inline int low(int x) {
return x & -x;
}
int sum(int x,int p) {
long long val = 0;
for (;x;x -= low(x)) val += d[p][x];
return val % P;
}
void add(int x,int k,int p) {
for (;x <= n; x += low(x)) d[p][x] = (d[p][x] + k) % P;
}
int main() {
T = read();
int cnt = 0;
while (T--) {
cnt++;
n = read(), m = read();
for (int i = 1;i <= n; i++) p[i] = (node){i,read()};
sort(p + 1,p + n + 1);
int x = 0, k = 0;
for (int i = 1;i <= n; i++) {
if (x == p[i].val)
p[i].val = k;
else {
x = p[i].val;
p[i].val = i;
k = i;
}
}
sort(p + 1,p + n + 1, cmp);
memset(d, 0, sizeof(d));
long long ans = 0;
for (int i = 1;i <= n; i++) {
add(p[i].val, 1, 1);
for (int j = 2;j <= m; j++) {
int k = sum(p[i].val-1, j-1);
add(p[i].val, k, j);
if (j == m) ans = (ans + k) % P;
}
}
if (m == 1) ans += n;
printf ("Case #%d: %lld\n", cnt, ans % P);
}
return 0;
}

UVA12983 The Battle of Chibi的更多相关文章

  1. The 2015 China Collegiate Programming Contest C. The Battle of Chibi hdu 5542

    The Battle of Chibi Time Limit: 6000/4000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Othe ...

  2. 2015南阳CCPC C - The Battle of Chibi DP

    C - The Battle of Chibi Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Cao Cao made up a ...

  3. hdu5542 The Battle of Chibi【树状数组】【离散化】

    The Battle of Chibi Time Limit: 6000/4000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Othe ...

  4. HDU - 5542 The Battle of Chibi(LIS+树状数组优化)

    The Battle of Chibi Cao Cao made up a big army and was going to invade the whole South China. Yu Zho ...

  5. CDOJ 1217 The Battle of Chibi

    The Battle of Chibi Time Limit: 6000/4000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Othe ...

  6. 2015南阳CCPC C - The Battle of Chibi DP树状数组优化

    C - The Battle of Chibi Description Cao Cao made up a big army and was going to invade the whole Sou ...

  7. The Battle of Chibi(数据结构优化dp,树状数组)

    The Battle of Chibi Cao Cao made up a big army and was going to invade the whole South China. Yu Zho ...

  8. The Battle of Chibi

    The Battle of Chibi 给出一段长度为n的序列\(\{a_i\}\),求其中长度为m的严格上升子序列个数\(mod\ 10^9+7\),\(n\leq 10^3\). 解 不难想到设\ ...

  9. ccpc_南阳 C The Battle of chibi dp + 树状数组

    题意:给你一个n个数的序列,要求从中找出含m个数的严格递增子序列,求能找出多少种不同的方案 dp[i][j]表示以第i个数结尾,形成的严格递增子序列长度为j的方案数 那么最终的答案应该就是sigma( ...

随机推荐

  1. 一文带您了解 Elasticsearch 中,如何进行索引管理(图文教程)

    欢迎关注笔者的公众号: 小哈学Java, 每日推送 Java 领域干货文章,关注即免费无套路附送 100G 海量学习.面试资源哟!! 个人网站: https://www.exception.site/ ...

  2. gym/102059 E

    gym/102059 待通过:A.D.G.J.M 已补过:E E:电路题,判断一个图是不是简单电路.不需要特殊的技巧,利用set存图,把度数为2的点都删掉,融入到一条边上即可. #include &l ...

  3. HDU - 5128The E-pang Palace+暴力枚举,计算几何

    第一次写计算几何,ac,感动. 不过感觉自己的代码还可以美化一下. 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5128 题意: 在一个坐标系中,有n个 ...

  4. CodeForces 765 F Souvenirs 线段树

    Souvenirs 题意:给你n个数, m次询问, 对于每次一次询问, 求出询问区间内绝对值差值的最小值. 题解:先按查询的右端点从小到大sort一下,然后对于塞入一个数的时候, 就处理出所有左端点到 ...

  5. hdu 3966 Aragorn's Story(树链剖分+区间修改+单点查询)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意:给一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路径上 ...

  6. 2018 Multi-University Training Contest 3(部分题解)

    Problem F. Grab The Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Ja ...

  7. 如何使用React搭建项目

    1.首先说明node.js.npm.cnpm分别是做什么的? node.js简单的说 Node.js 就是运行在服务端的 JavaScript,安装了node.js默认安装了npm,可以使用npm - ...

  8. ssh的秘钥认证

    ssh秘钥认证简述 通常我们会使用x-shell.putty.MobaXterm等支持ssh连接的工具去登录服务器进行管理,而执行ssh命令.scp命令等从一台服务器登录另外一台服务器的时候,通常需要 ...

  9. 网络基础 ----------- 电脑作为wifi站点

    在上大学的时候最难受的就是,没有无线,但是电脑有宽带,那么怎么将电脑变成路由器哪 1.首先查看你的无线网卡是否支持开无线 通过命令win + R 快捷件进入命令窗口输入 : . netsh wlan ...

  10. Winform中通过代码设置DevExpress的TextEdit的类型为Numbernic

    场景 使用DevExpress的EditText控件时,需要限制其输入类型为数字. 正常来说是窗体上拖拽一个TextEdit,然后在设计窗口点击小三角,选择Change Mask 但是如果说TextE ...