首页 > 其他分享 >2024牛客2I Red Playing Cards

2024牛客2I Red Playing Cards

时间:2024-07-21 16:09:57浏览次数:11  
标签:int long 2024 牛客 cards include Playing Red define

本文同步于我的博客

Problem

There are \(2\cdot n\) cards arranged in a row, with each card numbered from \(1\) to \(n\) having exactly 2 copies.

Each time, Red can choose a subarray of consecutive cards (at least \(2\) 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?

给你一个长度为 \(2n\) 的数组,\(1\) 到 $ n$ 每个数字恰好出现两次。你可以进行这样的操作:选择两个相同的数字 \(x\) (必须都还存在于数组中),将这两个数以及其间的所有数字(共计 \(cnt\) 个)全部拿走,并获得 \(x\cdot cnt\) 得分。求最终最多能够获得多少分?

\(1\le n\le 3\times 10^3\)

Solution

我们发现基本有这三种情况:

  • 相离。比如1 1 3 313互不影响。
  • 包含。比如1 3 3 1,可以先拿3-3再拿1-1,也可以直接一次拿1-1,此时3没有任何贡献。
  • 相交。比如1 3 1 3,如果拿了1-1就不能再拿3-3了,拿了3-3也不能再拿1-1

设 \(f(i)\) 为 \([l_i,r_i]\) 这段区间的最大得分。\(l_i,r_i\) 分别指第一个 \(i\) 和第二个 \(i\) 的位置。

先假设 \([l_i,r_i]\) 中每个数字的贡献都是 \(i\),而如果遇到了另一个区间 \([l_j,r_j]\) 满足 \((l_i<l_j<r_j<r_i)\),那么考虑使用 \(f(j)\) 来代替这一个子区间的贡献。

这里一定有 \(len_j<len_i\),所以我们按照区间长度 \(len_i\) 排序来计算 \(f(i)\)

那么如何计算 \(f(i)\) 呢?

设 \(g(k)\) 表示,在计算 \(f(i)\) 时,区间 \([l_i,k]\) 的最大贡献。

  • 对于一般情况而言,\(g(k)=g(k-1)+i\)。
  • 而如果当前 \(k\) 是某个数字 \(j\) 的第二次出现的地方,且这个数字第一次出现的地方 \(l_j\in[l_i,k]\),那么需要考虑有可能先抹去 \(j\) ,也就是取 \([l_j,r_j]\) 得分为 \(f(j)\),会使得答案更优,。

\[\begin{gather} g(k)=\left\{ \begin{array}{l} \max \left(g(k-1)+i, g\left(l_j-1\right)+f(j)\right), \text { if } k=r_j \text { and } l_j>l_i \\ g(k-1)+i, \text { otherwise } \end{array}\right. \end{gather} \]

而我们需要的 \(f(i)=g(r_i)\)。

为了得到整个数组的得分,这里有个trick。我们在数组前后添加两个0,求 \(f(0)\) 即可。

时间复杂度 \(O(n^2)\)。

Code

/**************************************************************
 * Problem: 
 * Author: Vanilla_chan
 * Date: 
 * E-Mail: [email protected]
 **************************************************************/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<limits.h>
#define IL inline
#define re register
#define LL long long
#define ULL unsigned long long
#ifdef TH
#define debug printf("Now is %d\n",__LINE__);
#else
#define debug
#endif
#ifdef ONLINE_JUDGE
char buf[1<<23],* p1=buf,* p2=buf,obuf[1<<23],* O=obuf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
#endif
using namespace std;



#define N 6010

int n;
int a[N],l[N],r[N],len[N];
bool cmp(int x,int y)
{
	return len[x]<len[y];
}
vector<int>p;
int f[N],g[N];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cout.precision(10);
	int t=1;
//	cin>>t;
	while(t--)
	{
		cin>>n;
		for(int i=1;i<=n*2;i++)
		{
			cin>>a[i];
			if(l[a[i]])
			{
				r[a[i]]=i;
				len[a[i]]=i-l[a[i]]+1;
			}
			else l[a[i]]=i;
		}
		l[0]=0,r[0]=2*n+1,len[0]=2*n+2;
		for(int i=0;i<=n;i++)
		{
			p.push_back(i);
		}
		sort(p.begin(),p.end(),cmp);
//		for(int x=0;x<=n;x++) cout<<p[x]<<" "; cout<<endl;
		for(auto x:p)
		{
			for(int k=l[x];k<=r[x];k++)
			{
				g[k]=g[k-1]+x;
				int y=a[k];
				if(k==r[y]&&l[y]>l[x])
				{
					g[k]=max(g[k],g[l[y]-1]+f[y]);
				}
			}
			f[x]=g[r[x]];
			for(int k=l[x];k<=r[x];k++) g[k]=0;
//			cout<<"f["<<x<<"]="<<f[x]<<endl;
		}
		cout<<f[0]<<endl;
		
	}
	return 0;
}

标签:int,long,2024,牛客,cards,include,Playing,Red,define
From: https://www.cnblogs.com/Vanilla-chan/p/18314571

相关文章

  • DASCTF 2024暑期挑战赛-WEB-Sanic's revenge gxngxngxn
    DASCTF-WEB-Sanic'srevengegxngxngxn写在开篇碎碎念在我上篇文章(https://www.cnblogs.com/gxngxngxn/p/18205235)的结尾,我分享了两点我在寻找污染链的过程中发现的一些新玩意。其中作为本题考点之一的file_or_directory就在其中,没看过的师傅可以看一眼(orz。而本题主要考点的......
  • 2024最新AI创作系统,ChatGPT商业运营系统,AI绘画系统源码,AI视频生成系统,AI智能体、文档
    一、人工智能人工智能技术正在迅速发展,AI语言模型、AI绘画、AI视频在多个领域都有广泛的应用。它们不仅在科技创新方面表现出色,还在艺术创作、内容生产和商业应用中展现出巨大的潜力。AI语言模型可以用于自动化内容生成、智能客服、文本翻译等方面,大大提升了工作效率和用户体......
  • 失败笔记本--HALCON--009--202407
    失败笔记本-HALCON篇-009项目场景:今天还是和大佬学习的一天,今儿个学习怎么识别图中的颜色块,实现下面的效果:识别到图中的颜色块的颜色,并显示在图中。参考大神的链接:halcon入门教程10_颜色识别11.通过颜色灰度识别图中的颜色步骤:上来我就是一套丝滑小连招啊,打开图片,......
  • 2024.7.21 鲜花
    兜兜兜兜兜兜——articles下面是翻译杀兜兜兜兜兜兜传说有个魔仙堡兜杀杀兜兜兜兜有个女王不得了兜兜兜兜杀兜兜兜每个魔仙得她指导逼杀兜兜兜兜兜兜都盼望世界更美好兜杀兜兜杀兜兜兜变大变小真的奇妙兜兜杀杀兜兜兜逼一个咒语一个符号兜兜兜兜杀杀兜兜兜......
  • Adobe InCopy 2024 v19.5 (macOS, Windows) - 编写和副本编辑软件
    AdobeInCopy2024v19.5(macOS,Windows)-编写和副本编辑软件Acrobat、AfterEffects、Animate、Audition、Bridge、CharacterAnimator、Dimension、Dreamweaver、Illustrator、InCopy、InDesign、LightroomClassic、MediaEncoder、Photoshop、PremierePro、AdobeXD......
  • Adobe InDesign 2024 v19.5 (macOS, Windows) - 版面设计和桌面出版软件
    AdobeInDesign2024v19.5(macOS,Windows)-版面设计和桌面出版软件Acrobat、AfterEffects、Animate、Audition、Bridge、CharacterAnimator、Dimension、Dreamweaver、Illustrator、InCopy、InDesign、LightroomClassic、MediaEncoder、Photoshop、PremierePro、Adob......
  • 【2024最新华为OD-C/D卷试题汇总】[支持在线评测] LYA的生日派对座位安排(200分) - 三
    ......
  • 2024杭电1003 HDOJ7435 树
    本文同步发布于我的网站Problem给一棵根为1的有根树,点\(i\)具有一个权值\(A_i\)。定义一个点对的值\(f(u,v)=\max\left(A_u,A_v\right)\times\left|A_u-A_v\right|\)。你需要对于每个节点\(i\),计算\(ans_i=\sum_{u\in\operatorname{subtree}(i),v\in\op......
  • NOI2024 集合 题解
    给个链接:集合。很神秘的题目。基本上看到之后就可以想到哈希。首先想到一个比较神秘的暴力。就是对于每个询问,扫一遍所有\(a\)中的数出现的位置,把它弄成一个哈希值(具体怎么弄随意)存到set里,然后看看是不是和\(b\)中的数出现的位置这样操作后的集合完全相等。事实上就是判断......
  • 2024最新子比主题源码zibll-V7.9(含教程) | WordPress主题
    内容目录一、详细介绍二、效果展示1.部分代码2.效果图展示三、学习资料下载一、详细介绍2024最新Zibll子比主题V7.9版本源码开心版|WordPress主题安装教程在压缩包内V7.7更新日志:新功能新增数字翻页输入页码跳转的功能(注:总页数超过8页才会显示)新增后台......