Consecutive Subsequence

本文介绍了一种求解整数数组中最长连续递增子序列的问题,通过使用map进行优化,实现高效的寻找过程,并提供了完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You are given an integer array of length nn.

You have to choose some subsequence of this array of maximum length such that this subsequence forms a increasing sequence of consecutive integers. In other words the required sequence should be equal to [x,x+1,,x+k1][x,x+1,…,x+k−1] for some value xx and length kk.

Subsequence of an array can be obtained by erasing some (possibly zero) elements from the array. You can erase any elements, not necessarily going successively. The remaining elements preserve their order. For example, for the array [5,3,1,2,4][5,3,1,2,4] the following arrays are subsequences: [3][3], [5,3,1,2,4][5,3,1,2,4], [5,1,4][5,1,4], but the array [1,3][1,3] is not.

Input

The first line of the input containing integer number nn (1n21051≤n≤2⋅105) — the length of the array. The second line of the input containing nn integer numbers a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109) — the array itself.

Output

On the first line print kk — the maximum length of the subsequence of the given array that forms an increasing sequence of consecutive integers.

On the second line print the sequence of the indices of the any maximum length subsequence of the given array that forms an increasing sequence of consecutive integers.

Examples
input
Copy
7
3 3 4 7 5 6 8
output
Copy
4
2 3 5 6
input
Copy
6
1 3 5 2 4 6
output
Copy
2
1 4
input
Copy
4
10 9 8 7
output
Copy
1
1
input
Copy
9
6 7 8 3 4 5 9 10 11
output
Copy
6
1 2 3 7 8 9
题解:dp[num]=dp[num-1]+1。因为num是离散的,所以可以使用map。
 1 #pragma warning(disable:4996)
 2 #include<map>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<iostream>
 6 #include<algorithm>
 7 using namespace std;
 8 #define ll long long
 9 
10 const int maxn = 200005;
11 
12 int n;
13 int a[maxn];
14 
15 int main()
16 {
17     while (scanf("%d", &n) != EOF) {
18         int ans = 0, end;
19         map<int, int> p;
20         for (int i = 1; i <= n; i++) {
21             scanf("%d", &a[i]);
22             p[a[i]] = max(p[a[i]], p[a[i] - 1] + 1);
23             if (p[a[i]] > ans) {
24                 ans = p[a[i]];
25                 end = a[i];
26             }
27         }
28 
29         cout << ans << endl;
30 
31         int start = end - ans + 1;
32         for (int i = 1; i <= n; i++) {
33             if (a[i] == start) {
34                 cout << i << endl;
35                 start++;
36             }
37         }
38     }
39     return 0;
40 }

 

转载于:https://www.cnblogs.com/zgglj-com/p/9005490.html

Sources operating in SPR mode Shallimplement over current protection to prevent damage from output current that exceeds the current handling capability of the Source. The definition of current handling capability is left to the discretion of the Source implementation and Shalltake into consideration the current handling capability of the connector contacts. If the over current protection implementation does not use a Hard Reset or Error Recovery, it Shall Notinterfere with the negotiated VBUScurrent level.After three consecutive over current events Source Shallgo to ErrorRecovery.Sources Shouldattempt to send Hard Resetsignalingwhen over current protection engages followed by an AlertMessage indicating an OCP event once an Explicit Contract has been established. The over current protection response Mayengage at either the port or system level. Systems or ports that have engaged over current protection Shouldattempt to resume USB Default Operation after determining that the cause of over current is no longer present and Maylatch off to protect the port or system. The definition of how to detect if the cause of over current is still present is left to the discretion of the Source implementation.The Source Shallrenegotiate with the Sink (or Sinks) after choosing to resume USB Default Operation . The decision of how to renegotiate after an over current event is left to the discretion of the Source implementation.The Source Shallprevent continual system or port cycling if over current protection continues to engage after initially resuming either USB Default Operation or renegotiation. Latching off the port or system is an acceptable response to recurring over current.During the over current response and subsequent system or port shutdown, all affected Source ports operating with VBUSgreater than vSafe5VShalldischarge VBUSto vSafe5Vby the time tSafe5Vand vSafe0Vby the time tSafe0V. 翻译上面的话
最新发布
07-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值