题目链接

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. close_wait状态的产生原因及解决(转)

    最近测试环境server由于需要与大量的后台server交互,今天突然发现有大量的close_wait产生,于是仔细研究了一下: 如果我们的服务器程序处于CLOSE_WAIT状态的话,说明套接字是被动 ...

  2. hadoop:WordCount问题总结

    今天玩了一下hadoop的MapReduce,中途遇到了几个问题,在此记录一下. 1.一切按照配置完成之后,hadoop namenode format,start-all.sh启动,使用jps查看进 ...

  3. MYSQL总结之sql语句大全

    一.基础1.说明:创建数据库 CREATE DATABASE database-name .说明:删除数据库 drop database dbname .说明:备份sql server --- 创建 ...

  4. ubuntu下设置静态ip

    直接修改系统配置文件ubuntu的网络配置文件是:/etc/network/interfacesubuntu命令行修改网络配置方法前面auto  eth?,让网卡开机自动挂载. 为网卡配置静态IP地址 ...

  5. UIWebView 加载网页、文件、 html

    UIWebView  是用来加载加载网页数据的一个框.UIWebView可以用来加载pdf word doc 等等文件 生成webview 有两种方法,1.通过storyboard 拖拽 2.通过al ...

  6. 排序算法 C++代码实现

    插入排序: 就像摸牌,摸一张插进去,找一个哨兵.从第二个開始,和前一个比較.小的话前移一位. #include <iostream> #include<stdlib.h> us ...

  7. SVN 创建仓库操作

    服务端安装完成后 1.创建一个存放仓库的文件夹(这里在home目录创建) #mkdir svnRepo #cd svnRepo/ 创建一个仓库 (写全路径) # svnadmin create /ro ...

  8. Android IntentService全然解析 当Service遇到Handler

    转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/47143563: 本文出自:[张鸿洋的博客] 一 概述 大家都清楚.在Andro ...

  9. HTML5 2D平台游戏开发#10Wall Jump

    这个术语不知道怎么翻译比较贴切,但并不妨碍对字面意思的理解,大概就是飞檐走壁.比如: 这是游戏<忍者龙剑传>中的场景,玩家可以通过操纵角色在墙面上移动并跳跃. 首先需要实现角色抓墙这一动作 ...

  10. applicationContext-XXX.xml和XXX-servlet.xml的区别

    1.ApplicationContext.xml  是spring 全局配置文件,用来控制spring 特性的 2.dispatcher-servlet.xml 是spring mvc里面的,控制器. ...