2017多校8-1008 Hybrid Crystals

来源:互联网 发布:北航网络教育平台 编辑:程序博客网 时间:2024/05/23 19:54

题目

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 (ii-th crystal has an energy level of aiai). 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 aiai, it emits +ai+ai units of energy.
  • For a dark-side crystal of energy level aiai, it emits −ai−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+ai or −ai−ai units of energy, depending on which side it has been tuned to.

Given nn crystals’ energy levels aiai and types bibi (1≤i≤n1≤i≤n), bi=Nbi=N means the ii-th crystal is a neutral one, bi=Lbi=L means a Light one, and bi=Dbi=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 kk.

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=Na1=1,b1=N.

Second, the Council has arranged the other n−1n−1 crystals in a way that
ai≤∑j=1i−1aj[bj=N]+∑j=1i−1aj[bi=L∩bj=L]+∑j=1i−1ajbi=D∩bj=D.
ai≤∑j=1i−1aj[bj=N]+∑j=1i−1aj[bi=L∩bj=L]+∑j=1i−1ajbi=D∩bj=D.

[cond][cond] evaluates to 11 if condcond holds, otherwise it evaluates to 00.

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}S⊆{1,2,…,n} and values sisi for all i∈Si∈S such that

∑i∈Sai∗si=k,
∑i∈Sai∗si=k,

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


输入

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

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

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

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


输出

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


样例输入

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


样例输出

yes
no


题意

给n个晶石,对于第i个晶石,L能加ai点能量,R能减ai点能量,N能加或减去ai点能量。问最后是否能获得k点能量。


Hint

首先第一个晶石能构成[-1,1]的所有整数,上面的那个很长很长的不等式能保证能取到左右区间的所有整数(即连续区间),故只要判断所有晶石的能量达到的最大值和最小值,看k是否在这区间里即可


#include<bits/stdc++.h>using namespace std ;typedef long long LL ;typedef pair<int , int> P ;int a[1000 + 10] ;int main(){    int T ;    scanf("%d",&T);    while(T--){        int n , k ;        scanf("%d %d",&n ,&k) ;        for(int i = 1 ; i<= n ; i++){            scanf("%d",&a[i]) ;        }        int L = 0 , R = 0 ;        char x ,o;        getchar() ;        for(int i = 1 ; i <= n ; i++){            scanf("%c%c",&x,&o) ;            if(x =='N'){                L -= a[i] ,R += a[i] ;            }            else if(x == 'L'){                R +=a[i] ;            }            else if(x == 'D'){                L -=a[i] ;            }        }        printf("%s\n", (k <= R && k >= L) ? "yes" : "no") ;    }    return 0 ;}

原创粉丝点击