BC63 sequence2
来源:互联网 发布:贴吧顶贴软件手机版 编辑:程序博客网 时间:2024/04/29 04:21
问题描述
给定长度为nnn的序列bib_ibi,求有多少长度为kkk的本质不同的上升子序列。设该序列位置为a1,a2...aka_1, a_2 ... a_k一个序列为上升子序列,当且仅当a1<a2<...<aka_1 < a_2 < ... < a_k且ba1<ba2<...<bakb_{a_1} < b_{a_2} < ... < b{a_k}。本质不同当且仅当两个序列aa和AA存在一个iii使得ai≠Ai。
输入描述
若干组数据(大概555组)。每组数据第一行两个整数n(1≤n≤100),k(1≤k≤n)n(1 \leq n \leq 100), k(1 \leq k \leq n)。接下来一行nnn个整数bi(0≤bi≤109)b_i(0 \leq b_i \leq 10^{9})。
输出描述
对于每组的每个询问,输出一行。
输入样例
3 21 2 23 21 2 3
输出样例
23
我们可以通过dp[i][j]dp[i][j]]表示第ii个数,当前这个数为序列中的第jj个数的方案总数。转移为dp[i][j]=sumdp[k][j−1](k<i,bk<bi)dp[i][j] = sum{dp[k][j-1]}(k < i, b_k < b_i)。本题需要高精度。
这道题只需要用到大数加法就行了,
#include <stdio.h>#include <string>#include <iostream>using namespace std;const int maxn = 105;string dp[maxn][maxn], ch[maxn];//一维表示j-i的长度序列,二维表示长度int a[maxn];void swap ( string & ch ){ for ( int i = 0, j = ch.size ( )-1; i < j; i ++, j -- ) { char t = ch[i]; ch[i] = ch[j]; ch[j] = t; }}void Add ( string & str, const string ch ) //大数加法{ string s = ""; int i, j, carry = 0, t; for ( i = str.size ( )-1, j = ch.size ( )-1; i >= 0 && j >= 0; i --, j -- ) { t = ( str[i]-'0' )+( ch[j]-'0' )+carry; carry = t/10; t = t%10; s = s+( char )( t+'0' ); } while ( i >= 0 ) { t = ( str[i]-'0' )+carry; carry = t/10; t = t%10; s = s+( char )( t+'0' ); i --; } while ( j >= 0 ) { t = ( ch[j]-'0' )+carry; carry = t/10; t = t%10; s = s+( char )( t+'0' ); j --; } if ( carry > 0 ) //进位 s = s+( char )( carry+'0' ); swap ( s ); str = s;}int main ( ){ int n, k; string ans; while ( ~ scanf ( "%d%d", &n, &k ) ) { for ( int i = 1; i <= n; i ++ ) scanf ( "%d", &a[i] ); for ( int i = 1; i < maxn; i ++ ) { for ( int j = 1; j < maxn; j ++ ) dp[i][j] = "0"; dp[i][1] = "1"; } for ( int i = 1; i <= n; i ++ ) { for ( int j = 1; j < i; j ++ ) { if ( a[j] >= a[i] ) //找到前面比a[i]小的a[j] continue ; for ( int l = 2; l <= k; l ++ ) //将递增序列长度为2-k全部累加 Add ( dp[i][l], dp[j][l-1] ); } } ans = "0"; for ( int i = 1; i <= n; i ++ ) Add ( ans, dp[i][k] ); //加长度为的k的值加起来 cout << ans << endl; } return 0;}
0 0
- BC63 sequence2
- BC63 matrix
- hdu5568 sequence2
- hdu-5568-sequence2-dp
- hdu5568 sequence2 dp+大数
- hdoj 5568 sequence2 【dp + 高精度】
- HDU 5568:sequence2 大数+DP
- HDU 5568 sequence2(大数+DP)
- [HDU 5568] sequence2 dp+大数
- HDU5568 sequence2(dp+大数)
- hdu5568 sequence2(dp + 高精度)
- hdu5568 sequence2(dp+大数)
- HDU 5568 sequence2 DP+高精度
- BestCoder Round #63 (div.1) A.sequence2
- HDU-5568 sequence2(DP+高精度)
- hdu 5568 sequence2(dp + 大数)
- BestCoder Round #63 (div.2) HDOJ5568 sequence2(dp + 大数)
- 洛谷10月月赛R1-T2-一道简单题 Sequence2
- Linux文件权限与文件权限修改
- PHP入门(6)-控制语句
- 南大软院大神养成计划——第七天
- iOS 7应用开发公开课笔记 L5:View Controller Lifecycle
- 配置xml方式实现Spring的aop
- BC63 sequence2
- android时间精确到毫秒calendar
- Java单例集合下List接口的常用实现类
- 数据挖掘十大算法翻译——1决策树
- B+ Tree
- 开发板与虚拟机与windows互连
- 放几张漂亮的壁纸
- iOS 7应用开发公开课笔记 L6:Polymorphism with Controllers, UI
- 程序员为什么成就感很高