6312. Lottery

6312. Lottery 
(File IO): input:lottery.in output:lottery.out

Time Limits: 1000 ms  Memory Limits: 32768 KB  Detailed Limits  

Description

定义两个序列对应位置上不同的值的个数不超过 k,则可称为 k 相似。
现在有一个长度为 n 的序列 a,将它划分为 n−l+1 个长度为 l 的子串(第 i 个子串为 a[i]~a[i+l-1])
q 组询问,第 j 组询问给出一个 kj,求每个子串与多少个其它的子串可称为 kj 相似。

Input

第一行输入包含两个整数 n 和 l。
接下来一行 n 个整数表示序列 a。
第三行输入一个整数 q。
接下来 q 行每行一个整数 kj。

Output

输出有 q 行,每行 n-l+1 个整数,第 i 个整数表示第 i 个子串与多少个其它的子串可称为 kj 相似。

Sample Input

6 2
1 2 1 3 2 1
2
1
2
 

Sample Output

2 1 1 1 1
4 4 4 4 4

Data Constraint

对于 25% 的数据点,n<=300;
对于另外 20% 的数据点,n<=2000;
对于另外 20% 的数据点,q=1,k1=1;
对于另外 15% 的数据点,q=1;
对于 100% 的数据点,kj<=l<=n<=10000,q<=100,ai<=10^9。

Source / Author: CEOI2018 day1 lot

 

题解:

设ans[j,i]表示与串[i,i+l-1]距离为j的串的个数,直接暴力复杂度为O(n^2*l) 考虑,若已知[l1,r1]和[l2,r2]的距离,则可以O(1)计算出[l1+1,r1+1]和[l2+1,r2+1]的距离 即O(n)时间计算O(n)对子串(端点差相同的子串对放在一起计算),那么O(n^2)可计算完所有子串对的距离。 然而空间O(n^2)没法接受。 注意到询问只有q次,所以,开个O(nq)的数组,前缀和计算一下即可

 

#include<cstdio>
#include<cstring>
#include<algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define mcy(a,b) memcpy(a,b,sizeof(a))
#define N 20010
#define Q 110
#define ll long long
#define re register
#define inf 2147483647
#define mod (ll)(1e9+7)
#define open(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout)
using namespace std;

template<class T> 
T in(T &x)
{
	char ch(0);T f=0; x=0;
	while(ch < '0' || ch > '9') ch = getchar() , f |= ch == '-';
	while(ch>='0' && ch<='9')  x = (x<<1) + (x<<3) + ch - '0' , ch = getchar();
	return f ? (x = -x) : x;
}
int n,l,q,a[N],k;
int ans[N][Q],qu[Q],qa[Q],r[N],t[N];

bool cmp(int a,int b){return a>b;}

void init()
{
	sort(qa+1 , qa+1+q , cmp);
	for(int i=1;i<=q;i++) r[qa[i]] = i;
	for(int i=l;i>0;--i)
		 r[i] = (r[i] ? r[i] : r[i+1]);
	r[0] = q;
	for(int i=1,match=0;i<=n-l;i++,match=0)
	{
		//calc [1-l][1+i - l+i]
		for(int k=1;k<=l;k++) if(a[k]!=a[i+k]) ++match;
		++ans[1][r[match]] , ++ans[i+1][r[match]];
		//push it
		for(int j=1;j<=n-l-i;j++) 
		{
			match += (a[j+l]!=a[j+i+l]) - (a[j]!=a[j+i]);
			++ans[i+j+1][r[match]];
			++ans[j+1][r[match]];
		}
	}
	for(int i=1;i<=n-l+1;i++) 
		for(int j=q;j>=0;j--) ans[i][j]+=ans[i][j+1];
}

int main()
{
	open("lottery");
	in(n) , in(l);
	for(int i=1;i<=n;i++) in(a[i]);
	in(q);
	for(int i=1;i<=q;i++) in(qu[i]);
	mcy(qa , qu);
	init();
	for(int i=1;i<=q;i++)
		{for(int j=1;j<=n-l+1;j++) printf("%d ",ans[j][r[qu[i]]]);puts("");}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值