C. Tenzing and Balls
链接:https://codeforces.com/problemset/problem/1842/C or https://www.luogu.com.cn/problem/CF1842C
大概的思路就是利用dp[i]记录前i个数据最多消掉的数字个数,然后对∀j:a[i] == a[j] && j < i
进行dp[i] = dp[j-1] + i-j+1
的递推
优化:
代码:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<sstream>
#include<string>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<map>
#include<queue>
#include<limits.h>
#include<climits>
#include<fstream>
#include<stack>
typedef long long ll;
using namespace std;
const int N = 2e5 + 10;
ll dp[N],a[N],mp[N];
int main()
{
ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
ll t; cin >> t;
while (t--)
{
ll n; cin >> n;
memset(dp, 0, sizeof(dp));
memset(mp, -0x3f3f3f3f, sizeof(mp));//这个初始设置成最小
for (ll i = 1; i <= n; i++)
{
ll x; cin >> x;
a[i] = x;
}
dp[1] = 0;//dp:消掉的数量
for (ll i = 1; i <= n; i++)
{
//dp[i] = max(dp[j - 1] + i - j + 1,dp[i]);
dp[i] = max(dp[i - 1], mp[a[i]] + i);
mp[a[i]] = max(mp[a[i]], dp[i - 1] - i + 1);
}
cout << dp[n]<<'\n';
}
return 0;
}