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 ...
随机推荐
- BZOJ 1227 虔诚的墓主人
Description 小W 是一片新造公墓的管理人.公墓可以看成一块N×M 的矩形,矩形的每个格点,要么种着一棵常青树,要么是一块还没有归属的墓地.当地的居民都是非常虔诚的基督徒,他们愿意提前为自己 ...
- js pix
window.onload = function () { var img = new Image(); img.src = '1.gif'; var ctx = document.querySele ...
- SWFUpload下载地址
SWFUpload托管在谷歌代码上面,点击下载: https://code.google.com/p/swfupload/
- 为你下一个项目准备的 50 个 Bootstrap 插件
Bootstrap是快速开发Web应用程序的前端工具包.它是一个CSS和HTML的集合,它使用了最新的浏览器技术,给你的Web开发提供了时尚的版式,表单,buttons,表格,网格系统等等. 本文向你 ...
- mysql中实现行号,oracle中的rowid
mysql中实现行号需要用到MYSQL的变量,因为MySql木有rownumber. MYSQL中变量定义可以用 set @var=0 或 set @var:=0 可以用=或:=都可以,但是如果变量用 ...
- Weex详解:灵活的移动端高性能动态化方案
原文地址:http://www.infoq.com/cn/articles/introducing-weex 在2016年4月份的QCon上,阿里巴巴资深总监,淘宝移动平台及新业务事业部.阿里百川负责 ...
- 2D多边形碰撞器优化器
http://www.unity蛮牛.com/thread-19827-1-1.html http://pan.baidu.com/s/1qW2mWS8 Asset Store Link: http: ...
- php获得当前日期时间 date函数
time()得到从1970-01-01到现在共走了多少秒 要找出前一天的时间就是 time()-60*60*24; 要找出前一年的时间就是 time()*60*60*24*365 那么如何把这个数字换 ...
- [codility]tape_equilibrium
http://codility.com/demo/take-sample-test/tapeequilibrium 简单题.记录到i为止的sum就可以了.O(n). // you can also u ...
- 第六章Audio设备
6.1 Audio设备介绍 USB协议制定时,为了方便不同设备的开发商基于USB进行设计,定义了不同的设备类来支持不同类型的设备.虽然在USB标准中定义了USB_DEVICE_CLASS_AUDIO- ...