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 ...
随机推荐
- pywinauto二次封装(pywinnat.py)
将pywinauto常用方法进行封装,使得pywinauto用起来更简单 #头文件的引入 from pywinauto import application from pywinauto import ...
- C# - 定义集合,索引符
Animal 类 Cow类 Chicken类 Animal 集合类 调用
- CSS3 animation的steps方式过渡
animation默认以ease方式过渡,它会在每个关键帧之间插入补间动画,所以动画效果 是连贯性的.除了ease,linear.cubic-bezier之类的过渡函数都会为其插入补间. 但有些效果不 ...
- [topcoder]FoxAndChess
http://community.topcoder.com/stat?c=problem_statement&pm=12725&rd=15702 这题比较简单.首先所有的LR的顺序要一 ...
- TYPEC 接口芯片CC逻辑原理与必要性
USB Type-C凭借其自身强大的功能,在Apple,Intel,Google等厂商的强势推动下,必将迅速引发一场USB接口的革命,并将积极影响我们日常生活的方方面面.为了能够使自己的设备兼容这些接 ...
- NIS 服务器
有没有想过,如果我有十部 Linux 主机,这十部主机仅负责不同的功能,事实上, 所有的主机账号与对应的密码都相同!那么我是将账号与密码分别设定置在十部计算机上面, 还是可以透过一部主机做为账号管理的 ...
- Two-Phase Locking
两阶段封锁(Two-Phase Locking) 两段锁协议的内容 1. 在对任何数据进行读.写操作之前,事务首先要获得对该数据的封锁 2. 在释放一个封锁之后,事务不再获得任何其他封锁. “两段”锁 ...
- Rails3.2.3+ruby1.9.3 环境搭建,提示安全警告
错误描述: 照着教程搭建了Rails的环境,能够正常运行,但是会但一个警告,如下: SECURITY WARNING: No secret option provided to Rack::Sessi ...
- 存储过程系列之调试存储过程 SQL Server 2005
在数据库中直接调试 在数据库中直接调试是调试SQL Server 2005的存储过程的最简单的方法. 在Visual Stuido的IDE中你可以选择单步执行存储过程,然后就可以一条语句一条语句地单 ...
- 【HDOJ】1042 N!
肯定是大叔,本来以为用加法做乘法,后来想想这样麻烦,还是可以使用乘法的,按位乘,ov可以看成不止一位,程序如下所示: #include <stdio.h> #define MAX 4000 ...