题目链接: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. hive常规配置及常用命令使用

    hive 常用的几种shell交互方式 查看hive命令帮助:bin/hive -help [hd@hadoop-senior hive]$ bin/hive -help usage: hive -d ...

  2. python 捕获异常详细信息

    import os import sys import traceback BasePath = os.path.dirname(os.getcwd()) sys.path.append(BasePa ...

  3. QT修改应用程序图标

    要准备一个ico的图标,必须是ico格式,切记!! 可以用png或者其他的在线转换:http://www.easyicon.net/covert/ 用记事本 新建文件icon.rc,内容为: IDI_ ...

  4. secureCRT linux shell显示中文乱码 解决方法

    引:有没有这样的经历: 1.在shell中直接查看包含中文的文件时,出现一堆火星文,不得不下载下来window看. 2.无法正常的在shell中输入中文. 3.make的时候输出一堆乱码. 以下是查阅 ...

  5. basic--factory

    <?php /* 工厂模式: 由工厂类根据参数来决定创建出哪一种产品类的实例: 工厂类是指包含了一个专门用来创建其他对象的方法的类.所谓按需分配,传入参数进行选择,返回具体的类.工厂模式的最主要 ...

  6. 3.15课·········out传值(传址)

    public void Hs(out int a, out int b) { a = 4; b = 6; a = b++;//a=6,b=b+1=7//b先赋值给a,然后b+1 b = ++a;//a ...

  7. iOS 在视图控制器里面判断 应用程序的前台 后台切换 UIViewController

    1.时机  用户点击home 键  应用退到后台 再次点击进入前台  在UIViewController里面 控制器如何获取相关的事件? 2.需求 (1)NSTimer   在应用程序进入后台 10秒 ...

  8. linux中什么是文件结构体?

    struct file结构体定义在include/linux/fs.h中定义.文件结构体代表一个打开的文件,系统中的每个打开的文件在内核空间都有一个关联的 struct file.它由内核在打开文件时 ...

  9. 每天一个Linux命令(14)head命令

    head命令用于显示文件的开头的内容.在默认情况下,head命令显示文件的头10行内容.    如果指定了多于一个文件,在每一段输出前会给出文件名作为文件头. 如果不指定文件,或者文件为"- ...

  10. Java多线程系列 JUC线程池07 线程池原理解析(六)

     关闭“线程池” shutdown()的源码如下: public void shutdown() { final ReentrantLock mainLock = this.mainLock; // ...