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

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

嗯。。已经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. uvision4 ide已停止工作

    情景描述: 笔者安装了新系统WIN8.1,装上了MDKV4.72.MDK编译程序可以正常工作,可是只要当我“下载程序”或者“调试程序”的时候就提示“uvision4 ide已停止工作”,迫不得已只能关 ...

  2. C99标准中的部分新特性

    我有点怀疑我会不会C语言了. 1.变长数组(VLA) ; scanf("%d", &n); int arr[n]; 2.数组初始化 ] = {[] = , [] = , [ ...

  3. MVC-登录并设置角色

    1.新建一个类,设置角色: using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

  4. ionic+angulajs

    基于ionic+angulajs的混合开发实现地铁APP 项目源码地址:https://github.com/zhangxy1035/SubwayMap 一.项目简介 在该项目中的地铁app是基于io ...

  5. maven 添加Sqlserver的jdbc jar包

    maven添加sqlserver的jdbc驱动包 jdbc.jar download url->http://pan.baidu.com/s/1hrEhdti 通过maven命令将jar包安装到 ...

  6. 常用DOS命令参数详解

    一.DIR命令的格式: dir [D:][PATH][NAME][[/A][:attrib]][/o:[sorted][/s][/b][/l][/c[h] (1)dir /a:attrib 缺省包含所 ...

  7. 图片生成操作属性导致WP内存溢出解决办法

    在开发的项目中,发现经常会出现异常 “内存溢出,不能再继续执行程序”,通过搜索一些国外的文章,发现原来是由于项目中的图片比较多,而生存操作设为了“内容”.通过设置图片的生成操作为“无”,复制操作为“如 ...

  8. C++ 数据类型及相关问题 及输出精度控制

    1.有哪些数据类型? 2.数据类型在不同的编译器会有不同的位宽,如何得知? 使用如下命令: cout<<sizeof(int)<<endl; cout<<sizeo ...

  9. AOT

    预 (AOT) 编译器 https://angular.cn/docs/ts/latest/cookbook/aot-compiler.html To run your app in AoT mode ...

  10. Asp.net MVC 视图之公用代码

    一.公共模板 转自:http://www.cnblogs.com/kissdodog/archive/2013/01/07/2848881.html 1.@RenderBody() 在网站公用部分通过 ...