There is a sequence X (i.e. x[1], x[2], ..., x[n]). We define increasing subsequence of X
as x[i1], x[i2],...,x[ik], which satisfies follow conditions:
1) x[i1] < x[i2],...,<x[ik];
2) 1<=i1 < i2,...,<ik<=n
As an excellent program designer, you must know how to find the maximum length of the
increasing sequense, which is defined as s. Now, the next question is how many increasing
subsequence with s-length can you find out from the sequence X.
For example, in one case, if s = 3, and you can find out 2 such subsequence A and B from X.
1) A = a1, a2, a3. B = b1, b2, b3.
2) Each ai or bj(i,j = 1,2,3) can only be chose once at most.
Now, the question is:
1) Find the maximum length of increasing subsequence of X(i.e. s).
2) Find the number of increasing subsequence with s-length under conditions described (i.e. num).
as x[i1], x[i2],...,x[ik], which satisfies follow conditions:
1) x[i1] < x[i2],...,<x[ik];
2) 1<=i1 < i2,...,<ik<=n
As an excellent program designer, you must know how to find the maximum length of the
increasing sequense, which is defined as s. Now, the next question is how many increasing
subsequence with s-length can you find out from the sequence X.
For example, in one case, if s = 3, and you can find out 2 such subsequence A and B from X.
1) A = a1, a2, a3. B = b1, b2, b3.
2) Each ai or bj(i,j = 1,2,3) can only be chose once at most.
Now, the question is:
1) Find the maximum length of increasing subsequence of X(i.e. s).
2) Find the number of increasing subsequence with s-length under conditions described (i.e. num).
InputThe input file have many cases. Each case will give a integer number n.The next line will
have n numbers.OutputThe output have two line. The first line is s and second line is num.Sample Input
4 3 6 2 5Sample Output
2 2
最长上升子序列。
代码:
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <map> #define Max 1000 using namespace std; int n,s[Max],vis[Max];///vis标记是否已经使用 int maxl() { int res = 0,t[Max]; for(int i = 0;i < n;i ++) { if(vis[i])continue; if(!res || t[res - 1] < s[i]) { t[res ++] = s[i]; vis[i] = 1; } else { // *lower_bound(t,t + res,s[i]) = s[i]; int l = 0,r = res,mid; while(l < r) { mid = (l + r) / 2; if(t[mid] >= s[i])r = mid; else l = mid + 1; } t[l] = s[i]; } } return res; } int main() { while(scanf("%d",&n) != EOF) { for(int i = 0;i < n;i ++) { scanf("%d",&s[i]); } memset(vis,0,sizeof(vis)); int m = maxl(),c = 1; while(maxl() == m)c ++; printf("%d\n%d\n",m,c); } }
如果觉得有帮助,点个推荐啦~
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· 程序员常用高效实用工具推荐,办公效率提升利器!
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 【译】WinForms:分析一下(我用 Visual Basic 写的)