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. ...
随机推荐
- Qt Widgets——动作类与小部件菜单项
本文主要涉及以下三个类: QAction ——QWidgetAction QActionGroup QAction可称为动作类,它一般可当作菜单中的项组成菜单,也可作为工具栏上的按钮,它主要由图标.文 ...
- find中的-exec参数
1.find中的-exec参数 在当前目录下(包含子目录),查找所有txt文件并找出含有字符串"bin"的行 find ./ -name "*.txt" -ex ...
- cmp的值到底是0还是1还是-1的问题
返回值不局限于这三个数返回负数,表示第一个参数小于第二个参数返回整数,表示第一个参数大于第二个参数返回0,表示他们相等
- 正则表达式test()和exec()、 search() 和 replace()用法实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 普通程序员,三年成为年薪70w架构师,只因做到了这些
每个程序员.或者说每个工作者都应该有自己的职业规划,如果你不是富二代,不是官二代,也没有职业规划,希望你可以思考一下自己的将来.今天给大家分享的是一篇来自阿里Java架构师对普通程序员的职业建议,希望 ...
- Python格式化字符 %s %d %f
格式 描述%% 百分号标记 #就是输出一个%%c 字符及其ASCII码%s 字符串%d 有符号整数(十进制)%u 无符号整数(十进制)%o 无符号整数(八进制)%x 无符号整数(十六进制)%X 无符号 ...
- Json使用示例
使用Json,可以下载如下所示的6个Jar包 整个工程目录结构如下: 简单的用法: package json; import net.sf.json.JSONArray; import net.sf. ...
- 堆排序,图解,C/C++实现
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- Oauth2.0:Access Token 与 Refresh Token
access token 是客户端访问资源服务器的令牌.拥有这个令牌代表着得到用户的授权.然而,这个授权应该是临时的,有一定有效期.这是因为,access token 在使用的过程中可能会泄露.给 a ...
- APC注入(Ring3)
首先简单介绍一下APC队列和Alertable. 看看MSDN上的一段介绍(https://msdn.microsoft.com/en-us/library/ms810047.aspx): The s ...