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( ...
随机推荐
- Web安全开发规范手册V1.0
一.背景 团队最近频繁遭受网络攻击,引起了部门技术负责人的重视,笔者在团队中相对来说更懂安全,因此花了点时间编辑了一份安全开发自检清单,觉得应该也有不少读者有需要,所以将其分享出来. 二.自检清单 检 ...
- npm基本命令
1.npm是什么? npm(Node Package Manager)意思是 node 的包管理器,它是随着 NodeJs 安装时一起被安装的: 无论是在前端还是在前端开发中都会使用到 npm 包管理 ...
- 关于工作流引擎ccflow待办分类 研究与技术实现
关于工作流引擎待办分类 研究与技术实现 关键字:工作流引擎 BPM系统 待办类型 名词:待办 概要介绍:待办就是当前的登录人员要处理的工作,在工作流程里面的节点类型不同,业务场景不同,我们把待办分为如 ...
- 使用python发生邮箱
1.在使用邮箱登陆需要在邮箱内开启SMTP服务 2.注意在代码中登陆程序使用的密码为第三方授权登陆码,QQ邮箱为系统提供的授权码 网易邮箱为自己设置的授权码 QQ邮箱模拟 import smtplib ...
- 牛客练习赛38 E 出题人的数组 2018ccpc桂林A题 贪心
https://ac.nowcoder.com/acm/contest/358/E 题意: 出题人有两个数组,A,B,请你把两个数组归并起来使得cost=∑i∗ci 最小,归并要求原数组的数的顺序在新 ...
- HDU- 3605 - Escape 最大流 + 二进制压位
HDU - 3605 : acm.hdu.edu.cn/showproblem.php?pid=3605 题目: 有1e5的的人,小于10个的星球,每个星球都有容量,每个人也有适合的星球和不适合的星球 ...
- lightoj 1134 - Be Efficient(组合数)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1134 题解:简单的一道组合题,现求一下前缀和,然后只要找前缀和膜m的结果相同的 ...
- CF1025C Plasticine zebra 思维 字符串
Plasticine zebra time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- ASP.NET CORE Docker发布记录
1.安装Docker yum install curl -y curl -fsSL https://get.docker.com/ | sh 2.编写Dockerfile文件 FROM microso ...
- WordCount--统计输入文件的字符数、行数、单词数(java)--初级功能
码云地址: https://gitee.com/YuRenDaZ/WordCount 个人PSP表格: PSP2.1 PSP阶段 预估耗时 (分钟) 实际耗时 (分钟) Planning 计划 180 ...