2015 CCPC-C-The Battle of Chibi (UESTC 1217)(动态规划+树状数组)
赛后当天学长就说了树状数组,结果在一个星期后赖床时才有了一点点思路……
因为无法提交,不确定是否正确。。嗯。。有错希望指出,谢谢。。。
嗯。。已经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)(动态规划+树状数组)的更多相关文章
- ACM学习历程—UESTC 1217 The Battle of Chibi(递推 && 树状数组)(2015CCPC C)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1217 题目大意就是求一个序列里面长度为m的递增子序列的个数. 首先可以列出一个递推式p(len, i) = ...
- [HDOJ5542]The Battle of Chibi(DP,树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:n个数中找m个数,使得从左到右读是上升的子序列.问一共有多少种. dp(i,j)表示取到第 ...
- HDU 6240 Server(2017 CCPC哈尔滨站 K题,01分数规划 + 树状数组优化DP)
题目链接 2017 CCPC Harbin Problem K 题意 给定若干物品,每个物品可以覆盖一个区间.现在要覆盖区间$[1, t]$. 求选出来的物品的$\frac{∑a_{i}}{∑b_ ...
- 2015 南阳ccpc The Battle of Chibi (uestc 1217)
题意:给定一个序列,找出长度为m的严格递增序列的个数. 思路:用dp[i][j]表示长度为i的序列以下标j结尾的总个数.三层for循环肯定超时,首先离散化,离散化之后就可以用树状数组来优化,快速查找下 ...
- 2015 北京网络赛 C Protecting Homeless Cats hihoCoder 1229 树状数组
题意:求在平面上 任意两点连线,原点到这个点的距离小于d的点对有多少个,n=200000; 解: 以原点为圆心做一个半径为d的圆,我们知道圆内的点和园内以外的点的连线都是小于d的还有,圆内和园内的点联 ...
- 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 ...
- 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 ...
- uestc oj 1217 The Battle of Chibi (dp + 离散化 + 树状数组)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1217 给你一个长为n的数组,问你有多少个长度严格为m的上升子序列. dp[i][j]表示以a[i]结尾长为j ...
- hdu5542 The Battle of Chibi【树状数组】【离散化】
The Battle of Chibi Time Limit: 6000/4000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Othe ...
随机推荐
- C语言中‘\n'为什么能表示CRLF两个字节
为什么要说这个简单的问题? 众所周知,在Windows下文本文件的换行符是CRLF,占两个字节.在Unix下是LF,占一个字节.(还有奇葩的Mac是CR).但是C语言中直接printf一个 ‘\n’, ...
- 软件测试 -- alpha测试和beta测试的区别
alpha测试是在用户组织模拟软件系统的运行环境下的一种验收测试,由用户或第三方测试公司进行的测试,模拟各类用户行为对即将面市的软件产品进行测试,试图发现并修改错误. Beta测试是用户公司组织各方面 ...
- Junit4.12、Hamcrest1.3、Eclemma的安装和使用
1. Junit4.12和Hamcrest1.3的安装过程 步骤: 网上下载Junit和Hamcrest包文件,保存在本地. 新建Java项目命名为Triangle,在Eclipse菜单栏选择项目(P ...
- nodejs 第一次使用
在win7下安装与使用 1 nodejs官网下载,安装 https://nodejs.org/ 2 下载最新的 npm,在E:\nodejs\中解压 http://nodejs.org/dist/ ...
- Javascript编程模式(JavaScript Programming Patterns)Part 2.(高级篇)
模块编程模式的启示(Revealing Module Pattern) 客户端对象(Custom Objects) 懒函数定义(Lazy Function Definition) Christian ...
- 一个C语言宏展开问题
转自一个C语言宏展开问题 一个令人比较迷惑的问题,学C语言好多年,今天终于搞明白,记之. ------------------------------------------------------- ...
- Android-AttributeSet详解
public interface AttributeSet { /** * Returns the number of attributes available in the set. * * @re ...
- C#中如何生成矢量图
主要的功能就是使用C#画矢量图,然后导出到Word.Excel.Powerpoint中,并且能够再次被编辑.以下是解决过程: 首先应该确定在Office文档中可编辑图形使用的格式:学习了相关资料,了解 ...
- 【HDOJ】1706 The diameter of graph
这么个简单的题目居然没有人题解.floyd中计算数目,同时注意重边. /* 1706 */ #include <iostream> #include <string> #inc ...
- Oracle并行更新的两种方式(merge/update内联视图)
对于Oracle的两表联合更新的场景(有A.B两表,以A.id=B.id关联,根据B表中的记录更新A表中的相应字段),一般有update内联视图和merge两种方式,下面举例介绍: 创建用例表: ...