题目链接

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. JavaScript类数组转换为数组 面试题

    1.JavaScript类数组转换为数组 (1)方法一:借用slice (2)方法二:Array.from 2.代码 <!DOCTYPE html> <html lang=" ...

  2. hdu4888 多校B 最大流以及最大流唯一推断+输出方案

    题意.给一个矩阵,告诉你每行和.每列和.而且限制所填数不大于k,问矩阵是否唯一. 经典建图不说了.第一次遇到推断最大流唯一性的.学习了:用dfs来推断残网中是否还存在环,若存在,则表明绕这个环走一圈, ...

  3. STL学习笔记(string)

    动机 C++标准程序库中的string class使我们可以将string当做一个一般型别.我们可以像对待基本型别那样地复制.赋值和比较string, 再也不必但系内存是否足够.占用的内存实际长度等问 ...

  4. 【BIEE】08_修改浏览器标题栏显示内容

    打开分析,我们可以看到标题栏中显示的BIEE默认的,现在想要把这个修改为自定义的 打开文件路径: D:\obiee\Oracle_BI1\bifoundation\web\msgdb\l_zh-CN\ ...

  5. JDBC编程理论知识(1)

    1.SUN公司为统一对数据库的操作,定义了一套Java操作数据库的规范,称之为JDBC 2.JDBC全称为:Java Data Base Connectivity(java数据库连接),它主要由接口组 ...

  6. 面向对象-Object类

    一.Object类中的equals()方法 equals(Object obj) :指示其它某个对象是否与此对象"相等". 返回值类型是boolean Oblect类中的equal ...

  7. JavaScript 中的命名空间

    全局变量应该由有系统范围相关性的对象们保留,并且它们的命名应该避免含糊并尽量减少命名冲突的风险.在实践中,这意味着你应该避免创建全局对象,除非它们是绝对必须的. 所以你对此是怎么做的?传统方法告诉我们 ...

  8. Java IO 常用类简介

    字节流 输入字节流 InputStream输入字节流的抽象类 ByteArrayInputStreambyte数组输入流 FileInputStream文件输入流 PipedInputStream管道 ...

  9. distcc加速内核编译

    Linux内核编译实在是费时间的事,搞内核移植的时候总要编译,生命有一部分就浪费在等内核编译完成上,有心想买个HP的工作站,看了下Z840的价格,想想还是算了.distcc早就听说过,一直没有去试试, ...

  10. SQL_为表和列加凝视

    ***********************************************声明*************************************************** ...