题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6140
题意:有n个数,每个数都对应一个下标。这n个数的原型是a_i,当对应下标是N时,这个数可正可负;当下标是L时,这个数是正数;D时是负数。 问是否可以从这n个数中选择任意个数,使其和恰好等于k。
题解:更新上界和下界,判断k是否在其中间。
ps:我怎么感觉哪儿不对 @_ @
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAX = 1005;
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
int a[MAX];
char b[MAX];
int main()
{
int t, n, k, uplimt, downlimt;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n, &k);
for(int i = 0; i < n; ++i)
scanf("%d", &a[i]);
for(int i = 0; i < n; ++i)
cin >> b[i];//scanf(" %c",b[i]);
uplimt = 1;
downlimt = -1;
for(int i = 1; i < n; ++i)
{
if(b[i] == 'N')
uplimt += a[i], downlimt -= a[i];
else if(b[i] == 'L')
uplimt += a[i];///上界
else
downlimt -= a[i];///下界
}
if(uplimt >= k && downlimt <= k)
puts("yes");
else
puts("no");
}
return 0;
}