zoukankan      html  css  js  c++  java
  • 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] << ' ';
        }
    }
  • 相关阅读:
    Mybatis中javaType和jdbcType对应关系
    spy日志
    mybatis批量插入和更新
    js打印方案
    js弹窗,父子窗口调用
    extjs4.1
    oracle开启远程连接访问
    javaweb打印
    Leetcode 392.判断子序列
    Leetcode 391.完美矩形
  • 原文地址:https://www.cnblogs.com/Jadon97/p/9005890.html
Copyright © 2011-2022 走看看