UVA12983 The Battle of Chibi
第一眼能看出来是个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的更多相关文章
- 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 ...
- 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 ...
- hdu5542 The Battle of Chibi【树状数组】【离散化】
The Battle of Chibi Time Limit: 6000/4000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Othe ...
- 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 ...
- CDOJ 1217 The Battle of Chibi
The Battle of Chibi Time Limit: 6000/4000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Othe ...
- 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 ...
- 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 ...
- The Battle of Chibi
The Battle of Chibi 给出一段长度为n的序列\(\{a_i\}\),求其中长度为m的严格上升子序列个数\(mod\ 10^9+7\),\(n\leq 10^3\). 解 不难想到设\ ...
- ccpc_南阳 C The Battle of chibi dp + 树状数组
题意:给你一个n个数的序列,要求从中找出含m个数的严格递增子序列,求能找出多少种不同的方案 dp[i][j]表示以第i个数结尾,形成的严格递增子序列长度为j的方案数 那么最终的答案应该就是sigma( ...
随机推荐
- HDU-2089不要62-暴力或数位DP入门
不要62 题意:给定区间,求在这个区间中有多少个数字,不包含4且不包含62: 这道题作为数位DP的入门题: 暴力也是可以过 #include<cstdio> #include <io ...
- lightoj 1126 - Building Twin Towers(dp,递推)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1126 题解:一道基础的dp就是简单的递推可以设dp[height_left][ ...
- CodeForces 402 E Strictly Positive Matrix
Strictly Positive Matrix 题解: 如果原来的 a[i][j] = 0, 现要 a[i][j] = 1, 那么等于 sum{a[i][k] + a[k][j]} > 1. ...
- codeforces 284 C. Cows and Sequence(线段树)
题目链接:http://codeforces.com/contest/284/problem/C 题意:就是给出3个操作 1)是将前i 个数加x 2)在数组最后添加一个数x 3)删除数组最后的那个数 ...
- webpack多页应用架构系列(一):一步一步解决架构痛点
这系列文章讲什么? 前些时间,写过一个项目,前后端分离,没有借助任何框架,项目页面特别的多,页面都是html直接写的,许多公共html,写了好多处,有一个地方需要改就得改好多地方,js也是随意写,每个 ...
- 电脑修改密码后,git push 报错unable to access
电脑修改密码后,git push 时报错 remote: Permission to xxx A. fatal: unable to access 解决这个问题有两种方法,一种是界面修改,一种是命令 ...
- 逆向破解之160个CrackMe —— 030
CrackMe —— 030 160 CrackMe 是比较适合新手学习逆向破解的CrackMe的一个集合一共160个待逆向破解的程序 CrackMe:它们都是一些公开给别人尝试破解的小程序,制作 c ...
- centos 6.5 搭建DHCP实验
搭建DHCP服务 安装DHCP服务 挂载光盘:mount /dev/cdrom /qswz 从光盘的安装包中安装DHCP rpm -ivh dhcp-4.1.1-38.P1.el6.centos.i6 ...
- ORACLE官网JAVA学习文档
Trails Covering the Basics 1 Getting Started 1.1 The Java Technology Phenomenon 1.1.1 About the Ja ...
- android 端缓存清理的实现
首先关于缓存清理,网上已经有太多的工具类,但是遗憾的是,基本上都不完善,或者说根本就不能用,而项目中又要求实现这个烂东西(其实这玩意真没一点屁用,毕竟第三方清理/杀毒软件都带这么一个功能),但是只好硬 ...