CodeForces - 597C Subsequences 【DP + 树状数组】
题目链接
http://codeforces.com/problemset/problem/597/C
题意
给出一个n 一个 k
求 n 个数中 长度为k的上升子序列 有多少个
思路
刚开始就是想用dp 复杂度 大概是 O(n ^ 2 * k)
T了
但是 思路还是一样的 只是用树状数组 优化了一下 第三层循环
dp[i][j] 表示 第 i 个数 长度为 j 时
那么 dp[i][j] 的状态转移就是 ∑(arr[i] > arr[k] ? : dp[k][j - 1] )
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <list>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a, b) memset(a, (b), sizeof(a));
#define pb push_back
#define bug puts("***bug***");
#define X first
#define Y second
#define L(on) (on<<1)
#define R(on) (L(on) | 1)
#define all(x) x.begin(), x.end()
#define stack_expand #pragma comment(linker, "/STACK:102400000,102400000")
#define syn_close ios::sync_with_stdio(false);cin.tie(0);
//#define bug
//#define gets gets_s
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <string, int> psi;
typedef pair <string, string> pss;
typedef pair <double, int> pdi;
const double PI = acos(-1.0);
const double EI = exp(1.0);
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 10;
const int MOD = 6;
int arr[maxn];
ll dp[maxn][15];
int lowbit(int x)
{
return x & (-x);
}
ll sum(int x, int y)
{
ll ans = 0;
while (x > 0)
{
ans += dp[x][y];
x -= lowbit(x);
}
return ans;
}
void add(int x, int y, ll val)
{
while (x <= maxn)
{
dp[x][y] += val;
x += lowbit(x);
}
}
int main()
{
int n, m;
scanf("%d%d", &n, &m);
m++;
for (int i = 1; i <= n; i++)
scanf("%d", &arr[i]);
CLR(dp, 0);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= min(i + 1, m); j++)
{
if (j == 1)
add(arr[i], 1, 1);
else
{
ll temp = sum(arr[i] - 1, j - 1);
add(arr[i], j, temp);
}
}
}
printf("%lld\n", sum(n, m));
}
//int arr[maxn]; // origin idea TLE on test 19
//
//ll dp[maxn][15];
//
//int main()
//{
// int n, K;
// scanf("%d%d", &n, &K);
// K++;
// for (int i = 0; i < n; i++)
// scanf("%d", &arr[i]);
// CLR(dp, 0);
// for (int i = 0; i < n; i++)
// dp[i][1] = 1;
// for (int i = 1; i < n; i++)
// {
// for (int j = 2; j <= min(i + 1, K); j++)
// {
// for (int k = 0; k < i; k++)
// {
// if (arr[i] > arr[k])
// dp[i][j] += dp[k][j - 1];
// }
// }
// }
// ll ans = 0;
//
// for (int i = 0; i < n; i++)
// ans += dp[i][K];
//
// cout << ans << endl;
//}
CodeForces - 597C Subsequences 【DP + 树状数组】的更多相关文章
- CodeForces - 597C Subsequences (树状数组+动态规划)
For the given sequence with n different elements find the number of increasing subsequences with k + ...
- codeforces 597C C. Subsequences(dp+树状数组)
题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...
- HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences ...
- 树形DP+树状数组 HDU 5877 Weak Pair
//树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...
- bzoj 1264 [AHOI2006]基因匹配Match(DP+树状数组)
1264: [AHOI2006]基因匹配Match Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 793 Solved: 503[Submit][S ...
- 【bzoj2274】[Usaco2011 Feb]Generic Cow Protests dp+树状数组
题目描述 Farmer John's N (1 <= N <= 100,000) cows are lined up in a row andnumbered 1..N. The cows ...
- 奶牛抗议 DP 树状数组
奶牛抗议 DP 树状数组 USACO的题太猛了 容易想到\(DP\),设\(f[i]\)表示为在第\(i\)位时方案数,转移方程: \[ f[i]=\sum f[j]\;(j< i,sum[i] ...
- [Codeforces 1208D]Restore Permutation (树状数组)
[Codeforces 1208D]Restore Permutation (树状数组) 题面 有一个长度为n的排列a.对于每个元素i,\(s_i\)表示\(\sum_{j=1,a_j<a_i} ...
- CodeForces - 314C Sereja and Subsequences (树状数组+dp)
Sereja has a sequence that consists of n positive integers, a1, a2, ..., an. First Sereja took a pie ...
随机推荐
- 计算两个有序数组的第K大数(转)
传统解法,最直观的解法是O(m+n).直接merge两个数组,然后求第K大的数字. 如果想要时间复杂度将为O(log(m+n)).我们可以考虑从K入手.如果我们每次能够删除一个一定在第K个元素之前的元 ...
- TELNET模拟HTTP请求
开启nginx服务,查看服务器地址(192.168.11.119) 使用telnet命令连接服务器的80端口 http协议报文格式 1.request 2.response 输入请求行: GET / ...
- ExtJs的Ext.grid.GridPanel不能选择复制表格中的内容解决方案
今天遇到grid复制的问题,在网上找到了一个解决办法,只需改下CSS和JS,给大家分享一下: 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/dy_paradise/a ...
- (三)storm-kafka源代码走读之怎样构建一个KafkaSpout
上一节介绍了config的相关信息,这一节说下,这些參数各自是什么.在zookeeper中的存放路径是如何的,之前QQ群里有非常多不知道该怎么传入正确的參数来new 一个kafkaSpout,其主要还 ...
- BufferedReader 使用 readLine() 读取 UTF-8 格式的文本第一行第一个字符是空字符的解决办法
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(ksmgVo.getFiledata( ...
- mysql忘记root密码解决
修改配置文件:my.cnf 加上skip-grant-tables 重启mysql mysql -uroot 登录 mysql> USE mysql ; mysql> UPDATE u ...
- Android6.0系统添加那些新特性
北京时间9月30日凌晨在美国旧金山举行2015年秋季新品公布会.在公布会上代号为"Marshmallow(棉花糖)"的安卓6.0系统正式推出.新系统的总体设计风格依旧保持扁 ...
- 在集群中使用文件加载graph
从hdfs上加载文件并创建graph scala> var graphs = GraphLoader.edgeListFile(sc,"/tmp/dataTest/graphTest. ...
- VMware Mac OS补丁安装
安装了VMware9.0在新建虚拟系统的时候,没有Appel MAC OS系统的选项,上网查了一下是需要打一个VMware Mac OS补丁就可以了.下面我来演示一下VMware Mac OS补丁怎么 ...
- Viewer 是一款强大的 jQuery 图像浏览插件。
Viewer 是一款强大的 jQuery 图像浏览插件. 主要功能: 支持选项 支持方法 支持事件 支持触摸 支持移动 支持缩放 支持旋转 支持键盘 跨浏览器支持 链接: viewer的官方演示,及g ...