Sereja ans Anagrams 优先队列控制
Sereja has two sequences a and b and number p. Sequence a consists of n integers a1, a2, ..., an. Similarly, sequence b consists of m integers b1, b2, ..., bm. As usual, Sereja studies the sequences he has. Today he wants to find the number of positions q (q + (m - 1)·p ≤ n; q ≥ 1), such that sequence b can be obtained from sequence aq, aq + p, aq + 2p, ..., aq + (m - 1)p by rearranging elements.
Sereja needs to rush to the gym, so he asked to find all the described positions of q.
Input
The first line contains three integers n, m and p (1 ≤ n, m ≤ 2·105, 1 ≤ p ≤ 2·105). The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109). The next line contains m integers b1, b2, ..., bm (1 ≤ bi ≤ 109).
Output
In the first line print the number of valid qs. In the second line, print the valid values in the increasing order.
Example
5 3 1
1 2 3 2 1
1 2 3
2
1 3
6 3 2
1 3 2 2 3 1
1 2 3
2
1 2 暴力肯定超时,总算写出来了,想到了只需要满足当判断了m个元素满足后,删除最初的那个元素以及标记,然后接着往下判断,但是写的很麻烦,后来用优先队列,但是又出了小问题(代码中有表示),很小的问题,以至于我烦的找不到。 代码:
#include <iostream>
#include <cstdio>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
struct que
{
int id,num;
friend bool operator<(que a,que b)
{
return a.id>b.id;
}
}t;
int n,m,p,a[],b[],k=,c,ans[];
int main()
{
map<int,int> mark;
scanf("%d%d%d",&n,&m,&p);
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
}
for(int i=;i<m;i++)
{
scanf("%d",&b[i]);
mark[b[i]]++;
}
for(int i=;i<p;i++)
{
map<int,int>check;
priority_queue <que> q;
for(int j=i;j<n;j+=p)
{
//cout<<q.size()<<endl;
if(mark[a[j]]==)
{
while(!q.empty()){check[a[q.top().num]]--;q.pop();}///check的下标应该是a数组的元素 而不是a的下标
continue;
}
if(check[a[j]]<mark[a[j]])
{
check[a[j]]++;
if(q.empty())
{
t.id=,t.num=j;
q.push(t);
}
else
{
t.id=q.top().id+q.size();
t.num=j;
q.push(t);
}
}
else
{
while(!q.empty()&&a[q.top().num]!=a[j])///判断 对应位置的元素是否相等,而我当时判断了下标,傻逼吧,下标怎么可能相等,热傻了吧
{
check[a[q.top().num]]--;
q.pop();
}
t.id=q.top().id+q.size();
t.num=j;
q.push(t);
q.pop();
}
if(q.size()==m)
{
//cout<<q.top().num<<endl;
ans[k++]=q.top().num+;
check[a[q.top().num]]--;
q.pop();
//cout<<q.top().num<<endl;
}
}
}
printf("%d\n",k);
sort(ans,ans+k);
for(int i=;i<k;i++)
printf("%d ",ans[i]);
}
Sereja ans Anagrams 优先队列控制的更多相关文章
- Codeforces Round #215 (Div. 1) B. Sereja ans Anagrams 匹配
B. Sereja ans Anagrams Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...
- Sereja ans Anagrams
Codeforces Round #215 (Div. 1) B:http://codeforces.com/problemset/problem/367/B 题意:给你两个序列a,b,然后给你一个数 ...
- Codeforces Round #215 (Div. 2) D. Sereja ans Anagrams
http://codeforces.com/contest/368/problem/D 题意:有a.b两个数组,a数组有n个数,b数组有m个数,现在给出一个p,要你找出所有的位置q,使得位置q q+ ...
- cf D. Sereja ans Anagrams
http://codeforces.com/contest/368/problem/D #include <cstdio> #include <cstring> #includ ...
- Codeforces Round #243 (Div. 2) C. Sereja and Swaps(优先队列 暴力)
题目 题意:求任意连续序列的最大值,这个连续序列可以和其他的 值交换k次,求最大值 思路:暴力枚举所有的连续序列.没做对是因为 首先没有认真读题,没看清交换,然后,以为是dp或者贪心 用了一下贪心,各 ...
- [ An Ac a Day ^_^ ] CodeForces 426C Sereja and Swaps 优先队列
题意: 给你一个有n个数的序列 取一个区间 这个区间内的数可以与区间外的值交换k次 问这样的区间最大值是多少 思路: 看数据是200 时间复杂度O(n*n) 应该可以暴力 顺便学习一下优先队列 枚举区 ...
- Codeforces Round #215 (Div. 2) D题(离散化+hash)
D. Sereja ans Anagrams time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces 367
A. Sereja and Algorithm 水题不解释. B. Sereja ans Anagrams 模p同余的为一组,随便搞. C. Sereja and the Arrangement of ...
- Codeforces Round #215 (Div. 1)
A Sereja and Algorithm 题意:给定有x,y,z组成的字符串,每次询问某一段s[l, r]能否变成变成zyxzyx的循环体. 分析: 分析每一段x,y,z数目是否满足构成循环体,当 ...
随机推荐
- Java类和对象的概念
Java是一门面向对象的编程语言,理解Java,首先要理解类与对象这两个概念. Java中的类可以看做C语言中结构体的升级版.结构体是一种构造数据类型,可以包含不同的成员(变量),每个成员的数据类型可 ...
- luogu P2408 不同子串个数
考虑反向操作,去计算有多少组相同的子串,对于一组大小为k的极大相同子串的集合,ans-=k-1. 为了避免重复计算,需要一种有效的,有顺序的记录方案. 比如说,对于每一个相同组,按其起始点所在的位置排 ...
- python-day41--数据库---数据类型
一.存储引擎决定了表的类型,而表内存放的数据也要有不同的类型,每种数据类型都有自己的宽度,但宽度是可选的 二.mysql 数据类型 1.数字:(宽度指的是显示宽度,与存储无关) 不用指定宽度, ...
- HDU-4848 Wow! Such Conquering! (回溯+剪枝)
Problem Description There are n Doge Planets in the Doge Space. The conqueror of Doge Space is Super ...
- sql 智能提示
依次打开SSMS—>工具—>选项—>文本编辑器—>Transact-SQL—>IntelliSense—>检查右侧窗体是否启用!!
- .net 中struct(结构)和class(类)的区别
1.struct 结构与class(类)的区别 1)struct是值类型,class是对象类型 2)struct不能被继承,class可以被继承 3)struct默认访问权限是public,而clas ...
- Openwrt working with patches in the build system (8)
Reference :https://openwrt.org/docs/guide-developer/build-system/use-patches-with-buildsystem exampl ...
- poj 1330 Nearest Common Ancestors(LCA 基于二分搜索+st&rmq的LCA)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 30147 Accept ...
- DBGRID 拖动滚动条 和 鼠标滚轮的问题
滚动条拖动问题 默认是,拖动时,网格内数据不变,等放开鼠标后才会变. 方法 拖动时同时变,当前记录也变,不用新控件 http://wenwen.sogou.com/z/q185291591.htm 鼠 ...
- Shiro 学习资料
参考链接:http://jinnianshilongnian.iteye.com/blog/2018398