题目链接:HDU 5183
Problem Description
When given an array $(a_0,a_1,a_2,⋯a_{n−1})$ and an integer $K$, you are expected to judge whether there is a pair $(i,j)(0≤i≤j<n)$ which makes that $NP−sum(i,j)$ equals to $K$ true. Here $NP−sum(i,j)=a_i−a_{i+1}+a_{i+2}+⋯+(−1)^{j−i}a_j$
Input
Multi test cases. In the first line of the input file there is an integer $T$ indicates the number of test cases.
In the next $2∗T$ lines, it will list the data for each test case.
Each case occupies two lines, the first line contain two integers $n$ and $K$ which are mentioned above.
The second line contain $(a_0,a_1,a_2,⋯a_{n−1})$ separated by exact one space.
[Technical Specification]
All input items are integers.
$0<T≤25,1≤n≤1000000,−1000000000≤a_i≤1000000000,−1000000000≤K≤1000000000$
Output
For each case,the output should occupies exactly one line. The output format is Case #id: ans, here id is the data number starting from 1; ans is “Yes.” or “No.” (without quote) according to whether you can find $(i,j)$ which makes $PN−sum(i,j)$ equals to $K$.
See the sample for more details.
Sample Input
1 | 2 |
Sample Output
1 | Case #1: Yes. |
Hint
If input is huge, fast IO method is recommended.
Source
Solution
题意
给定长度为 $n$ 的数组,问是否存在一段区间其加减交错的和等于 $k$。
思路
对该数组求前缀和然后哈希就行。
不过这题不同的 set 过不了。
所以要手写哈希。
当然 unordered_set 也可以过。
Code
1 |
|