Codeforces 471D MUH and Cube Walls【思维+KMP】

本文介绍了一个有趣的问题:如何在一个数组中寻找与另一个可调整高度的数组相匹配的连续子数组。通过采用KMP算法并利用差值的概念,文章提供了一种高效解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. MUH and Cube Walls
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev got hold of lots of wooden cubes somewhere. They started making cube towers by placing the cubes one on top of the other. They defined multiple towers standing in a line as a wall. A wall can consist of towers of different heights.

Horace was the first to finish making his wall. He called his wall an elephant. The wall consists of w towers. The bears also finished making their wall but they didn't give it a name. Their wall consists of n towers. Horace looked at the bears' tower and wondered: in how many parts of the wall can he "see an elephant"? He can "see an elephant" on a segment of w contiguous towers if the heights of the towers on the segment match as a sequence the heights of the towers in Horace's wall. In order to see as many elephants as possible, Horace can raise and lower his wall. He even can lower the wall below the ground level (see the pictures to the samples for clarification).

Your task is to count the number of segments where Horace can "see an elephant".

Input

The first line contains two integers n and w (1 ≤ n, w ≤ 2·105) — the number of towers in the bears' and the elephant's walls correspondingly. The second line contains n integers ai (1 ≤ ai ≤ 109) — the heights of the towers in the bears' wall. The third line contains w integers bi (1 ≤ bi ≤ 109) — the heights of the towers in the elephant's wall.

Output

Print the number of segments in the bears' wall where Horace can "see an elephant".

Examples
Input
13 5
2 4 5 5 4 3 2 2 2 3 3 2 1
3 4 4 3 2
Output
2
Note

The picture to the left shows Horace's wall from the sample, the picture to the right shows the bears' wall. The segments where Horace can "see an elephant" are in gray.



问题比较抽象,我们对于题目大意进行简化说明:

给你一个数组A,再给你一个数组B,我们可以任意将数组B整体增加或者减少值X(整数),我们可以进行修改值的操作无限次,每次我们要在数组A中找寻有几段和数组B完全匹配的子段。


思路:


如果我们枚举这个修改值X的话,时间复杂度非常的高,这里我们只要想到了差值,那么这个题就结束了。

对应数组B,我们无论对于整体修改值X为多大,其差值都是一样的:

3 4 4 3 2

-1 0 1 1 

所以我们只要对于这个差值进行KMP匹配即可。


当M==1的时候需要特判,答案为N.


Ac代码:

#include<stdio.h>
#include<string.h>
using namespace std;
int tmpa[2500050];
int tmpb[2500050];
int a[250050];
int b[250050];
int next[250050];
int n,m;
int lena;
int lenb;
int output;
void set_naxt()//子串的next数组
{
    int i=0,j=-1;
    next[0]=-1;
    while(i<lenb)
    {
        if(j==-1||b[i]==b[j])
        {
            i++; j++;
            next[i]=j;
        }
        else
        j=next[j];
    }
}

int kmp()
{
    int i=0,j=0;
    set_naxt();
    while(i<lena)
    {
        if(j==-1||a[i]==b[j])
        {
            i++;j++;
        }
        else
        j=next[j];
        if(j==lenb)
        {
            output++;
            j=next[j];
            //printf("%d\n",j);
        }
    }
    return -1;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0;i<n;i++)scanf("%d",&tmpa[i]);
        for(int i=0;i<m;i++)scanf("%d",&tmpb[i]);
        if(m==1)
        {
            printf("%d\n",n);
            continue;
        }
        for(int i=0;i<n-1;i++)tmpa[i]=tmpa[i]-tmpa[i+1];
        for(int i=0;i<m-1;i++)tmpb[i]=tmpb[i]-tmpb[i+1];
        lena=n-1;
        lenb=m-1;
        for(int i=0;i<n-1;i++)a[i]=tmpa[i];
        for(int i=0;i<m-1;i++)b[i]=tmpb[i];
        output=0;
        kmp();
        printf("%d\n",output);
    }
}






当前提供的引用内容并未提及关于Codeforces比赛M1的具体时间安排[^1]。然而,通常情况下,Codeforces的比赛时间会在其官方网站上提前公布,并提供基于不同时区的转换工具以便参赛者了解具体开赛时刻。 对于Codeforces上的赛事而言,如果一场名为M1的比赛被计划举行,则它的原始时间一般按照UTC(协调世界时)设定。为了得知该场比赛在UTC+8时区的确切开始时间,可以遵循以下逻辑: - 前往Codeforces官网并定位至对应比赛页面。 - 查看比赛所标注的标准UTC起始时间。 - 将此标准时间加上8小时来获取对应的北京时间(即UTC+8)。 由于目前缺乏具体的官方公告链接或者确切日期作为依据,无法直接给出Codeforces M1比赛于UTC+8下的实际发生时段。建议定期访问Codeforces平台查看最新动态更新以及确认最终版程表信息。 ```python from datetime import timedelta, datetime def convert_utc_to_bj(utc_time_str): utc_format = "%Y-%m-%dT%H:%M:%SZ" bj_offset = timedelta(hours=8) try: # 解析UTC时间为datetime对象 utc_datetime = datetime.strptime(utc_time_str, utc_format) # 转换为北京时区时间 beijing_time = utc_datetime + bj_offset return beijing_time.strftime("%Y-%m-%d %H:%M:%S") except ValueError as e: return f"错误:{e}" # 示例输入假设某场Codeforces比赛定于特定UTC时间 example_utc_start = "2024-12-05T17:35:00Z" converted_time = convert_utc_to_bj(example_utc_start) print(f"Codeforces比赛在北京时间下将是:{converted_time}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值