HDU 6140 Hybrid Crystals【思维】

来源:互联网 发布:源码教程 编辑:程序博客网 时间:2024/06/04 18:35

题目来戳呀

Problem Description

Kyber crystals, also called the living crystal or simply the kyber, and known as kaiburr crystals in ancient times, were rare, Force-attuned crystals that grew in nature and were found on scattered planets across the galaxy. They were used by the Jedi and the Sith in the construction of their lightsabers. As part of Jedi training, younglings were sent to the Crystal Caves of the ice planet of Ilum to mine crystals in order to construct their own lightsabers. The crystal’s mix of unique lustre was called “the water of the kyber” by the Jedi. There were also larger, rarer crystals of great power and that, according to legends, were used at the heart of ancient superweapons by the Sith.

— Wookieepedia

Powerful, the Kyber crystals are. Even more powerful, the Kyber crystals get combined together. Powered by the Kyber crystals, the main weapon of the Death Star is, having the firepower of thousands of Star Destroyers.

Combining Kyber crystals is not an easy task. The combination should have a specific level of energy to be stablized. Your task is to develop a Droid program to combine Kyber crystals.

Each crystal has its level of energy (i-th crystal has an energy level of ai). Each crystal is attuned to a particular side of the force, either the Light or the Dark. Light crystals emit positive energies, while dark crystals emit negative energies. In particular,

  • For a light-side crystal of energy level ai, it emits +ai units of energy.
  • For a dark-side crystal of energy level ai, it emits −ai units of energy.

Surprisingly, there are rare neutral crystals that can be tuned to either dark or light side. Once used, it emits either +ai or −ai units of energy, depending on which side it has been tuned to.

Given n crystals’ energy levels ai and types bi (1≤i≤n), bi=N means the i-th crystal is a neutral one, bi=L means a Light one, and bi=D means a Dark one. The Jedi Council asked you to choose some crystals to form a larger hybrid crystal. To make sure it is stable, the final energy level (the sum of the energy emission of all chosen crystals) of the hybrid crystal must be exactly k.

Considering the NP-Hardness of this problem, the Jedi Council puts some additional constraints to the array such that the problem is greatly simplified.

First, the Council puts a special crystal of a1=1,b1=N.

Second, the Council has arranged the other n−1 crystals in a way that
aii1j=1aj[bj=N]+i1j=1aj[bi=Lbj=L]+i1j=1aj[bi=Dbj=D](2in)
[cond] evaluates to 1 if cond holds, otherwise it evaluates to 0.

For those who do not have the patience to read the problem statements, the problem asks you to find whether there exists a set S⊆{1,2,…,n} and values si for all i∈S such that

iSaisi=k

where si=1 if the i-th crystal is a Light one, si=−1 if the i-th crystal is a Dark one, and si∈{−1,1} if the i-th crystal is a neutral one.

Input

The first line of the input contains an integer T, denoting the number of test cases.

For each test case, the first line contains two integers n (1≤n≤103) and k (|k|≤106).

The next line contains n integer a1,a2,…,an (0≤ai≤103).

The next line contains n character b1,b2,…,bn (bi∈{L,D,N}).

Output

If there exists such a subset, output “yes”, otherwise output “no”.

Sample Input

2

5 9
1 1 2 3 4
N N N N N

6 -10
1 0 1 2 3 1
N L L L L D

Sample Output

yes
no

Source

2017 Multi-University Training Contest - Team 8

题意

给你长度为n的a数列与b数列,a数列表示这个数的大小,b表示这个数对求和sum的贡献方式,一共有3种方式:N:可增可减 D:减 K:加。
问是否可以从这n个数中选择任意个数,使其和恰好等于k。
还有两个约束条件:
1.a1=1 ,b1=N
2.aii1j=1aj[bj=N]+i1j=1aj[bi=L?bj=L]+i1j=1aj[bi=D?bj=D](2in)

想法

给你一个数,这个数会对上下界有贡献,我们自然而然的想到,如果这个数的贡献是正的,上界会增加;如果是负的,下界会减小(总的范围还是大的);如果可正可负,上界会增加,下界会减小。然后我们只要判断k是否在这个上下界范围内就可以了。

但是,这是有问题的TAT。
比如,给你1 3 3 ,每个数的状态都是L,这样增加之后的上界应该是7,按理说应该是(1,7),但是当k=5时是不可以的,发现构不出这个数了。
所以友善的出题人为了保护我们的头发XP,给出我们第二个约束条件来保证我们这样变化上下界时,构成的区间内的数一定是连续的,即可以用所给的数来构成0-上界内的任何一个数。

至于这个约束条件的理解…O__O “…
aii1j=1aj[bj=N]+i1j=1aj[bi=L?bj=L]+i1j=1aj[bi=D?bj=D](2in)
是说,N的话全都加起来不在乎,如果是L的话,就把是L的值都加起来,反之,把是D的值都加起来
它间接表明第一个下标为L的数,只能取0或1;
(在不出现N时)第二个下标为L的数只能是0或1或2;
(在不出现N时)第三个下标为L的数取值范围为0-4;
(在不出现N时)第四个下标为L的数取值范围为0-8; 。

同理,如果是D的话就能确定下界以保证数值是连续的。

于是就很简单的敲代码辣~

#include<bits/stdc++.h>using namespace std;const int maxn=1100;int a[maxn];char b[maxn];int main(){    int t,n,k,up,down;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&k);        for(int i=1;i<=n;++i)            scanf("%d",&a[i]);        for(int i=1;i<=n;++i)            scanf(" %c",&b[i]);        up=1,down=-1;        for(int i=2;i<=n;++i)        {            if(b[i]=='N')            {                up+=a[i];                down-=a[i];            }            else if(b[i]=='L')                up+=a[i];            else                down-=a[i];        }        if(k>=down&&k<=up)            printf("yes\n");        else            printf("no\n");    }    return 0;}

ps:真的是做阅读理解了啊QAQ看完上句忘下句,结果还这么简单TAT