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}$, 并且能被 ...
随机推荐
- sort is deprecated, use sort_values(inplace=True) for INPLACE sorting
排序是过时的,用sort_values(到位=真)为就地排序
- 积木式编程——自制app点灯
技术:51单片机.串口.蓝牙.积木式编程 概述 这里简单入门了积木式编程,做了个简易APP用来开灯关灯,当然你可以继续添加内容,可以做智能家居.遥控汽车和环境监控等等. 详细 代码下载:http: ...
- (原)Max Area of Island(即连通域标记)
转载请注明出处: https://www.cnblogs.com/darkknightzh/p/10493114.html 1. 问题 Given a non-empty 2D array grid ...
- 找不到指定的 VM 安装:类型 标准 VM,名称 jre7
问题背景是这样 原来使用的是jre7.0.55,后来为了安装使用 layabox IDE ,然后装了jdk_8u144 之后需要切换环境变量 之后java项目调试的时候重新设置了jdk,没问题. 直到 ...
- 射频与微波测量之S参数
转自:https://www.cnblogs.com/lyh523329053/p/9128577.html S参数 S散射也叫散射参数.是微波传输中的一组重要参数.由于我们很难在高频率时测量电流或电 ...
- vue-resource和vue-async-data两个插件的使用
vue-resource和vue-async-data两个插件的使用,看了一下文档http://cn.vuejs.org/guide/plugins.html#u5DF2_u6709_u63D2_u4 ...
- np.corrcoef()方法计算数据皮尔逊积矩相关系数(Pearson's r)
上一篇通过公式自己写了一个计算两组数据的皮尔逊积矩相关系数(Pearson's r)的方法,但np已经提供了一个用于计算皮尔逊积矩相关系数(Pearson's r)的方法 np.corrcoef() ...
- jQuery雷达扫描切换幻灯片代码
基于jQuery雷达扫描切换幻灯片代码.这是一款切换效果类似雷达扫描,支持鼠标滚轮滚动切换.效果图如下: 在线预览 源码下载 实现的代码. html代码: <div class=" ...
- C语言 · 空白格式化
标题:空白格式化 “空白格式化”具体做法是:去掉所有首尾空白:中间的多个空白替换为一个空格.所谓空白指的是:空格.制表符.回车符. 填空为:*p_to<*p_from: #include< ...
- docker被屏蔽后下载方法
docker镜像默认的官网上传平台:https://hub.docker.com/,k8s运行时需要从google下载镜像(k8s.gcr.io),但该网被屏蔽了,怎样下载到所需镜像呢? 1. 可在知 ...