codeforces 597C C. Subsequences(dp+树状数组)
题目链接:
1 second
256 megabytes
standard input
standard output
For the given sequence with n different elements find the number of increasing subsequences with k + 1 elements. It is guaranteed that the answer is not greater than 8·10^18.
First line contain two integer values n and k (1 ≤ n ≤ 10^5, 0 ≤ k ≤ 10) — the length of sequence and the number of elements in increasing subsequences.
Next n lines contains one integer ai (1 ≤ ai ≤ n) each — elements of sequence. All values ai are different.
Print one integer — the answer to the problem.
Examples
5 2
1
2
3
5
4
7 题意: 问在有n个不同的数组成的序列中,有k+1个数的递增子序列的个数是多少; 思路:
k不大n也不大看起来好像是dp,假设dp[i][j]表示在前i个数中长度为j的递增子序列并且a[i]是这个序列的最后一位的个数;
ans[j]=dp[1][j]+dp[2][j]+...+dp[n][j];
dp[i][j]=dp[x][j-1](x为按a[]序列中数值比它小且在它前面的)
举一个例子:第一行为输入的a[]第二行为最后一个为递增子序列且最后位为a[i]的个数,再把第二行做成一个树状数组再查询,k-1次后就得到结果
| 6 | 10 | 9 | 7 | 1 | 2 | 8 | 5 | 4 | 3 | ans |
| 0 | 1 | 1 | 1 | 0 | 1 | 4 | 2 | 2 | 2 | 14 |
| 0 | 0 | 0 | 0 | 0 | 0 | 2 | 1 | 1 | 1 | 5 |
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+;
int a[N],n,k;
long long dp[N],sum[N],b[N];
int lowbit(int x)
{
return x&(-x);
}
void update(int x,long long num)
{
while(x<=n)
{
sum[x]+=num;
x+=lowbit(x);
}
}
long long query(int x)
{
long long s=;
while(x>)
{
s+=sum[x];
x-=lowbit(x);
}
return s;
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
{
b[i]=;
scanf("%d",&a[i]);
dp[i]=query(a[i]);
update(a[i],b[i]);
}
for(int i=;i<=k;i++)
{
memset(sum,,sizeof(sum));
for(int j=;j<=n;j++)
{
b[j]=dp[j];
dp[j]=query(a[j]);
update(a[j],b[j]);
}
}
long long ans=;
for(int i=;i<=n;i++)
{
ans+=dp[i];
}
if(!k)cout<<n<<"\n";//注意k==0的情况
else cout<<ans<<"\n";
return ;
}
codeforces 597C C. Subsequences(dp+树状数组)的更多相关文章
- HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences ...
- Codeforces 777E(离散化+dp+树状数组或线段树维护最大值)
E. Hanoi Factory time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- 树形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 - 597C Subsequences 【DP + 树状数组】
题目链接 http://codeforces.com/problemset/problem/597/C 题意 给出一个n 一个 k 求 n 个数中 长度为k的上升子序列 有多少个 思路 刚开始就是想用 ...
- CodeForces - 314C Sereja and Subsequences (树状数组+dp)
Sereja has a sequence that consists of n positive integers, a1, a2, ..., an. First Sereja took a pie ...
- Codeforces 909C Python Indentation:树状数组优化dp
题目链接:http://codeforces.com/contest/909/problem/C 题意: Python是没有大括号来标明语句块的,而是用严格的缩进来体现. 现在有一种简化版的Pytho ...
随机推荐
- MVC5 网站开发之二 创建项目
昨天对项目的思路大致理了一下,今天先把解决方案建立起来.整个解决包含Ninesky.Web.Ninesky.Core,Ninesky.DataLibrary等3个项目.Ninesky.Web是web应 ...
- Javascript 中 with 的替代方案和String 中的正则方法
这几天在升级自己的MVVM 框架,遇到很多小问题,就在这里统一解决了. with 语法 在代码中,要执行这么一个函数 function computeExpression(exp, scope) { ...
- centos7查看系统版本,查看机器位数x86-64
前言 由于不经常使用linux,每当使用的时候就是安装软件,安装软件的时候就要选择安装包平台,是32位的还是64位的.这时候突然发现不知道怎么查,于是百度.虽然轻而易举百度出来,但仍旧没有自己的笔记看 ...
- Node基础篇(概要)
Node简介 客户端的JavaScript是怎样的 什么是 JavaScript? 脚本语言 运行在浏览器中 一般用来做客户端页面的交互(Interactive) JavaScript 的运行环境? ...
- Nancy之大杂烩
Nancy关于Hosting的简单介绍 一. Nancy之基于Nancy.Hosting.Aspnet的小Demo 二.Nancy之基于Nancy.Hosting.Self的小Demo 三.Nancy ...
- C++进程间通信
# C++进程间通信 # 进程间通讯的四种方式:剪贴板.匿名管道.命名管道和邮槽 ## 剪切板 ## //设置剪切板内容 CString str; this->GetDlgItemText(ID ...
- ASP.NET Core模块概述
原文地址:ASP.NET Core Module overview By Tom Dykstra, Rick Strahl, and Chris Ross ASP.NET Core模块(ANCM)让你 ...
- Java Swing interview
http://www.careerride.com/Swing-AWT-Interview-Questions.aspx Swing interview questions and answers ...
- dbutils基本使用
dbutils的查询,主要用到的是query方法,增加,修改和删除都是update方法,update方法就不讲了 只要创建ResultSetHandler接口不同的实现类对象就可以得到想要的查询结果, ...
- WaitGroup is reused before previous Wait has returned
当你Add()之前,就Wait()了,就会发生这个错误.