ZOJ-4089-Little Sub and Isomorphism Sequences
给定你个数组,以及一些单点修改,以及询问,每次询问需要求得,最长的字串长度,它在其他位置存在同构。
当存在两个不相交的区间同构时,如:
1、2、……、n -1、n、n + 1、……、m、m + 1、m + 2、 ……、m + n - 1、m + n;(假设m > n&&[1, n]和[m + 1, m + n]同构)
那么 K = n 显然是存在的。但是这不是最大的K,因为[1, m]和[n + 1, n + m]也一定同构(使两边同时加上一个相同区间)
所以这题可以简化为找到一个区间首尾元素相同,求最大区间长度;
Accepted | 4089 | C++11 | 1200 | 23516 |
#include "bits/stdc++.h"
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5 + ;
map<int, set<int> > mp;
multiset<int> ans;
int arr[MAXN];
int main() {
int t, n, m;
scanf("%d", &t);
while (t--) {
mp.clear();
ans.clear();
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++) {
scanf("%d", &arr[i]);
mp[arr[i]].insert(i);
}
for (auto i : mp) {
ans.insert(*(--i.second.end()) - *i.second.begin());
}
int op, x, y;
while (m--) {
scanf("%d", &op);
if (op == ) {
if (*(--ans.end()) == ) {
puts("-1");
} else {
printf("%d\n", *(--ans.end()));
}
} else {
scanf("%d%d", &x, &y);
auto it = ans.find(*(--mp[arr[x]].end()) - *mp[arr[x]].begin());
ans.erase(it);
mp[arr[x]].erase(x);
if (!mp[arr[x]].empty()) {
ans.insert(*(--mp[arr[x]].end()) - *mp[arr[x]].begin());
} if (!mp[y].empty()) {
it = ans.find(*(--mp[y].end()) - *mp[y].begin());
ans.erase(it);
}
arr[x] = y;
mp[y].insert(x);
ans.insert(*(--mp[y].end()) - *mp[y].begin());
}
}
}
return ;
}
可能表述不是很清楚,这题参考了一下https://www.cnblogs.com/hua-dong/p/10293407.html这位大佬的博客;
ZOJ-4089-Little Sub and Isomorphism Sequences的更多相关文章
- 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 ...
- 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 Monthly, January 2019
A: Little Sub and Pascal's Triangle Solved. 题意: 求杨辉三角第n行奇数个数 思路: 薛聚聚说找规律,16说Lucas 答案是 $2^p \;\;p 为 n ...
- ZOJ Problem Set - 1338 Up and Down Sequences 解释 ac代码
这道题目我一开始一头雾水,怎么都数不对,参考了下网上的博文,才弄懂. 题意是这样的,如果是上升序列,上升序列的长度不是所有上升数字的,是这么规定的,如果它与前一个数字构成上升,那么这个数字算上长度.所 ...
- ZOJ 3861 - Valid Pattern Lock
3861 - Valid Pattern Lock Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- ZOJ 3233 Lucky Number
Lucky Number Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Original I ...
随机推荐
- leetcode 994.腐烂的橘子
题目: 在给定的网格中,每个单元格可以有以下三个值之一: 值 0 代表空单元格: 值 1 代表新鲜橘子: 值 2 代表腐烂的橘子. 每分钟,任何与腐烂的橘子(在 4 个正方向上)相邻的新鲜橘子都会腐烂 ...
- 计蒜客 一维坐标的移动(BFS)
在一个长度为 n 的坐标轴上,蒜头君想从 A 点 移动到 B 点.他的移动规则如下: 向前一步,坐标增加 1. 向后一步,坐标减少 1. 跳跃一步,使得坐标乘 2. 蒜头君不能移动到坐标小于 0 或大 ...
- 3.redis认证
redis认证方法 1.redis.conf requirepass PASSWORD 2.redis-cli auth PASSWORD redis清空数据库 flushdb //清空当前数据库 f ...
- 吴裕雄--天生自然TensorFlow2教程:Tensor数据类型
list: [1,1.2,'hello'] ,存储图片占用内存非常大 np.array,存成一个静态数组,但是numpy在深度学习之前就出现了,所以不适合深度学习 tf.Tensor,为了弥补nump ...
- python3 文件流
文件流 # python里的打开一个文件用open(),如果不能打开,会抛出异常:OSError # 文件流的基本参数 # . file: 打开一个文件 # . mode: 打开的模式,默认模式为tx ...
- 解决UITextView无法滚到底部
程序中有一个UITextView控件 @property (weak, nonatomic) IBOutlet UITextView *textView; 一般情况下,以下代码可实现UITextVie ...
- Mutation|DNM|
生命组学 DNA序列改变的分子基础 变异来源 据研究对象,可分为两类mutation:个体上的变异和群体上的变异,群体上的变异是关联研究,eg喝酒人群vs非喝酒人群相比. 造成mutation的三类机 ...
- iOS 10 新增plist文件属性
大概统计了一下需要加的一些字段列在下面: NSContactsUsageDescription -> 通讯录 NSMicrophoneUsageDescription -> 麦克风 NSP ...
- Linux服务器性能查看命令
一.uptime命令 [root@#test~]# uptime15:26:42 up 101 days, 18:44, 3 users, load average: 0.18, 0.22, 0. ...
- FactoryBean 和 BeanFactory
大佬勿喷,如果有什么地方理解错了欢迎吐槽 一 .BeanFacory 首先来看看下边的代码 package com.lhf.beanfactory; public class SingleBean { ...