题目链接

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 + 树状数组】的更多相关文章

  1. CodeForces - 597C Subsequences (树状数组+动态规划)

    For the given sequence with n different elements find the number of increasing subsequences with k + ...

  2. codeforces 597C C. Subsequences(dp+树状数组)

    题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...

  3. HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences             ...

  4. 树形DP+树状数组 HDU 5877 Weak Pair

    //树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...

  5. bzoj 1264 [AHOI2006]基因匹配Match(DP+树状数组)

    1264: [AHOI2006]基因匹配Match Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 793  Solved: 503[Submit][S ...

  6. 【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 ...

  7. 奶牛抗议 DP 树状数组

    奶牛抗议 DP 树状数组 USACO的题太猛了 容易想到\(DP\),设\(f[i]\)表示为在第\(i\)位时方案数,转移方程: \[ f[i]=\sum f[j]\;(j< i,sum[i] ...

  8. [Codeforces 1208D]Restore Permutation (树状数组)

    [Codeforces 1208D]Restore Permutation (树状数组) 题面 有一个长度为n的排列a.对于每个元素i,\(s_i\)表示\(\sum_{j=1,a_j<a_i} ...

  9. CodeForces - 314C Sereja and Subsequences (树状数组+dp)

    Sereja has a sequence that consists of n positive integers, a1, a2, ..., an. First Sereja took a pie ...

随机推荐

  1. Maven 缺省内置变量

    1.${project.build.directory} 构建目录,缺省为target 2.${project.build.outputDirectory} 构建过程输出目录,缺省为target/cl ...

  2. Install RabbitMQ server in CentOS 7

    About RabbitMQ RabbitMQ is an open source message broker software, also sometimes known as message-o ...

  3. xcode 项目证书跟签名都正确的时候,还报证书错误

    原因,安装证书错误,导致无法匹配证书, 方案:删除原来的证书,重新安装 打开终端 1.cd Library/ 2.cd MobileDevice/ 3.open Provisioning\ Profi ...

  4. 介绍C#结构体与类区别

    1. 结构体与类定义方式 结构体定义使用struct类定义使用class 结构体: struct testDemo{ int num; void action(){ } } 类: class test ...

  5. Tomcat + Spring MVC + HttpClient:怎样使用PUT和PATCH方法传递数据

    在RESTful风格的API中.PUT/PATCH方法一般用于更新数据.在项目的代码中,使用的是HttpClient 4.5,是这样写的: protected JSONObject doHttpUri ...

  6. python常见面试题(二)

    1. 到底什么是Python?你可以在回答中与其他技术进行对比(也鼓励这样做). 下面是一些关键点: Python是一种解释型语言.这就是说,与C语言和C的衍生语言不同,Python代码在运行之前不需 ...

  7. sklearn函数白板

    #使用make_classification构造500个样本,每个样本有20个feature from sklearn.datasets import make_classification X, y ...

  8. php编译安装与配置

    php编译安装与配置 =========================================== 官网:http://php.net/ 官网下载:http://php.net/downlo ...

  9. mysql解压版安装和卸载

    问题1:发生系统错误 5. 解决:使用管理员身份安装即可 问题2:发生系统错误 2. 解决:cd C:\Program Files\MySQL\MySQL Server 5.6\bin 进入mysql ...

  10. object.Equals与object.ReferenceEquals方法

    object.Equals方法表达的是语义判等,不一定是引用判等. object.ReferenceEquals方法是肯定是引用判等. 怎么实现一个对象的值语义的 Equals方法?实验. MyCla ...