http://codeforces.com/contest/368/problem/D

题意:有a、b两个数组,a数组有n个数,b数组有m个数,现在给出一个p,要你找出所有的位置q,使得位置q  q+p   q+2*p  .....q+(m-1)*p   经过一定的操作(不改变数据大小)全等于b数组。

思路:首先肯定对a数组离散,然后二分对a、b数组分配好离散后的值。其实我们只需要枚举0————p位置,哈希记录,然后从q----一直滚到q(m-1)*p>=n,对于一个数据,出来第一个数,进去最后一个数。

这样,如果没有避免对b数组扫描一次,那么还是会超时,额,我在这里超时了几次。这个题目的核心就是如何避免再一次扫描b数组,因为我要判断挑出来的m个值与b数组全等........其实可以这样,我们把b数组里的数据都记录一次,比如b[10]={1,2,3,1,2,3,4,5,1,2};

那么我们对于b数组哈希的话,就是vist[1]=3    vist[2]=3    vist[3]=2    vist[4]=1    vist[5]=1   那么,如果说要有一个数来记录b有几种不想等的数据的话,那么k=5;

那么同样的,我们会对于从a数组里面挑出来的m个值进行哈希,那么有vist1[1]   vist1[2]   vist1[3]   vist1[4]   vist1[5];

当且仅当,vist[1]==vist1[1]   vist[2]==vist1[2]   vist[3]==vist1[3]   vist[4]==vist1[4]   vist[5]==vist1[5]时,与b全等,那么也就是说

在严格挑选出来的m各值,要有m种数,并且,这m种数要等于k,如此就可以说挑出来的m个数就是b数组.........

那么其实,只要有vist[i]>0时  有vist[i]==vist1[i]    那就m++    如果某个操作,弹出来一个值,使得原本vist[i]>0时  有vist[i]==vist1[i]的,现在不想等了,那么就m--;

ac代码:

额,还需要注意一点,这个数据有的地方会超int型,所以,用__int64比较好把
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int vist[200005],vist1[200005];
int a[200005],b[200005],s[200005];
int total[200005],vist2[200005];
int vist3[200005];
int erfen(int ll,int rr,int ans)
{
while(ll<=rr)
{
int mid=(ll+rr)/2;
if(s[mid]>ans)
rr=mid-1;
else ll=mid+1;
}
return rr;
}
int main()
{
int n,m,p;
while(scanf("%d%d%d",&n,&m,&p)>0)
{
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
s[i]=a[i];
}
sort(s,s+n);
int cnt=1;
for(int i=1; i<n; i++)
if(s[i]!=s[i-1])
s[cnt++]=s[i];
sort(s,s+cnt);
memset(vist,0,sizeof(vist));
memset(vist1,0,sizeof(vist1));
memset(vist2,0,sizeof(vist2));
memset(vist3,0,sizeof(vist3));
//for(int i=0;i<n;i++)
//vist[s[i]]=i+1;
for(int i=0; i<m; i++)
scanf("%d",&b[i]); for(int i=0; i<n; i++)
{
int id=erfen(0,cnt-1,a[i]);
vist[id]=1;
a[i]=id;
}
int flg=1;
int ans=0,ans1=0;
for(int i=0; i<m; i++)
{
int id=erfen(0,cnt-1,b[i]);
if(b[i]==s[id])
{
if(vist1[id]==0)
ans++;
vist1[id]++;
} else
{
flg=0;
break;
}
b[i]=id; //vist2[id]=1;
} if(flg==0)
{
printf("0\n");
continue;
}
int sum=0;
for(int i=0; i<p; i++)
{ if((__int64)i+(__int64)(m-1)*(__int64)p>=n)
break; for(int j=0; j<m; j++)
{
if((__int64)i+(__int64)j*(__int64)p>=n)
break;
int y=a[i+j*p];
if(vist1[y])
{
vist2[y]++;
if(vist2[y]==vist1[y])
{
ans1++;
vist3[y]=0;
}
if(vist2[y]>vist1[y]&&vist3[y]==0)
{
ans1--;
vist3[y]=1;
}
} }
if(ans1==ans)
{
total[sum++]=i;
}
for(int j=i+p; (__int64)j+(__int64)(m-1)*(__int64)p<n;j+=p)
{
int y=a[j-p];
if(vist1[y])
{
if(vist2[y]==vist1[y])
{
ans1--;
vist3[y]=0;
} vist2[y]--;
if(vist2[y]==vist1[y])
{
ans1++;
vist3[y]=0;
}
}
y=a[j+(m-1)*p];
if(vist1[y])
{
vist2[y]++;
if(vist2[y]==vist1[y])
{
ans1++;
vist3[y]=0;
}
if(vist2[y]>vist1[y]&&vist3[y]==0)
{
ans1--;
vist3[y]=1;
}
}
if(ans1==ans)
{
total[sum++]=j;
}
}
for(int k=0;k<m;k++)
{
vist2[b[k]]=0;
vist3[b[k]]=0;
ans1=0;
}
}
printf("%d\n",sum);
sort(total,total+sum);
for(int i=0; i<sum; i++)
{
if(i==0)
printf("%d",total[i]+1);
else
printf(" %d",total[i]+1);
}
printf("\n");
}
return 0;
}

Codeforces Round #215 (Div. 2) D. Sereja ans Anagrams的更多相关文章

  1. 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 ...

  2. Codeforces Round #215 (Div. 2) B. Sereja and Suffixes map

    B. Sereja and Suffixes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...

  3. Codeforces Round #215 (Div. 2) C. Sereja and Algorithm

    #include <iostream> #include <vector> #include <algorithm> #include <string> ...

  4. Codeforces Round #215 (Div. 2) B. Sereja and Suffixes

    #include <iostream> #include <vector> #include <algorithm> #include <set> us ...

  5. Codeforces Round #215 (Div. 2) A. Sereja and Coat Rack

    #include <iostream> #include <vector> #include <algorithm> using namespace std; in ...

  6. 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 ...

  7. Codeforces Round #215 (Div. 1)

    A Sereja and Algorithm 题意:给定有x,y,z组成的字符串,每次询问某一段s[l, r]能否变成变成zyxzyx的循环体. 分析: 分析每一段x,y,z数目是否满足构成循环体,当 ...

  8. Codeforces Round #223 (Div. 2) E. Sereja and Brackets 线段树区间合并

    题目链接:http://codeforces.com/contest/381/problem/E  E. Sereja and Brackets time limit per test 1 secon ...

  9. Codeforces Round #223 (Div. 2)--A. Sereja and Dima

    Sereja and Dima time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

随机推荐

  1. HDU 4602 Partition (矩阵乘法)

    Partition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  2. 如何在Android Studio项目中导入开源库?

    前两天,谷歌发布了Android Studio 1.0的正式版,也有更多的人开始迁移到Android Studio进行开发.然而,网上很多的开源库,控件等还是以前的基于Eclipse进行开发,很多人不 ...

  3. 【Oracle】Oracle 的过程化SQL(PLSQL)中NULL值的处理

    下面是NULL的几个注意点: 1.NULL值既不是空格也不是0. 2.给表插入值的时候,如果没有给列指定列值,则默认为NULL. 3.当算术表达式里包含NULL值时,其计算结果也是NULL值. 这时候 ...

  4. OpenCV 学习笔记03 threshold函数

    opencv-python   4.0.1 简介:该函数是对数组中的每一个元素(each array element)应用固定级别阈值(Applies a fixed-level threshold) ...

  5. jquery ajax context

    function yflib_roomList(tpl) { var target = $(".roomList > li"); var _this = null; $(&q ...

  6. nginx根据http_user_agent防DDOS

    前端squid反向代理到nginx nginx根据http_user_agent防DDOS 首先查看访问日志,找出可疑访问 找到http_user_agent 的特征,然后再作过滤 "Moz ...

  7. CentOS 7.3 系统安装配置图解教程

    一.安装CentOS 7.3 CentOS 7.x系列只有64位系统,没有32位.生产服务器建议安装CentOS-7-x86_64-Minimal-1611.iso版本 成功引导系统后,会出现下面的界 ...

  8. hdu 4223 Dynamic Programming? (dp)

    //连续的和的绝对值最小 # include <stdio.h> # include <string.h> # include <algorithm> # incl ...

  9. Android_ViewPager_实现多个图片水平滚动

    1.示意图                       2.实现分析 (1).xml配置 <!-- 配置container和pager的clipChildren=false, 并且指定margi ...

  10. 尼康G镜头与D镜头的差别

    尼康系统实现自动对焦必须:机身和镜头,两者至少其一拥有马达. 尼康现在新出的G头,几乎都带马达.但在胶片时代的G头,几乎都不带马达. G只表示:没有手动光圈环. D只表示:镜头能提供距离信息给机身. ...