[Thinking] [STL] Codeforces Round #479 (Div. 3) F. Consecutive Subsequence (the longest continuous increasing subsequence)

 Link: http://codeforces.com/problemset/problem/977/F

 

The meaning of the question: first enter n and then enter n
  numbers to find the longest continuous ascending subsequence among the n numbers

  The output has two lines

  The first line is the length of the longest ascending subsequence, the second line is the subscript of each element in the sequence

 

Because it is an ascending subsequence and is continuous, you can use map to store map[i] = map[i-1]+1;

In this way, after we process the input data, we can use the iterator to traverse the maximum value of map[i] and the value of i (map->second map->first)

The value of i is the value of the last number of the longest ascending subsequence and the maximum value of map[i] is the first value at the beginning of the length sequence = i-max(map[i])+1;

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 #define ll long long
 6 #define max3(a,b,c) fmax(a,fmax(b,c))
 7 #define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
 8 
 9 bool cmp(const int &a,const int &b)
10 {
11     return a>b;
12 }
13 const int maxn = 2e6+10;
14 
15 int main()
16 {
17     map<int,int> m;
18     int n;
19     int a[maxn];
20     cin >> n;
21     for(int i = 1;i <= n;i++)
22     {
23         scanf("%d",&a[i]);
24         m[a[i]] = m[a[i]-1]+1;
25     }
26     int mmax = -999;
27     int flag = 0;
28     map<int,int>::iterator it;
29     for(it = m.begin();it != m.end();it++)
30     {
31         if(it->second > mmax)
32         {
33             mmax = it->second;
34             flag = it->first;
35         }
36     }
37     flag -= mmax;
38     ++flag;
39     printf("%d\n",mmax);
40     for(int i = 1;i <= n;i++)
41     {
42         if(a[i] == flag)
43         {
44             printf("%d ",i);
45             ++flag;
46         }
47     }
48     printf("\n");
49     return 0;
50 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325945691&siteId=291194637