题目链接: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. Latex中參考文献排序

    \bibliographystyle{unsrt}:依照引用的先后排序 \bibliographystyle{plain}:按字母的顺序排列,比較次序为作者.年度和标题.当中作者中姓氏字母优先. 关于 ...

  2. Solr In Action 中文版 第一章(四、五)

    1.1             功能概览1. 4 最后,让我们再依照以下的分类.高速的过一下Solr的主要功能: ·用户体验 ·数据建模 ·Solr 4的新功能 在本书中.为你的用户提供良好的搜索体验 ...

  3. 在fc6上搭tftpd

    公司的开发环境依然停留在fc6上,,,,对..很旧,旧到想死. 我在没有进一步熟悉ubuntu的基础上,为了保持ABI一致. 只能依旧在FC6 上开发. 可是现在发现开发完成,我要在fc6上文件到wi ...

  4. JS表单提交

    测试一: function submit(){var form1=document.getElementById("form1")form1.action="/manag ...

  5. 我的Android进阶之旅------>Android关于TextWatcher的初步了解

    首先来看一下TextWatcher的源代码 package android.text; /** * When an object of a type is attached to an Editabl ...

  6. 安装Eclipsemaven插件

    Maven的Eclipse插件m2eclipse的安装 The goal of the m2ec project is to provide a first-class Apache Maven su ...

  7. Java多线程系列 JUC线程池01 线程池框架

    转载  http://www.cnblogs.com/skywang12345/p/3509903.html 为什么引入Executor线程池框架 new Thread()的缺点 1. 每次new T ...

  8. 高性能js之数据访问性能

    js中si中基本数据访问: 直接量, 变量, 数组项, 对象成员 性能问题: 首先要理解作用域链的基本概念,例如,当一个函数被创建时,就会产生一个激活对象(AO对象),AO对象中存储了该函数中所有的属 ...

  9. leetcode 901. Online Stock Span

    Write a class StockSpanner which collects daily price quotes for some stock, and returns the span of ...

  10. MapReduce分区的使用(Partition)

    MapReduce中的分区默认是哈希分区,根据map输出key的哈希值做模运算,如下 int result = key.hashCode()%numReduceTask; 如果我们需要根据业务需求来将 ...