链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
题目描述
There are 2⋅n2\cdot n2⋅n cards arranged in a row, with each card numbered from 111 to nnn having exactly 222 copies.
Each time, Red can choose a subarray of consecutive cards (at least 222 cards) to remove from the deck. The chosen subarray must satisfy that the first and last cards have the same number. The score for this operation is: the number of cards multiplied by the number on the first card. Now Red wants to know what is the maximum value of the final score?
输入描述:
The first line contains a positive integer n(1≤n≤3×103)n(1\leq n \leq 3\times 10^3)n(1≤n≤3×103) — the number of card types. The second line contains 2⋅n2\cdot n2⋅n positive integers ai(1≤ai≤n)a_i(1\leq a_i \leq n)ai(1≤ai≤n) — the number on each card from left to right.
输出描述:
A single positive integer — representing the maximum final score.
示例1
输入
4 4 4 1 2 3 1 3 2
输出
21
说明
First operation: choose the 555-th to 777-th cards, get 999 points, and the remaining cards are [4,4,1,2,2]. Second operation: from the remaining cards, choose the 111-st to 222-nd cards, get 8 points, and the remaining cards are [1,2,2]. Third operation: from the remaining cards, choose the 222-nd to 333-rd cards, get 4points, and there is still 1 card remaining, so no more operations can be performed.
题目大意
给定一个长度为 2 × n 的数组,1 到 n 每个元素恰好出现两次
每次操作可以删除一个长度不小于 2 的连续子数组,需要满足该子数组
首尾相同,获得该连续子数组“首尾元素值”乘以“元素数量”的分数
问最终可以得到的最大分数
1 ≤ ai ≤ n ≤ 3000
样例g数组推导如下:
#include<bits/stdc++.h>
#define endl '\n'
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int N=3005;
int n;
int a[N*2];
int f[N];
int g[N*2];
map<int,int>fr,se;
signed main() {
IOS
cin>>n;
memset(a,0,sizeof a);
n++;
for(int i=2; i<n*2; i++) {
cin>>a[i];
}
for(int i=1; i<=n*2; i++) {
if(!fr[a[i]]) fr[a[i]]=i;
else if(!se[a[i]]) se[a[i]]=i;
}
for(int i=n-1; i>=0; i--) {
int L=fr[i],R=se[i];
for(int j=L-1; j<=R+1; j++) g[j]=0;
for(int j=L; j<=R; j++) {
g[j]=max(g[j-1]+i,g[j]);
if(a[j]<=i) continue;//当前的i和当前位置的a[j]相比,如果i大就直接继续
if(j!=se[a[j]]) continue;//当前j不是a[j]第二次出现的地方就继续
if(fr[a[j]]<L) continue;//a[j]第一次出现的地方在i第一次出现之前就继续
g[j]=max(g[fr[a[j]]-1]+f[a[j]],g[j]);//判断原来这个区间内的数据大还是当前区间的数据大
}
f[i]=g[R];
}
cout<<f[0]<<endl;
return 0;
}