HDU 2017 多校联合训练赛8 1008 6140 Hybrid Crystals

来源:互联网 发布:保温杯套子 淘宝 编辑:程序博客网 时间:2024/06/08 18:24

Hybrid Crystals

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


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 (1in),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 exactlyk.

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 n1 crystals in a way that
aij=1i1aj[bj=N]+j=1i1aj[bi=Lbj=L]+j=1i1aj[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 setS{1,2,,n} and values si for all iS 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 (1n103) and k (|k|106).

The next line contains n integer a1,a2,...,an (0ai103).

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
25 9 1 1 2 3 4N N N N N 6 -101 0 1 2 3 1N L L L L D
 

Sample Output
yesno
 

Source
2017 Multi-University Training Contest - Team 8


题目大意
给出n个数,每个数对应一个属性,L代表正数,D代表负数,N代表可能为正数可能为负数,现在,要求判断能不能用给出的数相加组成k。
【敲黑板】题目为了减低难度,还给出了两个条件,这也正是解题的关键。
1.每组数据中的第一个数a1,数值一定是1,并且属性一定为N。
2.除第一个数之外的n-1个数都满足条件 aij=1i1aj[bj=N]+j=1i1aj[bi=Lbj=L]+j=1i1aj[bi=Dbj=D](2in).
假设ai属性为L,ai <= 下标在1~i-1的所有属性为N的数之和 + 下标在1~i-1的所有属性为L的数之和;
假设ai属性为D,ai <= 下标在1~i-1的所有属性为N的数之和 + 下标在1~i-1的所有属性为D的数之和;
假设ai属性为N,ai <= 下标在1~i-1的所有属性为N的数之和 。


题目分析
因为数据范围n<=1000,所以记录所有能凑出来的小于10^6的数,一定会TLE的。所以理解题目给出的附加条件就显得尤为重要了。
(属性为N的数既可以作为L也可以作为D看待),如果我们只关注序列中属性为L和N的数,那么第一个数为1,第二个数<=1,即0或1;第三个数<=前两个数之和,依次类推。现在假设序列中属性为L和N的前4个数为1、1、2、3。那么前两个数的和是2,他们可以组成小于等于2的所有自然数;前三个数的和为4,并且可以组成小于等于4的所有自然数;再加上第四个数3,那么前四个数可以组成所有[1, 4]和[5, 7]的所有数。所以,我们只要确定所有数能组成的最大数,就能确定我们能组成0~最大数区间内的所有数。属性为D和N的数同理可得我们能组成的最小数。最后只需要判断k是否在 最大数~最小数 的区间内,即可判断能否组成k。。

程序其实有一个漏洞,当k==0的时候,程序作出的判断有可能是错的,但是按照题意,组成一个能量为0的水晶好像并没有什么用,并且数据也没有涉及。


代码
#include <cstdio>#include <cstring>#include <iostream>using namespace std;int T, n, k;int v[1005];char c[1005];int l, r;int main(){    scanf ("%d",&T);    while (T--)    {        scanf ("%d %d",&n,&k);        for (int i=0; i<n; ++i)        {            scanf ("%d",&v[i]);        }        getchar();        for (int i=0; i<n; ++i)        {            scanf ("%c",&c[i]);            getchar();        }        l = r = 0;        for (int i=0; i<n; ++i)        {            if (c[i] == 'L')                r += v[i];            else if(c[i] == 'D')                l -= v[i];            else if(c[i] == 'N')            {                r += v[i];                l -= v[i];            }        }        if (k<=r && k>=l)            printf("yes\n");        else            printf("no\n");    }    return 0;}


参考
http://blog.csdn.net/young_12138/article/details/77341341