指定长度递增子序列计数【CCPC C题】
来源:互联网 发布:生产跟单软件 编辑:程序博客网 时间:2024/06/04 19:17
题目链接:http://acm.uestc.edu.cn/#/problem/show/1217
这题赛场上过了一百多人,然而我们一点思路都没有,并不是我们实力不够,而是之前训练总是逃避DP,以至于比赛的时候这样的DP题一点思路都没有。
题意:给一个长度为n的数组,找出其中长度为m的递增子序列的个数,答案对10^9+7取余。
样例输入
2
3 2
1 2 3
3 2
3 2 1
样例输出
3
0
这题乍一看,第一感觉是一个DP加上组合数学的题目,那么既然是DP就要找到状态转移方程,最难的莫过于想到开这样一个数组dp[i][j]表示以第i个数结尾,长度为j的递增子序列的个数,其实如果想到这一步,离成功也不远了,同时我们可以否定一开始的判断,这题里面并没有组合数学。
当我们试图把第i个数放入子序列中形成以第i个数结尾,长度为j的递增子序列,便需要遍历所有以k(1~i-1)结尾的长度为j-1的,并且a[k]<a[i]的子序列的个数,并全部累加在dp[i][j]上。
代码如下:
/* * test.cpp * * Created on: 2015-10-21 * Author: Ivan_w */#include<iostream>#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>#include<stdlib.h>#include<queue>#include<stack>#include<map>#include<vector>#define mem(a) memset(a,0,sizeof(a))#define pfn printf("\n")#define ll __int64#define pfd(a) printf("%d\n",a)#define pf2d(a,b) printf("%d %d\n",a,b)#define pf3d(a,b,c) printf("%d %d %d\n",a,b,c)#define pfs(a) printf("%s\n",a)#define sfd(a) scanf("%d",&a)#define sf2d(a,b) scanf("%d%d",&a,&b)#define sf3d(a,b,c) scanf("%d%d%d",&a,&b,&c)#define sfs(a) scanf("%s",a)#define sfscanf#define pfprintf#define fr(i,n) for(int i=0;i<n;i++)#define wang int#define bo main#define byebye return 0#define bye returnconst double PI = acos(-1.0);const double e = exp(1.0);template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }template<class T> inline T Min(T a, T b) { return a < b ? a : b; }template<class T> inline T Max(T a, T b) { return a > b ? a : b; }bool cmpbig(int a,int b){return a>b;}bool cmpsmall(int a,int b){return a<b;}using namespace std;#define N 1000+10#define MOD 100000007int a[N];int dp[N][N];wang bo(){//freopen("1.txt","w",stdout);//freopen("1.txt","r",stdin);//freopen("2.txt","w",stdout);int t;while(~sfd(t)){for(int cas=1;cas<=t;cas++){printf("Case #%d: ",cas);int n,m;sf2d(n,m);for(int i=1;i<=n;i++){sfd(a[i]);}mem(dp);for(int i=1;i<=n;i++)//结尾为i{dp[i][1]=1;for(int j=2;j<=m;j++)//长度为j{for(int k=1;k<i;k++){if(a[k]<a[i])dp[i][j]+=dp[k][j-1];dp[i][j]%=MOD;}}}int ans=0;for(int i=1;i<=n;i++){ans+=dp[i][m];ans%=MOD;}pfd(ans);}}byebye;}
不过这个代码复杂度有点高,会超时,需要优化~~~暂时还不会~~~
0 0
- 指定长度递增子序列计数【CCPC C题】
- 【gcj 2008 1c】长数字串运算计数,递增子序列计数
- 求最长递增子序列的长度
- 最长递增子序列长度问题
- 求最长连续递增子序列长度
- HDU 1025 最大递增子序列长度
- 最长递增子序列的长度
- 最长递增子序列长度算法
- 最长递增子序列的长度
- 【算法】最长递增子序列的长度
- 最长单调递增子序列的长度
- 求解最长递增子序列的长度
- 求最长递增子序列的长度
- 最长递增子序列长度+最长公共子序列
- 最长递增子序列长度+最长公共子序列
- 求解最长递增子序列长度|动态规划+二分查找:C\C++实现
- NYOJ 236 心急的C小加(最长递增子序列长度)
- 查询序列中的最长递增子序列的长度
- Android 异步消息处理机制 让你深入理解 Looper、Handler、Message三者关系
- Java——继承,抽象,接口
- 创建第一个android 应用程序时,SDK和ADT一定要保持版本一致。
- 推荐外链网盘 OpenDrive
- NSOperation 之 Block循环引用——以及nil Nil NULL [NULL null]的区别
- 指定长度递增子序列计数【CCPC C题】
- 欢迎使用CSDN-markdown编辑器
- 基于Python的江苏大学校园网暴力破解
- nyoj 82 迷宫寻宝(一)(广搜)
- js正则
- MySQL数据库的同步配置+MySql读写分离
- request.getParameter() 和request.getAttribute() 区别
- DOM要点总结
- Leetcode -- Median of Two Sorted Arrays