CodeForces 977F Consecutive Subsequence (贪心+DP)

本文详细解析了CodeForces平台上的977F题目,采用贪心算法结合动态规划(DP)的方法进行高效求解。通过定义状态转移方程和优化搜索路径,实现了对给定序列最长递增子序列的有效求解。

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

题目链接:http://codeforces.com/problemset/problem/977/F

贪心性质+DP

#include<bits/stdc++.h>
using namespace std;

#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long

#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define fi first
#define se second
#define mk(x,y) make_pair(x,y)
const int mod=1e9+7;
const int maxn=3e5+100;
const int ub=1e6;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){
    if(y==0) return x;
    return gcd(y,x%y);
}
int n,a[maxn],nxt[maxn],dp[maxn];
int idx=1,maxv=1;
map<int,pii> mp;
void output(int x){
    if(x==0) return ;
    output(nxt[x]);
    cout<<x<<" ";
}
int main(){
    mst(nxt,-1);
    cin>>n;
    rep(i,1,n+1){
        cin>>a[i];
        dp[i]=mp[a[i]-1].fi+1;
        if(dp[i]>maxv){
            maxv=dp[i];
            idx=i;
        }
        nxt[i]=mp[a[i]-1].se;
        mp[a[i]].fi=dp[i];
        mp[a[i]].se=i;
    }
    cout<<maxv<<endl;
    output(idx);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值