Tenzing and Balls

news/2025/2/14 6:15:54/

题面翻译

有一个大小为 n n n 的数组 a a a。你可以进行下列操作任意多次:

选择 i i i j j j,使 1 ≤ i < j ≤ ∣ a ∣ 1\leq i \lt j \leq |a| 1i<ja 并且 a i = a j a_i=a_j ai=aj
从数组中删除 a i , a i + 1 , … , a j a_i,a_{i+1},…,a_{j} ai,ai+1,,aj
(并将 a j a_j aj 右边的所有元素的索引减少 j − i + 1 j−i+1 ji+1)。

求出能删除数的最大数量。

注: ∣ a ∣ |a| a 表示 a a a 数组的大小。

题目描述

Enjoy erasing Tenzing, identified as Accepted!

Tenzing has $ n $ balls arranged in a line. The color of the $ i $ -th ball from the left is $ a_i $ .

Tenzing can do the following operation any number of times:

  • select $ i $ and $ j $ such that $ 1\leq i < j \leq |a| $ and $ a_i=a_j $ ,
  • remove $ a_i,a_{i+1},\ldots,a_j $ from the array (and decrease the indices of all elements to the right of $ a_j $ by $ j-i+1 $ ).

Tenzing wants to know the maximum number of balls he can remove.

输入格式

Each test contains multiple test cases. The first line of input contains a single integer $ t $ ( $ 1\leq t\leq 10^3 $ ) — the number of test cases. The description of test cases follows.

The first line contains a single integer $ n $ ( $ 1\leq n\leq 2\cdot 10^5 $ ) — the number of balls.

The second line contains $ n $ integers $ a_1,a_2,\ldots,a_n $ ( $ 1\leq a_i \leq n $ ) — the color of the balls.

It is guaranteed that sum of $ n $ of all test cases will not exceed $ 2\cdot 10^5 $ .

输出格式

For each test case, output the maximum number of balls Tenzing can remove.

样例 #1

样例输入 #1

2
5
1 2 2 3 3
4
1 2 1 2

样例输出 #1

4
3

提示

In the first example, Tenzing will choose $ i=2 $ and $ j=3 $ in the first operation so that $ a=[1,3,3] $ . Then Tenzing will choose $ i=2 $ and $ j=3 $ again in the second operation so that $ a=[1] $ . So Tenzing can remove $ 4 $ balls in total.

In the second example, Tenzing will choose $ i=1 $ and $ j=3 $ in the first and only operation so that $ a=[2] $ . So Tenzing can remove $ 3 $ balls in total.

分析

设a[i]==a[j],且假设j<i,那么[j,i]段可以去除
dp[i]表示前i位最大可去除的个数
那么dp[i]=max(dp[i-1],dp[j-1]+i-j+1)
可以看出式子中dp[j-1]-j这一块未知
使得dp[i]最大,即使dp[j-1]-j最大,使用mx来存储
对于a[i]来说dp[j-1]-j --> dp[i-1]-i
mx[a[i]]=max(mx[a[i]],dp[i-1]-i);
代码:

#include<bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int main()
{int T;cin>>T;while(T--){int n; cin >> n;vector<int> a(n + 1);vector<int> dp(n + 1);vector<int> mx(n + 1, -INF);for (int i = 1; i <= n; i++){scanf("%d", &a[i]);}for(int i=1;i<=n;i++){dp[i]=max(dp[i-1],mx[a[i]]+i+1);mx[a[i]]=max(mx[a[i]],dp[i-1]-i);}cout<<dp[n]<<endl;}
}

http://www.ppmy.cn/news/1039475.html

相关文章

合宙Air724UG LuatOS-Air script lib API--wifiRil

wifiRil Table of Contents wifiRil wifiRil.regRsp(head, fnc, typ, formt) wifiRil.regUrc(prefix, handler) wifiRil.deRegUrc(prefix) wifiRil.request(cmd, arg, onrsp, delay, param) wifiRil 模块功能&#xff1a;esp8266 wifi模块AT命令交互管理 wifiRil.regRsp(head,…

龙迅LT9711 2PORT MIPI或者LVDS转TYPE-C

LT9711 1.描述&#xff1a; Lontium LT9711是双端口MIPI/LVDS到DP1.2转换器&#xff0c;内部有c型替代模式开关和PD控制器。MIPI DSI/CSI输入具有可配置的单端口或双端口&#xff0c;具有1个时钟通道&#xff0c;1个~4个数据通道&#xff0c;最大运行2Gbps/通道&#xff0c;可…

【Java开发实战攻关】「JPA技术专题」带你一同认识和使用JPA框架进行开发你的应用服务

带你一同认识和使用JPA框架进行开发你的应用服务 什么是JPA框架JPA持久层框架使用JPA实现持久化JPA注解介绍按类别划分的 JPA注解实体注解Entity模型注解Table示例-显示了如何使用此批注指定主表名。 注解TableGeneratorTableGenerator主要属性strategystrategy属性可以是下列枚…

【Linux】一切皆文件

Linux 下一切皆为文件&#xff0c; 文件包括头文件&#xff0c;库文件&#xff08;静态库和共享库&#xff09;&#xff0c;可执行文件&#xff0c;目录文件&#xff0c;软链接文件&#xff0c;配置文件等。 每个文件都依据权限分为用户、用户组和其他人三个身份&#xff0c;…

2023年4大收银系统软件排名(真实测评)

现在满大街的各种服装店、便利店、百货店、母婴店...... 每天都要处理大量的订单。 使用传统的人工开单记账&#xff0c;效率低下、客户体验差、而且容易出错&#xff0c;需要耗费很多时间来回对账&#xff1b; 聪明的零售店老板都已经开始使用收银系统软件&#xff0c;通过手…

二刷LeetCode--48. 旋转图像(C++版本),数学题

思路&#xff1a;主要是观察变化之后的数组和最开始的数组的区别&#xff0c;不难发现&#xff0c;先转置在左右镜像对称即可。需要注意的是转置和镜像对称中for变量的终止条件。 class Solution { public:void rotate(vector<vector<int>>& matrix) {// 行数…

步入React正殿 - React组件设计模式

目录 扩展学习资料 高阶组件 /src/components/hoc/withTooltip.js /src/components/hoc/itemA.jsx /src/components/hoc/itemB.jsx /src/App.js 函数作为子组件【Render pprops】 函数作为子组件 /src/components/rp/itemC.jsx【父组件】 /src/components/rp/withToo…

责任链模式简单实现

两种实现方式 第一种 public interface IBaseTask {public void doAction(String isTask,IBaseTask iBaseTask); }public class ChainManager implements IBaseTask{//工作类的集合private List<IBaseTask> iBaseTaskList new ArrayList<>();public void addTas…