赛后当天学长就说了树状数组,结果在一个星期后赖床时才有了一点点思路……

因为无法提交,不确定是否正确。。嗯。。有错希望指出,谢谢。。。

嗯。。已经A了。。提交地址http://acm.uestc.edu.cn/#/problem/show/1217

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; /**
题意:T代表测试组数 (T<=100)
每组两个数N, M(1<=N<=1000,1<=M<=N)
给N个数ai(1<=ai<=10^9)
求该数列的m长递增子序列
方法:容易想到dp[i][k]表示到第i个数,长度为k的子序列,O(N^3)可解
通过树状数组可以优化到O(N^2lgN)
*/ struct node{
int data;
int pos;
bool operator < (const node a) const
{
if (data == a.data)
return pos < a.pos;
return data < a.data;
}
} a[1005]; int c[1005][1005]; // c[k][i] 结尾数字为i,长度为k的子序列数量
int b[1005];
int N, M; const int MOD = 1000000007; int lowbit(int x)
{
return x & (-x);
} int sum(int x, int n)
{
int ans = 0;
while (n > 0) {
ans = (ans + c[x][n]) % MOD;
n -= lowbit(n);
}
return ans;
} void plu(int x, int pos, int num)
{
while (pos <= N) {
c[x][pos] = (c[x][pos] + num) % MOD;
pos += lowbit(pos);
}
} int main()
{
int t, cas = 0;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &N, &M);
for (int i = 1; i <= N; ++i)
{
scanf("%d", &a[i].data);
a[i].pos = i;
} // 离散化
sort(a + 1, a + N + 1);
int cnt = 1;
b[a[1].pos] = cnt;
for (int i = 2; i <= N; ++i)
{
if (a[i].data > a[i - 1].data) cnt++;
b[a[i].pos] = cnt;
} //for (int i = 1; i <= N; ++i) cout << b[i] << endl; memset(c, 0, sizeof c); for (int i = 1; i <= N; ++i)
{
plu(1, b[i], 1); // 以每个数结尾的长度为1的数都是1
for (int k = 2; k <= M; ++k)
{
int temp = sum(k - 1, b[i] - 1); // 所有比b[i]小的数长度为k-1的和就是以b[i]为结尾的长度为k的了。。
plu(k, b[i], temp);
}
} int ans = sum(M, N);
printf("Case #%d: %d\n", ++cas, ans);
}
return 0;
} /**
Input:
5
3 2
1 2 3
3 2
3 2 1
3 1
1 2 3
3 2
1 1 2
7 3
1 1 2 2 1 1 4 Output:
3
0
3
2
4
*/

  

2015 CCPC-C-The Battle of Chibi (UESTC 1217)(动态规划+树状数组)的更多相关文章

  1. ACM学习历程—UESTC 1217 The Battle of Chibi(递推 && 树状数组)(2015CCPC C)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1217 题目大意就是求一个序列里面长度为m的递增子序列的个数. 首先可以列出一个递推式p(len, i) =  ...

  2. [HDOJ5542]The Battle of Chibi(DP,树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:n个数中找m个数,使得从左到右读是上升的子序列.问一共有多少种. dp(i,j)表示取到第 ...

  3. HDU 6240 Server(2017 CCPC哈尔滨站 K题,01分数规划 + 树状数组优化DP)

    题目链接  2017 CCPC Harbin Problem K 题意  给定若干物品,每个物品可以覆盖一个区间.现在要覆盖区间$[1, t]$. 求选出来的物品的$\frac{∑a_{i}}{∑b_ ...

  4. 2015 南阳ccpc The Battle of Chibi (uestc 1217)

    题意:给定一个序列,找出长度为m的严格递增序列的个数. 思路:用dp[i][j]表示长度为i的序列以下标j结尾的总个数.三层for循环肯定超时,首先离散化,离散化之后就可以用树状数组来优化,快速查找下 ...

  5. 2015 北京网络赛 C Protecting Homeless Cats hihoCoder 1229 树状数组

    题意:求在平面上 任意两点连线,原点到这个点的距离小于d的点对有多少个,n=200000; 解: 以原点为圆心做一个半径为d的圆,我们知道圆内的点和园内以外的点的连线都是小于d的还有,圆内和园内的点联 ...

  6. 2015南阳CCPC C - The Battle of Chibi DP

    C - The Battle of Chibi Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Cao Cao made up a ...

  7. 2015南阳CCPC C - The Battle of Chibi DP树状数组优化

    C - The Battle of Chibi Description Cao Cao made up a big army and was going to invade the whole Sou ...

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

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

  9. hdu5542 The Battle of Chibi【树状数组】【离散化】

    The Battle of Chibi Time Limit: 6000/4000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Othe ...

随机推荐

  1. 使用memcache(thinkphp框架学习)

    $memcache = new Memcache; $memcache->connect("localhost",11211); $memcache->set('sxt ...

  2. 创建第一个UI

    创建一个2D UI 制作UI时,首先要创建UI的"根".在Unity顶部NGUI菜单中选择Create,然后选择2D UI. 创建完成后,在Scene窗口中,NGUI自动生成了一个 ...

  3. vue-cli + webpack

    vue-cli + webpack 关于vue.js vue.js是一套构建用户界面的 轻型的渐进式前端框架.它的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件.使用vue可以给你 ...

  4. Elasticsearch强大的聚合功能Facet

    在常规数据库中,我们都知道有一个sql就是group,分组.如果主表只有对应的一个列记录的分组的ID,那么还好统计,比如说每本书book表,有一个分类catId,记录是属于哪一类的书,那么直接按照ca ...

  5. Hadoop集群系类文章

    http://www.cnblogs.com/xia520pi/archive/2012/04/08/2437875.html 后续文章地址:http://www.xiapistudio.com/ta ...

  6. jquery and event

    jquery阻止事件冒泡 event.stopPropagation(); event.cancelBubble = true; jquery阻止默认操作 event.preventDefault() ...

  7. Linux下的绘图(流程图、UML、mindmap)工具

    http://blog.csdn.net/piyajee/article/details/5902380

  8. QT带OpenGL与不带的区别,QT5是一个伟大的框架,短时期内根本不会有替代者

    你好 , 我Qt的初学者 , 我在官网下载Qt时感觉很迷茫 , 不知道要下载哪个, 麻烦你写他们之间的不同点:Qt 5.2.0 for Windows 32-bit (MinGW 4.8, OpenG ...

  9. 分析Java的类加载器与ClassLoader(二):classpath与查找类字节码的顺序,分析ExtClassLoader与AppClassLoader的源码

    先回顾一下classpath classpath的作用: classpath的作用是指定查找类的路径:当使用java命令执行一个类(类中的main方法)时,会从classpath中进行查找这个类. 指 ...

  10. Android:Fragment+ViewPager实现Tab滑动

    public class FragAdapter extends FragmentPagerAdapter { private List<Fragment> fragments ; pub ...