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 ...
随机推荐
- tomcat使用redis存储共享session
在tomcat集群环境下实现session共享有几种解决方式,这里介绍一种简单的方案. 使用redis对session进行存储,配置比較简单.webserver是tomcat6 1.下载jar包: c ...
- 【Excle数据透视表】如何得到数据透视表中某个汇总行的明细数据
例如: 现在想得到"北京 汇总"的明细数据,该怎么处理呢? 步骤 右键数据透视表任意单元格→数据透视表选项→启用显示明细数据→确定→单击"北京 汇总"行最后一个 ...
- IIS7.5下的web.config 404应该如何配置
IIS环境下web.config的配置的问题,在IIS7.5中添加配置404页面时遇到了一些问题,记录如下: 一开始在<customError>下的<error>节点配置404 ...
- Database returned an invalid value in QuerySet.datetimes(). Are time zone definitions for your datab
Database returned an invalid value in QuerySet.datetimes(). Are time zone definitions for your datab ...
- 如果你报createSQLQuery is not valid without active transaction,请看这里
原文:https://blog.csdn.net/yinjian520/article/details/8666695 很多时候我们使用hibernate的session时,都是让session在某一 ...
- 【Lucene】Apache Lucene全文检索引擎架构之构建索引2
上一篇博文中已经对全文检索有了一定的了解,这篇文章主要来总结一下全文检索的第一步:构建索引.其实上一篇博文中的示例程序已经对构建索引写了一段程序了,而且那个程序还是挺完善的.不过从知识点的完整性来考虑 ...
- UICollectionView的header悬停
UICollectionView的header悬停,继承UICollectionViewFlowLayout,重写相关方法 // // StickyHeaderLayout.h // Wombat / ...
- presentModalViewController方法,present一个透明的viewController,带动画效果
//假设需要被present的控制器实例为controller,controller的背景色设置为clearColor UIViewController * rootcontroller = self ...
- nginx实现某个页面http访问,其余全部跳转到https
全站https实现某个页面可以http访问,其余全部跳转到https,注意下面的location,如果不加root 配置 找不到这个文件的server { listen ; server_name w ...
- stage3D基础二-----顶点和片段着色器(转)
来源:http://www.adobe.com/cn/devnet/flashplayer/articles/vertex-fragment-shaders.html 本教程将介绍着色器.着色器是 S ...