Gym 101194C / UVALive 7899 - Mr. Panda and Strips - [set][2016 EC-Final Problem C]
题目链接:
http://codeforces.com/gym/101194/attachments
题意:
一个长度为 $N$ 的序列,要求选出两段不重叠的区间,要求两个区间包含的元素均互不相同,求两段区间的长度和最大为多少。
题解:
(主要参考https://blog.csdn.net/a1214034447/article/details/78768645)
首先用 $back[i]$ 和 $front[i]$ 分别表示 $i$ 这个位置的数从i往右看第一次出现的位置,和往左看第一次出现的位置。
接着,我们从位置 $n$ 开始倒退回去枚举右区间的左端点 $r$,用 $rlen = back[r] - r$ 表示当前位置往右延伸的最长长度,并且用一个set保存目前右区间 $[r,back[r])$ 里所有的数。
然后,对于每个 $r$,均枚举左区间的右端点 $l(l<r)$,用 $llen = l - front[l]$ 表示目前该位置往左延伸的最长长度。
同时,再用一个set维护:找到左区间里,所有在右区间出现过的数,存储这些数的下标。
接下来依次枚举这些数,考虑这些数若不让其在右区间取到(当然,还有一种情况是都在右区间取),那么可以算出此时相应左区间最长能有多长。将左右区间长度求和,维护最大值。
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+;
const int maxc=1e5+; int n,m;
int num[maxn],pos[maxc];
int bak[maxn],frt[maxn];
set<int> st,se; inline int maxllen(int p,int l,int llen)
{
auto it=se.end();
while((it--)!=se.begin())
if(pos[num[*it]]<p) return l-(*it);
return llen;
} int main()
{
int T;
cin>>T;
for(int kase=;kase<=T;kase++)
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&num[i]); memset(pos,,sizeof(pos));
for(int i=;i<=n;i++)
{
frt[i]=pos[num[i]];
pos[num[i]]=i;
}
memset(pos,0x3f3f3f3f,sizeof(pos));
for(int i=n;i>=;i--)
{
bak[i]=pos[num[i]];
pos[num[i]]=i;
} int ans=;
st.clear();
for(int i=n,rlen=;i>=;i--,rlen++)
{
while(bak[i] <= i+rlen-)
{
st.erase(num[i+rlen-]);
rlen--;
}
st.insert(num[i]), pos[num[i]]=i;
se.clear();
for(int j=,llen=;j<i;j++,llen++)
{
while(frt[j] >= j-llen+)
{
se.erase(j-llen+);
llen--;
}
if(st.count(num[j])) se.insert(j);
if(!se.size()) ans=max(ans,llen+rlen);
else ans=max(ans,j-*(--se.end())+rlen);
for(auto it=se.begin();it!=se.end();it++)
ans=max(ans,maxllen(pos[num[*it]],j,llen)+pos[num[*it]]-i);
}
}
printf("Case #%d: %d\n",kase,ans);
}
}
Gym 101194C / UVALive 7899 - Mr. Panda and Strips - [set][2016 EC-Final Problem C]的更多相关文章
- Codeforces Gym 101194C Mr. Panda and Strips(2016 EC-Final,区间DP预处理 + 枚举剪枝)
题目链接 2016 EC-Final 题意 现在要找到数列中连续两个子序列(没有公共部分).要求这两个子序列本身内部没有重复出现的数. 求这两个子序列的长度的和的最大值. 首先预处理一下.令$ ...
- Gym 101194D / UVALive 7900 - Ice Cream Tower - [二分+贪心][2016 EC-Final Problem D]
题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...
- Gym 101194H / UVALive 7904 - Great Cells - [数学题+快速幂][2016 EC-Final Problem H]
题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...
- Gym 102056L - Eventual … Journey - [分类讨论][The 2018 ICPC Asia-East Continent Final Problem L]
题目链接:https://codeforces.com/gym/102056/problem/L LCR is really an incredible being. Thinking so, sit ...
- H - Mr. Panda and Birthday Song Gym - 101775H (动态规划)
Mrs. Panda’s birthday is coming. Mr. Panda wants to compose a song as gift for her birthday. It is k ...
- hdu6007 Mr. Panda and Crystal 最短路+完全背包
/** 题目:hdu6007 Mr. Panda and Crystal 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6007 题意:魔法师有m能量,有n ...
- 2018 China Collegiate Programming Contest Final (CCPC-Final 2018)-K - Mr. Panda and Kakin-中国剩余定理+同余定理
2018 China Collegiate Programming Contest Final (CCPC-Final 2018)-K - Mr. Panda and Kakin-中国剩余定理+同余定 ...
- Gym 102056I - Misunderstood … Missing - [DP][The 2018 ICPC Asia-East Continent Final Problem I]
题目链接:https://codeforces.com/gym/102056/problem/I Warm sunshine, cool wind and a fine day, while the ...
- Codeforces Gym 101775D Mr. Panda and Geometric Sequence(2017-2018 ACM-ICPC Asia East Continent League Final,D题,枚举剪枝)
题目链接 ECL-Final 2017 Problem D 题意 给定$2*10^{5}$组询问,每个询问求$l$到$r$之间有多少个符合条件的数 如果一个数小于等于$10^{15}$, 并且能被 ...
随机推荐
- 每天一个linux命令:free
1.命令简介 free (free) 命令可以显示Linux系统中空闲的.已用的物理内存及swap内存,及被内核使用的buffer. 2.用法 free [-b | -k | -m | -g | -h ...
- linux 通过nvm安装node
官方介绍:https://github.com/creationix/nvm#installation PS:通常不要用root权限安装软件,因为线上任何服务部署都不允许用root,其他软件用root ...
- “5W1H”带你来学习JavaScript
上次的设计模式讲课,从中学习到了非常多.不仅是技术上,更重要的是怎样来学习.我们学习的技术.科技的更新速度超过我们的想象,对于我们这个有生命年限的个体,怎样可以在有生之年可以让自己立足于科技的不败浪潮 ...
- docker的/var/lib/docker/overlay文件夹突然占满了硬盘
场景描述 事情是这样的,前两天客户的服务器突然断电了,导致用docker部署的服务也挂了.昨天去现场,重启了docker,同时准备更新一下服务.结果发现有一台节点硬盘满了- 还是觉得有些奇怪的,毕竟d ...
- [转]decorator(HTML装饰器)
原文地址:https://blog.csdn.net/jzh440/article/details/7770013 1>:每当遇到一个新的技术,首先我会问自己,这个技术是做神马的?用这个技术有神 ...
- Docker入门简记
Docker的容器环境实际上是借助类Linux命名空间,将各种系统资源按照容器不同划分了不同的命名空间进行隔离,为各个进程提供独立的运行环境关键概念:容器,镜像两个概念一起看,镜像好比平常系统中的各个 ...
- go: writing stat cache:, permission denied
sudo chown -R $(whoami):admin /Users/zhushuyan/go/pkg && sudo chmod -R g+rwx /Users/zhushuya ...
- win10 caffe python Faster-RCNN训练自己数据集(转)
一.制作数据集 1. 关于训练的图片 不论你是网上找的图片或者你用别人的数据集,记住一点你的图片不能太小,width和height最好不要小于150.需要是jpeg的图片. 2.制作xml文件 1)L ...
- virtualbox 在物理机是无线网卡的时候做桥接配置
在“计算机”图标上右键选择“管理”,在打开的“计算机管理”窗口中选择左侧的“设备管理器”,然后在右侧图示的地方右键选择“添加过时硬件”. 在打开的窗口中点击“下一步”. 选择“安装我手动从列表中选择的 ...
- sqlyog一些快捷键
http://zhidao.baidu.com/link?url=q5GI6myyUENGkDKfGsz-4P01kbdkJBeSgPFvV3HeNjTTvh9QhTXYx5W0xdcS1P7qxmA ...