codeforce 977 F. Consecutive Subsequence

F. Consecutive Subsequence

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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+k?1][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 (1≤n≤2?1051≤n≤2?105) — the length of the array. The second line of the input containing nn integer numbers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤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

Note

All valid answers for the first example (as sequences of indices):

  • [1,3,5,6][1,3,5,6]
  • [2,3,5,6][2,3,5,6]

All valid answers for the second example:

  • [1,4][1,4]
  • [2,5][2,5]
  • [3,6][3,6]

All valid answers for the third example:

  • [1][1]
  • [2][2]
  • [3][3]
  • [4][4]

All valid answers for the fourth example:

  • [1,2,3,7,8,9]

题意:给你一个数组找出最长的递增子序列的长度以及下标位置。

例如第一组样例:7

3 3 4 7 5 6 8

最长的子序列为3 4 5 6,长度为4。

下标为1 3 5 6或2 3 5 6

题解:用map进行动态规划,mp[i]表示以i数字开头的最长的递增子序列的长度,即有转移方程mp[i]=max(mp[i],mp[i-1]+1)

最后找出map里面子序列长度最长的数字i,倒着输出就行了。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
using namespace std;
const int inf=0x3f3f3f3f;  

map<int ,int >dp;
int t[300005];
stack<int>s;
int main()
{
    int n;
    scanf("%d",&n);
    int ans=-inf;
    int maxx;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&t[i]);
        dp[t[i]]=dp[t[i]-1]+1;
        if(dp[t[i]]>ans)
        {
            ans=dp[t[i]];
            maxx=t[i];
        }
    }
    printf("%d\n",ans);
    for(int i=n;i>=1;i--)
    {
        if(t[i]==maxx)
        {
            s.push(i);
            maxx--;
        }
    }
    for(int i=0;i<ans;i++)
    {
        if(i!=0)printf(" ");
        printf("%d",s.top());
        s.pop();
    }
    printf("\n");
}  

原文地址:https://www.cnblogs.com/caiyishuai/p/9021456.html

时间: 2024-11-03 23:10:46

codeforce 977 F. Consecutive Subsequence的相关文章

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

Consecutive Subsequence (DP+map)

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 [

最大连续子序列和问题(Maximum Consecutive Subsequence Sum)

该算法的定义是:给出一个int序列,元素有正有负,找出其中的最大连续子序列的和. 例如:-2,11,-4,13,-5-2,:最大和为20(11,-4, 13). 怎么考虑这个问题呢? 要充分利用,连续,这个条件. 连续子序列的和可能为正,也可能为负.如果为正,那么我们要继续加下去,因为如果后面一个数是正数, 最大子序列和必定包括前序列的和(毕竟前者和为正),如果后面一个是负数,我们也加,直到子序列的和不为正为止. 因为它不为正,所以如果后面有更大的连续子序列,那么也不会包括它,因为加上一个非正数

LeetCode 659: Split Array into Consecutive Subsequence

Note: 1. If it does not belong any sequences : append.getOrDefault(num, 0) == 0, create a new sequence. It requires num + 1 and num + 2 count > 0. 2. Once it has sequence, move sequece to num + 1. class Solution { public boolean isPossible(int[] nums

Codeforces Round #479 (Div. 3) 题解

Codeforce官方给的题解 传送门 A. Wrong Subtraction 交题的传送门:http://codeforces.com/contest/977/problem/A 题意:题意很简单,就是给你一个数,如果能够整除10就直接将该数缩小10倍:否则直接将这个数减一.问k此操作之后,这个数n变为多少? 题解:直接按照题意模拟即可QAQ 代码如下: 1 //A 2 #include<bits/stdc++.h> 3 using namespace std; 4 5 int main(

Codeforces Round #479 (Div. 3)解题报告

题目链接: http://codeforces.com/contest/977 A. Wrong Subtraction 题意 给定一个数x,求n次操作输出.操作规则:10的倍数则除10,否则减1 直接写,手速题,没啥好说的 B. Two-gram 题意 求出现次数最多的连续两个字符 还是签到题,我居然很麻烦地用了map,= =算了,思路畅通都无所谓了 #include <iostream> #include<stdio.h> #include<algorithm> #

CodeForces div3 第一场

A Wrong Subtraction 题意: 对于一个数操作n次,操作如下: 如果末尾是0就将这个数除以10, 如果末尾不是0就将这个数-1, 直接做就好了. 代码: 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout

Hdu 5776 sum

sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 3295    Accepted Submission(s): 1210 Problem Description Given a sequence, you're asked whether there exists a consecutive subsequence whose

hdu-5776 sum(同余)

题目链接: sum Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 131072/131072 K (Java/Others) Problem Description Given a sequence, you're asked whether there exists a consecutive subsequence whose sum is divisible by m. output YES, otherwise outp