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 集合-Set接口和三个子类实现
2017-10-31 19:20:45 Set 一个不包含重复元素的 collection.无序且唯一. HashSet LinkedHashSet TreeSet HashSet是使用哈希表(has ...
- Python 爬虫-Requests库入门
2017-07-25 10:38:30 response = requests.get(url, params=None, **kwargs) url : 拟获取页面的url链接∙ params : ...
- [Android教程] Cordova开发App入门(二)使用热更新插件
前言 不知各位遇没遇到过,刚刚发布的应用,突然发现了一个隐藏极深的“碧油鸡(BUG)”,肿么办!肿么办!肿么办!如果被老板发现,一定会让程序员哥哥去“吃鸡”.但是想要修复这个“碧油鸡”,就必须要重新打 ...
- English trip -- VC(情景课)2 A At school
xu言: Sometimes, I feel very confused. However, there will always be a weak light in a corner to ligh ...
- 4-6 select_tag和select的区别和理解。javascript_tag
via: :all是什么意思?主要用于约束http动作. <%= select_tag "set_locale", options_for_select(LANGUAGES, ...
- CF1082G Petya and Graph
题意 定义图权 = 图中边权总和 - 图中点权总和(空图的图权=0),求 n 个点 m 条边的无向图最大权子图. 把边看成点,这个点与两个原图中的点连边.直接最小割求最大闭合子图即可.
- 『PyTorch』第七弹_nn.Module扩展层
有下面代码可以看出torch层函数(nn.Module)用法,使用超参数实例化层函数类(常位于网络class的__init__中),而网络class实际上就是一个高级的递归的nn.Module的cla ...
- OncePerRequestFilter的作用
在spring中,filter都默认继承OncePerRequestFilter,但为什么要这样呢? OncePerRequestFilter顾名思义,他能够确保在一次请求只通过一次filter,而不 ...
- shell 命令参数
$# 是传给脚本的参数个数$0 是脚本本身的名字$1 是传递给该shell脚本的第一个参数$2 是传递给该shell脚本的第二个参数$@ 是传给脚本的所有参数的列表$* 是以一个单字符串显示所有向脚本 ...
- 062——VUE中vue-router之命名视图的实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...