ACM学习历程—UESTC 1217 The Battle of Chibi(递推 && 树状数组)(2015CCPC C)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1217
题目大意就是求一个序列里面长度为m的递增子序列的个数。
首先可以列出一个递推式p(len, i) = sum(p(len-1, j)) (a[j] < a[i])
p(len, i)表示以第i个结尾的长度为len的子序列的个数。
但是如果按照递增子序列的思想,然后直接弄的话,复杂度是n*m*n的。
如果需要优化的话,可以优化的地方就是那个求sum的过程。
把p数组映射到树状数组,那么求和的过程就能在logn时间完成。
这样写完是m*logn,一不小心可能写成nlogn的。。
不过这样竟然还是T(比赛的时候能过,不知道是评测机性能不一样,还是加数据了)。
需要加一个剪枝,就是从长度1到m枚举时,当某次查询的结果为0时,说明前面的子序列长度都达不到这个长度了,那么更长的显然也达不到了,于是此时直接break了OK了。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#define LL long long
#define MOD 1000000007 using namespace std; const int maxN = ;
int n, m;
struct node
{
int v;
int id;
}a[maxN]; bool cmp(node x, node y)
{
if (x.v != y.v) return x.v < y.v;
else return x.id > y.id;
} LL d[maxN][maxN]; int lowbit(int x)
{
return x&(-x);
} void add(int len, int id, LL pls)
{
while(id <= maxN)//id最大是maxN
{
d[len][id] += pls;
d[len][id] %= MOD;
id += lowbit(id);
}
} LL sum(int len, int to)
{
LL s = ;
while(to > )
{
s = (s+d[len][to])%MOD;
to -= lowbit(to);
}
return s;
} void input()
{
scanf("%d%d", &n, &m);
int k;
for (int i = ; i < n; ++i)
{
scanf("%d", &k);
a[i].id = i+;
a[i].v = k;
}
sort(a, a+n, cmp);
memset(d, , sizeof(d));
} void work()
{
LL k;
for (int i = ; i < n; ++i)
{
add(, a[i].id, );
for (int j = ; j <= m; ++j)
{
k = sum(j-, a[i].id-);
if (!k) break;
add(j, a[i].id, k);
}
}
printf("%lld\n", sum(m, n));
} int main()
{
//freopen("test.in", "r", stdin);
int T;
scanf("%d", &T);
for (int times = ; times <= T; ++times)
{
printf("Case #%d: ", times);
input();
work();
}
return ;
}
ACM学习历程—UESTC 1217 The Battle of Chibi(递推 && 树状数组)(2015CCPC C)的更多相关文章
- uestc oj 1217 The Battle of Chibi (dp + 离散化 + 树状数组)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1217 给你一个长为n的数组,问你有多少个长度严格为m的上升子序列. dp[i][j]表示以a[i]结尾长为j ...
- ACM学习历程—51NOD 1685 第K大区间2(二分 && 树状数组 && 中位数)
http://www.51nod.com/contest/problem.html#!problemId=1685 这是这次BSG白山极客挑战赛的E题. 这题可以二分答案t. 关键在于,对于一个t,如 ...
- 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 ...
- ACM学习历程—SNNUOJ 1116 A Simple Problem(递推 && 逆元 && 组合数学 && 快速幂)(2015陕西省大学生程序设计竞赛K题)
Description Assuming a finite – radius “ball” which is on an N dimension is cut with a “knife” of N- ...
- ACM学习历程——HDU4814 Golden Radio Base(数学递推) (12年成都区域赛)
Description Golden ratio base (GRB) is a non-integer positional numeral system that uses the golden ...
- C - The Battle of Chibi HDU - 5542 (树状数组+离散化)
Cao Cao made up a big army and was going to invade the whole South China. Yu Zhou was worried about ...
- UESTC 1584 Washi与Sonochi的约定【树状数组裸题+排序】
题目链接:UESTC 1584 Washi与Sonochi的约定 题意:在二维平面上,某个点的ranked被定义为x坐标不大于其x坐标,且y坐标不大于其y坐标的怪物的数量.(不含其自身),要求输出n行 ...
- ACM学习历程—UESTC 1218 Pick The Sticks(动态规划)(2015CCPC D)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1218 题目大意就是求n根木棒能不能放进一个容器里,乍一看像01背包,但是容器的两端可以溢出容器,只要两端的木 ...
- ACM学习历程—UESTC 1222 Sudoku(矩阵)(2015CCPC H)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1226 题目大意就是构造一个行列和每个角的2*2都是1234的4*4矩阵. 用dfs暴力搜索,不过需要每一步进 ...
随机推荐
- Android Studio报Error:Execution failed for task ':Companion:preDexDebug'.
错误例如以下: Error:Execution failed for task ':Companion:preDexDebug'. > com.android.ide.common.proces ...
- 第三方苹果开发库之ASIHTTPRequest(翻译版)
本文转载至 http://www.cnblogs.com/daguo/archive/2012/08/03/2622090.html 来自:http://www.dreamingwish.com/ ...
- 你可能不知道的5个功能强大的 HTML5 API
HTML5 新增了许多重要的特性,像 video.audio 和 canvas 等等,这些特性使得能够很容易的网页中包含多媒体内容,而不需要任何的插件或者 API.而其它的新元素,例如 section ...
- 6.2-SingletonBeanRegistry-DefaultSingletonBeanRegistry
SingletonBeanRegistry package org.springframework.beans.factory.config; public interface SingletonBe ...
- linux c编程:进程控制(三)_exec函数
fork()函数通过系统调用创建一个与原来进程(父进程)几乎完全相同的进程(子进程是父进程的副本,它将获得父进程数据空间.堆.栈等资源的副本.注意,子进程持有的是上述存储空间的“副本”,这意味着父子进 ...
- cocos2d-x 源代码分析 : control 源代码分析 ( 控制类组件 controlButton)
源代码版本号来自3.1rc 转载请注明 cocos2d-x源代码分析总文件夹 http://blog.csdn.net/u011225840/article/details/31743129 1.继承 ...
- activiti 基础搭建
首先在eclipse内添加activiti的插件,https://blog.csdn.net/qq_22701869/article/details/79537971 1.创建一个maven的java ...
- vim中设置tab的长度的方法
linux下使用vim编程是比較常见的事情,但vim默认的tab是8个空格.但一般的编辑器是4个空格,所以希望改动下.详细方法例如以下:1. 创建文件名称为 .vimrc 的系统文件首先切换到用户根文 ...
- 未启用对服务器的远程访问 win7
设置好远程桌面,但是输入IP后却说未启用服务器远程访问 家里有2台机.另外一台经设置后可以使用远程桌面控制主机的程序了.但是主机在运行远程桌面访问另一台机时却说由于一些原因之一无法连接到远程计算机:1 ...
- Vue:实践学习笔记(1)——快速使用
Vue:实践学习笔记(1)——快速使用 Vue基础知识 0.引入Vue 官方地址:Vue的官方下载地址 Vue推荐博客:keepfool 在你的程序中快速引入Vue: <!-- 开发环境版本,包 ...