LightOJ 1085 - All Possible Increasing Subsequences (离散化+树状数组+dp)

本文解析了1085号问题——所有可能的递增子序列,这是一道经典的算法题。通过动态规划方法,文章详细介绍了如何计算给定序列中所有递增子序列的数量,并提供了完整的代码实现。
1085 - All Possible Increasing Subsequences
Time Limit: 3 second(s)Memory Limit: 64 MB

An increasing subsequence from a sequence A1, A2 ... An is defined by Ai1, Ai2 ... Aik, where the following properties hold

  1. i1 < i2 < i3 < ... < ik and
  2. 2.      Ai1 < Ai2 < Ai3 < ... < Aik

Now you are given a sequence, you have to find the number of all possible increasing subsequences.

Input

Input starts with an integer T (≤ 10), denoting the number of test cases.

Each case contains an integer n (1 ≤ n ≤ 105) denoting the number of elements in the initial sequence. The next line will contain n integers separated by spaces, denoting the elements of the sequence. Each of these integers will be fit into a 32 bit signed integer.

Output

For each case of input, print the case number and the number of possible increasing subsequences modulo 1000000007.

Sample Input

Output for Sample Input

3

3

1 1 2

5

1 2 1000 1000 1001

3

1 10 11

Case 1: 5

Case 2: 23

Case 3: 7

Notes

  1. For the first case, the increasing subsequences are (1), (1, 2), (1), (1, 2), 2.
  2. Dataset is huge, use faster I/O methods.

 

题目大意:

  就是说,给你一个长度为n的序列,然后,让你求出这个序列中所有的上升子序列的个数

解题思路:

  直接把tree[i]当做dp[i]来用,表示的是以a[i]结尾的上升序列的最大个数

代码:

 1 # include<iostream>
 2 # include<cstdio>
 3 # include<algorithm>
 4 # include<cstring>
 5 
 6 using namespace std;
 7 
 8 # define MAX 500000+4
 9 # define MOD 1000000007
10 
11 typedef long long LL;
12 
13 LL a[MAX];
14 LL data[MAX];
15 int tree[MAX];
16 int n,cc;
17 
18 void discrete()
19 {
20     memset(data,0,sizeof(data));
21     for ( int i = 0;i < n;i++ )
22     {
23         data[i] = a[i];
24     }
25     sort(data,data+n);
26     cc = unique(data,data+n)-data;
27     for ( int i = 0;i < n;i++ )
28     {
29         a[i] = 1+lower_bound(data,data+cc,a[i])-data;
30     }
31 }
32 
33 
34 void update ( int pos,int val )
35 {
36     while ( pos <= n )
37     {
38         tree[pos]=(tree[pos]+val)%MOD;
39         pos += pos&(-pos);
40     }
41 }
42 
43 LL read ( int pos )
44 {
45     LL sum = 0;
46     while ( pos>0 )
47     {
48         sum=(sum+tree[pos])%MOD;
49         pos-=pos&(-pos);
50     }
51     return sum%MOD;
52 }
53 
54 
55 int main(void)
56 {
57     int icase = 1;
58     int t;cin>>t;
59     while ( t-- )
60     {
61         while ( cin>>n )
62         {
63         if ( n==0 )
64             break;
65         LL ans = 0;
66         for ( int i = 0;i < n;i++ )
67         {
68             cin>>a[i];
69         }
70         discrete();
71         memset(tree,0,sizeof(tree));
72         for ( int i = 0;i < n;i++ )
73         {
74             update(a[i],read(a[i]-1)+1);
75         }
76         printf("Case %d: ",icase++);
77         cout<<read(cc)%MOD<<endl;
78         }
79     }
80 
81     return 0;
82 }

 

转载于:https://www.cnblogs.com/wikioibai/p/4457209.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值