ZOJ - 4089 :Little Sub and Isomorphism Sequences (同构 set)
Little Sub has a sequence . Now he has a problem for you.
Two sequences of length and of length are considered isomorphic when they meet all the following two conditions:
- ;
- Define as the number of times integer occur in sequence . For each integer in , always holds.
Now we have operations for . and there are two kinds of operations:
- 1 x y: Change to (, );
- 2: Query for the greatest () that there exist two integers and () and is isomorphic with . Specially, if there is no such , please output "-1" (without quotes) instead.
There are multiple test cases. The first line of the input contains an integer (), indicating the number of test cases. For each test case:
The first line ontains two integers .
The second line contains integers ().
In the following lines, each line contains one operation. The format is described above.
Output
For each operation 2, output one line containing the answer.
Sample Input
1
3 5
1 2 3
2
1 3 2
2
1 1 2
2
Sample Output
-1
1
2
题意:给定你个数组,以及一些单点修改,以及询问,每次询问需要求得,最长的字串长度,它在其他位置存在同构。
思路:最长同构子串对总是会重叠的,然后两个子串不重叠的部分必须是同构。 那么一定有,最优之一就是“x+公共”与“公共+x”这两个同构最长。
这个不难反证。 那么实现就是每种数离散后建立set,然后保存每个set的最远距离即可。
#include<bits/stdc++.h>
#define rep(i,w,v) for(int i=w;i<=v;i++)
using namespace std;
const int maxn=;
int a[maxn],b[maxn],c[maxn],x[maxn],y[maxn],tot;
set<int>s[maxn];
multiset<int>S;
multiset<int>::iterator it;
int main()
{
int T,N,M,ans;
scanf("%d",&T);
while(T--){
scanf("%d%d",&N,&M); tot=;
rep(i,,N) scanf("%d",&a[i]),b[++tot]=a[i];
rep(i,,M){
scanf("%d",&c[i]);
if(c[i]==) {
scanf("%d%d",&x[i],&y[i]);
b[++tot]=y[i];
}
}
sort(b+,b+tot+);
tot=unique(b+,b+tot+)-(b+);
S.clear(); rep(i,,tot) s[i].clear();
rep(i,,N) {
a[i]=lower_bound(b+,b+tot+,a[i])-b;
s[a[i]].insert(i);
}
rep(i,,tot) if(!s[i].empty())
S.insert(*(--s[i].end())-*s[i].begin());
rep(i,,M){
if(c[i]==){
ans=-;
if(!S.empty()) ans=*(--S.end());
if(ans==) ans=-;
printf("%d\n",ans);
}
else {
it=S.find(*(--s[a[x[i]]].end())-*s[a[x[i]]].begin());
s[a[x[i]]].erase(x[i]);
S.erase(it);
if(!s[a[x[i]]].empty()) S.insert(*(--s[a[x[i]]].end())-*s[a[x[i]]].begin()); y[i]=lower_bound(b+,b+tot+,y[i])-b; a[x[i]]=y[i];
if(!s[y[i]].empty()) S.erase(S.find(*(--s[y[i]].end())-*s[y[i]].begin()));
s[y[i]].insert(x[i]);
S.insert(*(--s[y[i]].end())-*s[y[i]].begin());
}
}
}
return ;
}
ZOJ - 4089 :Little Sub and Isomorphism Sequences (同构 set)的更多相关文章
- Little Sub and Isomorphism Sequences ZOJ - 4089
ZOJ - 4089 思路:可以反正 最长重构序列必然符合 此模式 x + { } 与 { } + x 那么 题意转化为了 找两个距离最长的相同的数.eeee 先离散化 然后 ...
- ZOJ Monthly, January 2019 Little Sub and Isomorphism Sequences 【离线离散化 + set + multiset】
传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5868 Little Sub and Isomorphism Seque ...
- ZOJ Monthly, January 2019 I Little Sub and Isomorphism Sequences(set 妙用) ZOJ4089
写这篇博客来证明自己的愚蠢 ...Orz 飞机 题意:给定你个数组,以及一些单点修改,以及询问,每次询问需要求得,最长的字串长度,它在其他位置存在同构 题解:经过一些奇思妙想后 ,你可以发现问题是传 ...
- ZOJ-4089-Little Sub and Isomorphism Sequences
给定你个数组,以及一些单点修改,以及询问,每次询问需要求得,最长的字串长度,它在其他位置存在同构. 当存在两个不相交的区间同构时,如: 1.2.…….n -1.n.n + 1.…….m.m + 1.m ...
- ZOJ Monthly, January 2019
A: Little Sub and Pascal's Triangle Solved. 题意: 求杨辉三角第n行奇数个数 思路: 薛聚聚说找规律,16说Lucas 答案是 $2^p \;\;p 为 n ...
- Isomorphism 同构
小结: 1.两个有限维度的向量空间,在同一数域下,是同构的 等价于 它们维数相等. Isomorphism 同构 0.1.8 Isomorphism. If U and V are vector sp ...
- 【BZOJ4474】isomorphism(树的同构,哈希)
题意:一个无向树的度数为 2的结点称为假结点,其它结点称为真结点.一个无向树的简化树其结点由原树的全体真结点组成,两个真结点之间有边当且仅当它们在原树中有边,或者在原树中有一条联结这两个结点的路,其中 ...
- ZOJ Problem Set - 1338 Up and Down Sequences 解释 ac代码
这道题目我一开始一头雾水,怎么都数不对,参考了下网上的博文,才弄懂. 题意是这样的,如果是上升序列,上升序列的长度不是所有上升数字的,是这么规定的,如果它与前一个数字构成上升,那么这个数字算上长度.所 ...
- Python数据类型之“序列概述与基本序列类型(Basic Sequences)”
序列是指有序的队列,重点在"有序". 一.Python中序列的分类 Python中的序列主要以下几种类型: 3种基本序列类型(Basic Sequence Types):list. ...
随机推荐
- ubuntu16.10 安装ibus中文输入法
安装以下几种常用输入法: IBus拼音:sudo apt-get install ibus-pinyin IBUS五笔:sudo apt-get install ibus-table-wubi 谷歌拼 ...
- 逆袭之旅DAY16.东软实训.Oracle.序列
2018-07-12 14:07:44 序列 序列1.创建序列create sequence 序列名 [increment by n] ---步长 [start with n] ---序列的起始值 序 ...
- EF-生成迁移版本
前面讲到可以使用迁移技术让程序自动更新数据库中相关的结构.在我们每次需要新增模型类时,请一定要养成一个好的习惯,使用Add-Migration命令生成迁移版本.这样能恢复被误删除的表. 一.新增迁移版 ...
- vue-2-计算属性和观察者
<div id="example"> <p>Original message: "{{ message }}"</p> &l ...
- PLY文件格式
一.PLY简介 PLY文件格式是Stanford大学开发的一套三维mesh模型数据格式,图形学领域内很多著名的模型数据,比如Stanford的三维扫描数据库(其中包括很多文章中会见到的Happy Bu ...
- Cracking The Coding Interview 5.5
#include <iostream> #include <vector> using namespace std; int getNum1(int N) { int num= ...
- 深入理解java虚拟机---java虚拟机的发展史(四)
1.java虚拟机 A:java虚拟机有很多个版本,但是我们经常使用的是sun公司的HotSpot,可以通过以下命令获取java虚拟机版本 B:JAVA虚拟机分类: 1.Sun Class VM 2. ...
- log4net 2.0.8 不支持core 数据库记录日志
经过反编译log4net 标准库的代码,原本有的数据库链接AdoNetAppender 在core里面引用的,没有掉了. 可能新版本会有.
- UFT测试本地应用程序登陆小实例(描述性编程)
Dim username,password Dim casecount,i Dim currentid DataTable.ImportSheet ,"Action1" casec ...
- HashMap中hashCode()和equals()重要性
Java中HashMap根据hashCode()和equals()方法来获取键值对的索引,同时也通过这两个方法由key值获取value值.如果没有这两个方法,那么当有两个相同的 hash值时,可能会被 ...