题目链接: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.
InputThe 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 YES if an interval with prescribed color counts exists, or output NO if there is none.
Example5 2
1 1 2 2 1
1 2
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 :要求的 //三个单词不会要人命