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 + 5;
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 = 1; 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 == 2) {
                if (*(--ans.end()) == 0) {
                    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 0;
}

可能表述不是很清楚,这题参考了一下https://www.cnblogs.com/hua-dong/p/10293407.html这位大佬的博客;

原文地址:https://www.cnblogs.com/Angel-Demon/p/10295711.html

时间: 2024-10-13 16:34:50

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 considered isomorphic when they meet all the following two conditions: ; Define as the number of times integer occur in sequence . For each integer in

ZOJ Monthly, January 2019

A: Little Sub and Pascal's Triangle Solved. 题意:求杨辉三角第n行奇数个数 思路:薛聚聚说找规律,16说Lucas 1 #include<bits/stdc++.h> 2 3 using namespace std; 4 5 typedef long long ll; 6 7 ll n; 8 9 int main() 10 { 11 int t; 12 scanf("%d", &t); 13 while(t--) 14 {

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

ZOJ 3233 Lucky Number

Lucky Number Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Original ID: 323364-bit integer IO format: %lld      Java class name: Main Watashi loves M mm very much. One day, M mm gives Watashi a chance to choose a number

zoj题目分类

饮水思源---zoj 转载自:http://bbs.sjtu.edu.cn/bbscon,board,ACMICPC,file,M.1084159773.A.html 注:所有不是太难的题都被归成了“简单题”,等到发现的时候已经太晚了,我太死脑筋 了……:( 有些题的程序我找不到了,555……:( SRbGa的题虽然都很经典……但是由于其中的大部分都是我看了oibh上的解题报告后做 的,所以就不写了…… 题目排列顺序没有规律……:( 按照个人感觉,最短路有的算做了DP,有的算做了图论. 有些比较

zoj Gao The Sequence

Gao The Sequence Time Limit: 2 Seconds      Memory Limit: 65536 KB You are given a sequence of integers, A1,A2,...,An. And you are allowed a manipulation on the sequence to transform the origin sequence into another sequence B1,B2,...,Bn(Maybe the tw

ZOJ Problem Set - 1004 Anagrams by Stack (回溯法)

ZOJ Problem Set - 1004 Anagrams by Stack Time Limit: 2 Seconds      Memory Limit: 65536 KB How can anagrams result from sequences of stack operations? There are two sequences of stack operators which can convert TROT to TORT: [ i i i i o o o o i o i

[LeetCode]Repeated DNA Sequences

题目:Repeated DNA Sequences 给定包含A.C.G.T四个字符的字符串找出其中十个字符的重复子串. 思路: 首先,string中只有ACGT四个字符,因此可以将string看成是1,3,7,20这三个数字的组合串: 并且可以发现{ACGT}%5={1,3,2,0};于是可以用两个位就能表示上面的四个字符: 同时,一个子序列有10个字符,一共需要20bit,即int型数据类型就能表示一个子序列: 这样可以使用计数排序的思想来统计重复子序列: 这个思路时间复杂度只有O(n),但是

概率dp ZOJ 3640

Help Me Escape Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3640 Appoint description:  System Crawler  (2014-10-22) Description Background     If thou doest well, shalt thou not be accepted? an