CF 977 F. Consecutive Subsequence

题意:

第一场div3, 求的是一个序列中最长连续(a,a+1,a+2...)子序列。

分析:

设一个DP[i] 表示 序列以i结尾的最长长度, 一开始都设为0。

那么如果这个数是a, 他的最长长度就是 Dp[a-1] + 1, 最后找出最大那个值就是答案, 倒回去输出序列就可以了

#include <bits/stdc++.h>
using namespace std;
const int maxN = 2e5 + 7;
int a[maxN];
map<int, int> dp;
int main(){
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    int maxCnt = -1, last;
    for(int i = 1;i <= n; i++){
        int temp;
        cin >> temp;
        a[i] = temp;
        dp[temp] = dp[temp-1] + 1;
        if(dp[temp] > maxCnt){
            maxCnt = dp[temp];
            last = temp;
        }
    }
    cout << maxCnt << "
";
    int b = last;
    vector<int> ans;
    for(int i = n; i >= 1; i--){
        if(a[i] == last){
            ans.push_back(i);
            last--;
        }
    }
    reverse(ans.begin(), ans.end());
    for(int i = 0; i < ans.size(); i++){
        cout << ans[i] << ' ';
    }
}
原文地址:https://www.cnblogs.com/Jadon97/p/9005890.html