题目链接:

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. (原)Max Area of Island(即连通域标记)

    转载请注明出处: https://www.cnblogs.com/darkknightzh/p/10493114.html 1. 问题 Given a non-empty 2D array grid ...

  2. python两个 list 获取交集,并集,差集的方法

    1. 获取两个list 的交集 #方法一: a=[2,3,4,5] b=[2,5,8] tmp = [val for val in a if val in b] print tmp #[2, 5] # ...

  3. Chrome 控制台报错Unchecked runtime.lastError: The message port closed before a response was received

    Chrome浏览器控制台报错提示 Unchecked runtime.lastError: The message port closed before a response was received ...

  4. Java基础(四)线程快速了解

    开始整理线程之前,之前有个命令忘记整理了,先整理一下jar命令的使用 Jar包 其实可以理解是java的压缩包方便使用,只要在classpath设置jar路径即可数据库驱动,ssh框架等都是以jar包 ...

  5. 【iCore4 双核心板_ARM】例程二十六:LWIP_MODBUS_TCP实验——电源监控

    实验现象: 核心代码: int main(void) { system_clock.initialize(); led.initialize(); adc.initialize(); delay.in ...

  6. Caffe多线程环境下检测缓慢问题

    对于多线程运行环境以及Web框架下(其实也相当于多线程)Caffe运行缓慢的原因可能是仅在某一个线程中设置caffe.set_mode_gpu().但是该操作不会影响其他线程,此时其他线程还是CPU模 ...

  7. swiper4自动轮播切换手动触碰后停止踩坑——属性disableOnInteraction

    swiper4轮播设置autoplay自动切换后,即默认设置: <script> var mySwiper = new Swiper('.swiper-container', { auto ...

  8. 抖音圈圈乐 系统搭建H5微信小游戏圈圈乐系统介绍

    网红线下游戏抖音圈圈乐改造而来 一.搭建此系统需要准备如下资料: 1. 认证微信服务号 2. 微信支付商户号 3. 备案域名及云服务器 二.系统功能简介: 1. 游戏闯关 2. 每个商品闯关难度后台自 ...

  9. 【Postgres】空间数据库创建

    1.数据库创建问题-Navicat-ERROR: source database "template1" is being accessed by other users 2.解决 ...

  10. maven项目中使用redis集群报错: java.lang.NumberFormatException: For input string: "7001@17001"

    解决:由于redis集群的采用的版本是2.7的,在maven的pom.xml中将jedis的版本改成2.9的就可以了