CodeForces - 958F1 (你要提醒自己,英语很重要)

解析 CodeForces 958/F1 题目,任务涉及从给定序列中找出符合特定颜色数量要求的连续子序列。通过暴力枚举方法解决此问题。

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

题目链接:http://codeforces.com/problemset/problem/958/F1

There is unrest in the Galactic Senate. Several thousand solar systems have declared their intentions to leave the Republic. Master Heidi needs to select the Jedi Knights who will go on peacekeeping missions throughout the galaxy. It is well-known that the success of any peacekeeping mission depends on the colors of the lightsabers of the Jedi who will go on that mission.

Heidi has n Jedi Knights standing in front of her, each one with a lightsaber of one of m possible colors. She knows that for the mission to be the most effective, she needs to select some contiguous interval of knights such that there are exactly k1 knights with lightsabers of the first color, k2 knights with lightsabers of the second color, ..., km knights with lightsabers of the m-th color. Help her find out if this is possible.

Input

The first line of the input contains n (1 ≤ n ≤ 100) and m (1 ≤ m ≤ n). The second line contains n integers in the range {1, 2, ..., m} representing colors of the lightsabers of the subsequent Jedi Knights. The third line contains m integers k1, k2, ..., km (with ) – the desired counts of lightsabers of each color from 1 to m.

Output

Output YES if an interval with prescribed color counts exists, or output NO if there is none.

Example
Input
5 2
1 1 2 2 1
1 2
Output
YES


学校集训中的一题,当时没看懂题目,发现是真的难过,我要好好学习英语,嘤嘤嘤。

题意:

      输入n,m,再给出n个数字,取值区间在[1,m],再给出m个数字每个数字表示1-m要求的出现的次数,然后在那n个数字中找一段区间使满足m个数字表示的1-m出现的次数。样例举例就是在 1 1 2 2 1 中 找到1出现1次,2出现2次的一个区间,很明显是 1 " 1 2 2 " 1  或 1 1 “ 2 2 1 "区间,找得到输出YES否则NO。


思路:

   我要好好学英语,我要好好背单词~~

   数据量较少,可以暴力求解,区间不间断且固定,只要顺序访问标记就行。

AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
    int a[105],b[105],b1[105];
    int n,m,sum=0;
    scanf("%d%d",&n,&m);
    for( int i=1; i<=n; i++ )
    scanf("%d",&a[i]);
    for( int i=1; i<=m; i++ )
    {
        scanf("%d",&b[i]) ;
        sum+=b[i];
    }

    int flag=0;
    for( int i=1; i<=n ; i++ )
    {
        memset(b1,0,sizeof(b1));
        for( int j=i; i+sum-1<=n&&j<=i+sum-1; j++ )
        {
            b1[ a[j] ] ++ ;
        }

        for( int j=1; j<=m; j++ )
        {
            if( b1[j] != b[j] ) break;
            else if( j==m )
            {
             flag=1;
             goto L;
            }
        }

    }
    L:
    if( flag ) printf("YES\n");
    else printf("NO\n");
    return 0;
}

 interval  :区间            prescribed :所规定的       desired :要求的  //三个单词不会要人命



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值