[ABC446D] Max Straight 题解

AT_abc446_d [ABC446D] Max Straight

题目描述

You are given an integer sequence $ A=(A_1,A_2,\ldots,A_N) $ of length $ N $ .

Find the maximum length of a subsequence $ B=(B_1,B_2,\ldots,B_{|B|}) $ of integer sequence $ A $ that satisfies the following condition.

  • $ B_i+1=B_{i+1} $ for all integers $ i $ satisfying $ 1\le i\le |B| - 1 $ .

    What is a subsequence A subsequence of sequence $ A $ is a sequence obtained by choosing zero or more elements of $ A $ to delete, and arranging the remaining elements in their original order.

输入格式

The input is given from Standard Input in the following format:

$ N $ $ A_1 $ $ A_2 $ $ \ldots $ $ A_N $

输出格式

Output the answer.

输入输出样例 #1

输入 #1

7
3 4 3 5 7 6 2

输出 #1

4

输入输出样例 #2

输入 #2

5
5 4 3 2 1

输出 #2

1

输入输出样例 #3

输入 #3

10
1 2 3 4 5 6 7 8 9 10

输出 #3

10

说明/提示

Sample Explanation 1

$ B=(3,4,5,6) $ is a subsequence of $ A $ that satisfies the condition, and its length is $ 4 $ .

There is no subsequence satisfying the condition with length greater than $ 4 $ , so output $ 4 $ .

Constraints

  • $ 1\le N\le 2\times 10^5 $
  • $ 1\le A_i\le 10^9 $
  • All input values are integers.

思路

水题,直接AC。

代码见下

#include<bits/stdc++.h> 
using namespace std;
long long n,a[200005],f[200005],op=0;
map<long long,long long> mp;
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<=n;i++){
		f[i]=mp[a[i]-1]+1;
		mp[a[i]]=max(mp[a[i]],f[i]);
		op=max(op,f[i]);
	}
	cout<<op<<endl;
	return 0; 	
}
posted @ 2026-02-22 16:23  bz02_2023f2  阅读(3)  评论(0)    收藏  举报  来源