题目链接:

http://codeforces.com/gym/101194/attachments

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5921

题意:

一个长度为 $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]的更多相关文章

  1. Codeforces Gym 101194C Mr. Panda and Strips(2016 EC-Final,区间DP预处理 + 枚举剪枝)

    题目链接  2016 EC-Final 题意  现在要找到数列中连续两个子序列(没有公共部分).要求这两个子序列本身内部没有重复出现的数.   求这两个子序列的长度的和的最大值. 首先预处理一下.令$ ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. hdu6007 Mr. Panda and Crystal 最短路+完全背包

    /** 题目:hdu6007 Mr. Panda and Crystal 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6007 题意:魔法师有m能量,有n ...

  7. 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-中国剩余定理+同余定 ...

  8. 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 ...

  9. 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}$, 并且能被 ...

随机推荐

  1. java logAspect

    @Around("execution(* com.iotx.cep.biz.rpc.impl.*.*(..)) " + "&& !execution(* ...

  2. 【转】最近很火的 Safe Area 到底是什么

    iOS 7 之后苹果给 UIViewController 引入了 topLayoutGuide 和 bottomLayoutGuide 两个属性来描述不希望被透明的状态栏或者导航栏遮挡的最高位置(st ...

  3. H+ 添加(新增)Tab选项卡

    //注:在contabs.js文件中 $(function () { }); 方法外 加入//注: data-name="' + menuName + '" 这句是加入的自定义属性 ...

  4. Unty中通过镜像优化HDRI全景图体积

    全景图即HDRI贴图,可以代替6面cubemap,传统3D软件运用较为广泛.一般反射探针,天空盒等都会用到. 但是体积过大是个问题,特别是移动端会对包体大小进行控制,虽说可以通过球面贴图替换掉部分环境 ...

  5. Win10 calc.exe 无法打开计算器的解决方法

    先将所有程序关闭,以管理员身份运行 Windows PowerShell,之后输入以下命令 Get-AppXPackage -AllUsers | Foreach {Add-AppxPackage - ...

  6. 【翻译】Nginx的反向代理

    本文为翻译文,原文地址:https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/ 本文描述代理服务器的基本配置.你能学到如何 ...

  7. VBA二次学习笔记(2)——两个Excel表内容比较

    说明(2018-9-3 22:38:58): 1. 就是之前问同事要来的作业,有两个格式一样的Excel文件,一个是正确答案,一个是员工作答的.通过代码将两个文件进行比对,把不同之处列出来. 正文: ...

  8. android6.0 Activity(四) Surface创建

     原文:http://blog.csdn.net/luoshengyang/article/details/8303098.原文代码比較老了,可是核心不变.在原文基础上改动了一些代码,以及增加自己 ...

  9. linux php --ini

    $ php --ini

  10. rpc error: code = Internal desc = stream terminated by RST_STREAM with error code: PROTOCOL_ERROR

    使用grpc-go调用grpc服务端时,出现rpc error: code = Internal desc = stream terminated by RST_STREAM with error c ...