I. The aesthetics of the sequence
Title link: https://ac.nowcoder.com/acm/contest/9983/I
Title description:
Suppose the aesthetics of a sequence S of length m is equal to how many integers i satisfy 1 ≤ i ≤ m−1 and S i = S i+1, where S i represents the i-th element of the sequence S.
Given a sequence a of length n, ask what is the maximum beauty of all its subsequences.
A subsequence of a sequence is a new sequence formed by removing some elements from the original sequence (or not removing, that is, the sequence itself is also a subsequence) but not destroying the relative position (before or after) of the remaining elements.
Enter a description:
The first line is a positive integer n.
Next line n positive integers ai separated by spaces.
2≤ n ≤10^6
1≤ ai ≤10^6
Output description:
Output an integer to represent the answer.
Example 1:
Input
5
1 1 2 3 2
Output
2
Description The most
beautiful subsequence is [1,1,2,2]
Example 2:
Input
7
1 1 2 2 2 1 1
Output
4
Problem-solving ideas:
dp
dp[i] represents the maximum aesthetics of the first i characters
①I don’t choose i/i is selected but i is different from the previous one dp[i] = dp[i-1]
②I is selected, and i is connected to the same as it (I is the same as the previous one) dp[i] = dp[pre[a[i]]] + 1 (pre[a[i]] is the position of the previous a[i].)
code show as below:
#include<iostream>
#include<cstdio>
using namespace std;
int dp[1000010],a[1000010],pre[1000010];
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=n;i++){
if(pre[a[i]] != 0)
dp[i]=max(dp[i-1],dp[pre[a[i]]]+1);
else
dp[i] = dp[i-1];
pre[a[i]] = i;
}
cout<<dp[n]<<endl;
return 0;
}