题目链接: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. Linux - SVN的基本操作

    SVN的基本操作 本文地址: http://blog.csdn.net/caroline_wendy $ svn diff //显示改动 $ post-review --summary="b ...

  2. ProjectManager Beta 7 项目管理器发布

    上次在Alpha阶段有一个可用版本Alpha 8也在这个博客发布了,传送:http://www.cnblogs.com/deali/p/ProjectManager.html ProjectManag ...

  3. lua(注册c库)

    #include <iostream> #include <string.h> extern "C" { #include "lua-5.2.2/ ...

  4. (比赛)A - Simple String Problem

    A - Simple String Problem Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%lld & ...

  5. Hadoop伪分布式环境快速搭建

    Hadoop分支 Apache Cloudera Hortonworks 本文是采用Cloudera分支的hadoop. 下载cdh-5.3.6 版本 下载地址:http://archive.clou ...

  6. json.dumps(i['bd_res'], ensure_ascii=False)

    json.dumps(i['bd_res'], ensure_ascii=False) import xlrd import time import sys import os import requ ...

  7. 【python】-- 信号量(Semaphore)、event(红绿灯例子)

    信号量(Semaphore) 之前讲的线程锁(互斥锁) 同时只允许一个线程更改数据,而Semaphore是同时允许一定数量的线程更改数据 ,比如厕所有3个坑,那最多只允许3个人上厕所,后面的人只能等里 ...

  8. LookAndFeelInfo

    在读zap源码的时候碰到:UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());以下来自百度知道UIManager.s ...

  9. Python 面试题(上)

    Python语言特性 1 Python的函数参数传递 看两个例子: a = 1 deffun(a): a = 2 fun(a) printa # 1 a = [] deffun(a): a.appen ...

  10. SQL性能优化常用语句(摘录网上)

    1.把trace文件导入到表中 , ) AS RowNumber,* into TableName FROM fn_trace_gettable('trace.trc', default) 2.查询C ...