Codeforces 597C 子序列
题面
【题目描述】
给你一个包含n个不同元素的序列,让你求出在这个序列中有多少个长度为k+1的上升子序列。保证答案不会超过8*10^18。
【输入描述】
第一行包括两个正整数n和k(1<=n<=10^5,0<=k<=10)
接下来n行每行包括一个正整数ai(1<=ai<=n),所有ai都是不同的。
【输出描述】
输出一个整数,表示这个问题的答案。
【样例输入】
5 2
1
2
3
5
4
【样例输出】
7
题解
树状数组优化\(O(kn \log n)\)求不下降子序列数.
算是补了ISN那一道题的坑吧.
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
namespace Zeonfai
{
inline long long getInt()
{
long long a = 0, sgn = 1;
char c;
while(! isdigit(c = getchar()))
if(c == '-')
sgn *= -1;
while(isdigit(c))
a = a * 10 + c - '0', c = getchar();
return a * sgn;
}
}
const long long N = (long long)1e5;
struct binaryIndexTree
{
long long a[N + 1];
inline void clear()
{
memset(a, 0, sizeof(a));
}
inline void modify(long long u, long long dta, long long bnd)
{
for(; u <= bnd; u += u & - u)
a[u] += dta;
}
inline long long query(long long u)
{
long long res = 0;
for(; u; u -= u & -u)
res += a[u];
return res;
}
}BIT;
int main()
{
#ifndef ONLINE_JUDGE
freopen("CF597C.in", "r", stdin);
#endif
using namespace Zeonfai;
long long n = getInt(), k = getInt();
static long long a[N];
for(long long i = 0; i < n; ++ i)
a[i] = getInt();
static long long f[N];
for(long long i = 0; i < n; ++ i)
f[i] = 1;
for(long long i = 0; i < k; ++ i)
{
BIT.clear();
for(long long j = 0; j < n; ++ j)
BIT.modify(a[j], f[j], n), f[j] = BIT.query(a[j] - 1);
}
long long ans = 0;
for(long long i = 0; i < n; ++ i)
ans += f[i];
printf("%lld", ans);
}
Codeforces 597C 子序列的更多相关文章
- codeforces 597C (树状数组+DP)
题目链接:http://codeforces.com/contest/597/problem/C 思路:dp[i][j]表示长度为i,以j结尾的上升子序列,则有dp[i][j]= ∑dp[i-1][k ...
- Codeforces 597C. Subsequences (树状数组+dp)
题目链接:http://codeforces.com/contest/597/problem/C 给你n和数(1~n各不同),问你长为k+1的上升自序列有多少. dp[i][j] 表示末尾数字为i 长 ...
- CodeForces - 597C Subsequences 【DP + 树状数组】
题目链接 http://codeforces.com/problemset/problem/597/C 题意 给出一个n 一个 k 求 n 个数中 长度为k的上升子序列 有多少个 思路 刚开始就是想用 ...
- codeforces 597C C. Subsequences(dp+树状数组)
题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...
- CodeForces - 597C Subsequences (树状数组+动态规划)
For the given sequence with n different elements find the number of increasing subsequences with k + ...
- codeforces 597C - Subsequences
枚举子序列的末尾,递推. 方案数:f[i = 以i结尾][k =子序列长度] = sum(f[j][k-1]),j < i. 转移就建立k个BIT. #include<bits/stdc+ ...
- CodeForces - 597C:Subsequences (主席树+DP)
For the given sequence with n different elements find the number of increasing subsequences with k + ...
- codeforces mysterious present 最长上升子序列+倒序打印路径
link:http://codeforces.com/problemset/problem/4/D #include <iostream> #include <cstdio> ...
- Codeforces Round #345 (Div. 1) D. Zip-line 上升子序列 离线 离散化 线段树
D. Zip-line 题目连接: http://www.codeforces.com/contest/650/problem/D Description Vasya has decided to b ...
随机推荐
- 1250 Fibonacci数列(矩阵乘法快速幂)
1250 Fibonacci数列 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 定义:f0=f1=1, f ...
- Python中__str__和__repr__的区别
Python有一个内置的函数叫repr,它能把一个对象用字符串的形式表达出来以便辨认,这就是“字符串表示形式”.repr就是通过__repr__这个特殊方法来得到一个对象的字符串表示形式.如果没有实现 ...
- MSSQL将多行单列变一行一列并用指定分隔符分隔,模拟Mysql中的group_concat
-- 将多行记录(只能一个列)用指定分隔符分隔 IF(OBJECT_ID('sp_RowsChangeClosBySplit',N'P') IS NOT NULL) DROP PROC sp_Rows ...
- 连接Oracle 10g时ORA-12514:TNS:监听进程不能解析在连接描述符中给出的SERVICE_NAME错误的解决
近日服务器断电,导致客户端连接ORACLE服务器时出现ORA-12514错误,在网上查得解决方法如下 解决方法: 1. 打开/network/admin/listener.ora文件,找到: SID_ ...
- 数据结构之c++感悟
#include<iostream.h> 头文件: #include<iostream.h> #define MAX 1024 typedef int Elemtype; ty ...
- HLG1125 循环小数2
循环小数 II Time Limit: 1000 MS Memory Limit: 65536 K Total Submit: 155(55 users) Total Accepted: 92(51 ...
- git 远程仓库回滚
git branch backup #创建备份分支 git push origin backup:backup #push到远程 git reset --hard commit_id #本地分支回滚 ...
- Hadoop入门第四篇:手动搭建自己的hadoop小集群
前言 好几天没有更新了,本来是应该先写HDFS的相关内容,但是考虑到HDFS是我们后面所有学习的基础,而我只是简单的了解了一下而已,后面准备好好整理HDFS再写这块.所以大家在阅读这篇文章之前,请先了 ...
- 修改host文件实现自定义域名和iis站点本地调试
修改host文件实现自定义域名和iis站点本地调试 自定义域名:myhost.com windows版本:win7 iis版本:iis7.x vs版本:vs2010 现在开始动手设置了: 一.修改ho ...
- BZOJ 3462 DZY Loves Math II ——动态规划 组合数
好题. 首先发现$p$是互质的数. 然后我们要求$\sum_{i=1}^{k} pi*xi=n$的方案数. 然后由于$p$不相同,可以而$S$比较小,都是$S$的质因数 可以考虑围绕$S$进行动态规划 ...