题目意思
搞得好像阅读理解,就是给定一个序列。序列的每个值都有属性,N代表可加可减,L代表只能加,D代表只能减,当然这些都可以用或者不用。给定一个k,问k是否可能用这个序列构成。
解题思路
这道题就是固定左右的区间,不断地扩展,然后判断k是否在这个区间内就可。
感觉这道题就是阅读理解,给的式子也没啥用,关键竟然能够水过,根本就没有深究,考虑的不够严谨,竟然还有这种操作,表示很心累。
代码部分
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
int main()
{
int t,n,k;
int a[1100];
char b[1100];
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
int temp1=0,temp2=0;
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
for(int i=1; i<=n; i++)
{
scanf(" %c",&b[i]);
if(b[i]=='N')
{
temp1+=a[i];
temp2-=a[i];
}
else if(b[i]=='L')
temp1+=a[i];
else
temp2-=a[i];
}
if(k>=temp2&&k<=temp1)
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}
return 0;
}