题目链接: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)的更多相关文章

  1. uestc oj 1217 The Battle of Chibi (dp + 离散化 + 树状数组)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1217 给你一个长为n的数组,问你有多少个长度严格为m的上升子序列. dp[i][j]表示以a[i]结尾长为j ...

  2. ACM学习历程—51NOD 1685 第K大区间2(二分 && 树状数组 && 中位数)

    http://www.51nod.com/contest/problem.html#!problemId=1685 这是这次BSG白山极客挑战赛的E题. 这题可以二分答案t. 关键在于,对于一个t,如 ...

  3. 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 ...

  4. 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- ...

  5. ACM学习历程——HDU4814 Golden Radio Base(数学递推) (12年成都区域赛)

    Description Golden ratio base (GRB) is a non-integer positional numeral system that uses the golden ...

  6. 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 ...

  7. UESTC 1584 Washi与Sonochi的约定【树状数组裸题+排序】

    题目链接:UESTC 1584 Washi与Sonochi的约定 题意:在二维平面上,某个点的ranked被定义为x坐标不大于其x坐标,且y坐标不大于其y坐标的怪物的数量.(不含其自身),要求输出n行 ...

  8. ACM学习历程—UESTC 1218 Pick The Sticks(动态规划)(2015CCPC D)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1218 题目大意就是求n根木棒能不能放进一个容器里,乍一看像01背包,但是容器的两端可以溢出容器,只要两端的木 ...

  9. ACM学习历程—UESTC 1222 Sudoku(矩阵)(2015CCPC H)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1226 题目大意就是构造一个行列和每个角的2*2都是1234的4*4矩阵. 用dfs暴力搜索,不过需要每一步进 ...

随机推荐

  1. Jmeter 04 JMeter 负载与监听

    1. 场景设计 2. 场景设置 3. JMeter性能参数配置 4. 测试监听

  2. [SCOI2009]生日礼物(尺取法)

    Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2201  Solved: 1186[Submit][Status][Discuss] Descript ...

  3. Js编写的菜单树

    只需要提供这种JSON格式就ok了 其他的都可以直接引用这个代码进去 var testMenu=[ { "name": "一级菜单", "submen ...

  4. C#DataSet/DataAdapter

    DataReader必须持续连接,所以在调用方法SqlDataReader作为返回类型时候,必须在方法外关闭流,很不方便. DataAdapter用于对数据源检索数据并填充到DataSet中的表.Da ...

  5. C# ADO.NET学习

    Connetction 对象: 数据库服务器 数据库名字 登录名.密码 连接数据库所需要的其他参数 Command对象: ExecuteScalar();//首行首列的内容 ExecuteNomQue ...

  6. Linux基础系列:常用命令(8)_shell script

    一.什么是shell script 将OS命令堆积到可执行的文件里,由上至下的顺序执行文本里的OS命令 就是脚本了. 再加上些智能(条件/流控)控制,就变成了智能化脚本了 二.变量命名规则 以字母或下 ...

  7. ubuntu14.04 pygame安装 python2.7

    系统:ubuntu14.04 LTS amd64python版本:2.7.6pygame版本:1.9.1release别这种方法了,这么安装不知道什么原因就出现了问题,在使用pygame.image. ...

  8. read + 计算

    #!/bin/sbin read -p "please input first number:" a read -p "please input second numbe ...

  9. Windows 2003 复制大文件提示系统资源不足的处理方法

    方案一: 修改虚拟内存,让虚拟内存的大小略微超过要复制的文件的大小. 方案二: 修改注册表,如下: 注册表设置1 单击开始,单击运行,在打开框中键入“REGEDIT“ ,然后单击“确定”. 找到并单击 ...

  10. 推荐ajaxfilemanager for tiny_mce 比较完善的tiny_mce编辑器的图片上传及图片管理插件PHP版 支持中文

    tiny_mce编辑器,我觉得挺简洁.好用的,但就是图片上传的插件是收费的,而且网上找了半天也没有找到开源好用的上传插件. 不过功夫不负有心人,终于还就被我找到一款相当满意的插件. 这个插件的名字叫a ...