codeforces-977F-Consecutive Subsequence【动态规划】

codeforces-977F-Consecutive Subsequence

传送门:codeforces-977F-Consecutive Subsequence

求最长连续上升子序列,输出序列各元素的位置

刚开始没看懂是要连续,果断打了个LIS,wa了,然后又在上面做了改动,还是不对,老老实实重写dp吧

定义dp[x]是以x为结尾的最长串的长度

状态转移方程dp[a[i]]=dp[a[i]-1]+1;

a[i]最大是1e9,数组存不下,可以用map

上代码

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 const int N=1e6+9;
 5 const int inf=0x3f3f3f3f;
 6 int a[N];
 7 map<int,int>dp;
 8 int main()
 9 {
10     int n,k=0,ans;
11     scanf("%d",&n);
12     for(int i=1;i<=n;i++)
13     {
14         scanf("%d",&a[i]);
15     }
16     for(int i=1;i<=n;i++)
17     {
18         dp[a[i]]=dp[a[i]-1]+1;
19         if(dp[a[i]]>k)
20         {
21             k=dp[a[i]];
22             ans=a[i];
23         }
24     }
25     printf("%d\n",k);
26     int cnt=ans-k+1;
27     for(int i=1;i<=n;i++)
28     {
29         if(a[i]==cnt)
30         {
31             printf("%d ",i);
32             cnt++;
33         }
34     }
35     printf("\n");
36     return 0;
37 }

猜你喜欢

转载自www.cnblogs.com/YangKun-/p/12569299.html