977F - Consecutive Subsequence - CodeForces Solution


dp *1700

Please click on ads to support us..

Python Code:

from collections import *
from math import inf
import math, os, sys, heapq, bisect, random
from functools import lru_cache
from itertools import *
from io import BytesIO, IOBase
def inp(): return sys.stdin.readline().rstrip("\r\n")
def out(var): sys.stdout.write(str(var))  def inpu(): return int(inp())
def lis(): return list(map(int, inp().split()))
def stringlis(): return list(map(str, inp().split()))
def sep(): return map(int, inp().split())
def strsep(): return map(str, inp().split())
def fsep(): return map(float, inp().split())
M,M1=1000000007,998244353
BUFSIZE = 8192
def main():
    how_much_noob_I_am = 1
        for __ in range(how_much_noob_I_am):
        n=inpu()
        arr = lis()
        ind = defaultdict(list)
        for i in range(n):
            ind[arr[i]].append(i+1)
        d=defaultdict(int)
        for i in range(n):
            d[arr[i]] = d[arr[i]-1]+1
        max1 = -inf
        res=-1
        for i in d:
            if d[i]>max1:
                res=i
                max1 = d[i]
        start = res-max1+1
        ans=[]
        req=-1
        for i in range(start,start+max1):
            req = bisect.bisect_left(ind[i],req)
            req = ind[i][req]
            ans.append(req)
        print(len(ans))
        print(*ans)
if __name__ == '__main__':
    main()

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
	int n;
	cin>>n;
	vector<ll> v(n+1);
	for(int i=1;i<=n;i++){
		cin>>v[i];
	}
	map<ll,pair<ll,ll>> m;
	ll parent[n+1]={0};
	for(ll i=1;i<=n;i++){
		if(m.count(v[i]-1)){
			ll temp=(m[v[i]-1].first)+1;
			m[v[i]]={temp,i};
			parent[i]=m[v[i]-1].second;
		}
		else{
			if(!m.count(v[i]))
			m[v[i]]={1,i};
		}
	}
	ll ans=1;
	ll i=0;
	for(auto it:m){
		if(ans<it.second.first){
			ans=it.second.first;
			i=it.second.second;
		}
	}
	cout<<ans<<endl;
	if(ans==1){
		cout<<1;
		return 0;
	}
	stack<ll> s;
	while(i!=0){
		s.push(i);
		i=parent[i];
	}
	while(!s.empty()){
		ll temp=s.top();
		cout<<temp<<" ";
		s.pop();
	}
	return 0;
}


Comments

Submit
0 Comments
More Questions

1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD
149A - Business trip
34A - Reconnaissance 2
59A - Word
462B - Appleman and Card Game
1560C - Infinity Table
1605C - Dominant Character
1399A - Remove Smallest
208A - Dubstep
1581A - CQXYM Count Permutations
337A - Puzzles