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( ...
随机推荐
- HTML 参考手册(摘自菜鸟教程)
标签 描述 基础 <!DOCTYPE> 定义文档类型. <html> 定义一个 HTML 文档 <title> 为文档定义一个标题 <body> ...
- C#开发BIMFACE系列7 服务端API之获取文件信息列表
系列目录 [已更新最新开发文章,点击查看详细] 本文详细介绍如何获取BIMFACE平台中所有上传过的文件信息列表. 请求地址:GET https://file.bimface.com/file ...
- cogs1709. [SPOJ 705] 不同的子串(后缀数组
http://cogs.pro:8080/cogs/problem/problem.php?pid=vyziQkWaP 题意:给定一个字符串,计算其不同的子串个数. 思路:ans=总共子串个数-相同的 ...
- UVA - 315 Network(tarjan求割点的个数)
题目链接:https://vjudge.net/contest/67418#problem/B 题意:给一个无向连通图,求出割点的数量.首先输入一个N(多实例,0结束),下面有不超过N行的数,每行的第 ...
- Netty源码分析 (四)----- ChannelPipeline
netty在服务端端口绑定和新连接建立的过程中会建立相应的channel,而与channel的动作密切相关的是pipeline这个概念,pipeline像是可以看作是一条流水线,原始的原料(字节流)进 ...
- Java集合:LinkedList (JDK1.8 源码解读)
LinkedList介绍 还是和ArrayList同样的套路,顾名思义,linked,那必然是基于链表实现的,链表是一种线性的储存结构,将储存的数据存放在一个存储单元里面,并且这个存储单元里面还维护了 ...
- Django + Gunicorn + Nginx 部署之路
前言 最近,我已经成功将我的个人网站从 Flask 迁移到 Django 了,最早接触 Django 的时候大概是在 4 年前,我记得那个时候 Django 中的路由配置使用 正则 来进行的,但是我有 ...
- Storm 系列(四)—— Storm 集群环境搭建
一.集群规划 这里搭建一个 3 节点的 Storm 集群:三台主机上均部署 Supervisor 和 LogViewer 服务.同时为了保证高可用,除了在 hadoop001 上部署主 Nimbus ...
- zookeeper 单机. 集群环境搭建
zookeeper分布式系统中面临的很多问题, 如分布式锁,统一的命名服务,配置中心,集群的管理Leader的选举等 环境准备 分布式系统中各个节点之间通信,Zookeeper保证了这个过程中 数据的 ...
- SpringCloud学习笔记(5):Hystrix Dashboard可视化监控数据
简介 上篇文章中讲了使用Hystrix实现容错,除此之外,Hystrix还提供了近乎实时的监控.本文将介绍如何进行服务监控以及使用Hystrix Dashboard来让监控数据图形化. 项目介绍 sc ...