hdu 5183 Negative and Positive (NP)

时间:2023-03-09 09:53:04
hdu 5183 Negative and Positive (NP)

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5183

Negative and Positive (NP)

Description

When given an array $\left( {{a_0},{a_1},{a_2}, \cdots {a_{n - 1}}} \right)$ and an integer $K$, you are expected to judge whether there is a pair $(i,j)\ (0 \leq i \leq j < n)$ which makes that $NP−sum(i,j)$ equals to $K$ true. Here $NP-sum(i,j)={a_i}{\rm{ - }}{a_{i{\rm{ + 1}}}}{\rm{ + }}{a_{i{\rm{ + }}2}}{\rm{ + }} \cdots {\rm{ + ( - 1}}{{\rm{)}}^{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 $\left( {{a_0},{a_1},{a_2}, \cdots {a_{n - 1}}} \right)$separated by exact one space.
[Technical Specification]
All input items are integers.
$0 < T \leq 25,1 \leq n \leq 1000000,-1000000000 \leq ai \leq 1000000000,-1000000000 \leq K \leq 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

2
1 1
1
2 1
-1 0

Sample Output

Case #1: Yes.
Case #2: No.

哈希大法好。。

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<set>
using std::set;
using std::sort;
using std::pair;
using std::swap;
using std::queue;
using std::multiset;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 1000007;
const int INF = 0x3f3f3f3f;
typedef long long ll;
struct Hash_Set {
ll num[N << 1];
int tot, head[N], next[N];
inline void init() {
tot = 0, cls(head, -1);
}
inline void insert(ll val) {
int u = abs(val) % N;
num[tot] = val, next[tot] = head[u], head[u] = tot++;
}
inline bool find(ll val) {
int u = abs(val) % N;
for (int i = head[u]; ~i; i = next[i]) {
if (num[i] == val) return true;
}
return false;
}
}hash;
ll arr[N], sum[N];
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
ll k;
int t, n, c = 1;
scanf("%d", &t);
while (t--) {
hash.init();
scanf("%d %lld", &n, &k);
for (int i = 1; i <= n; i++) scanf("%lld", &arr[i]);
for (int i = 1; i <= n; i++) {
sum[i] = sum[i - 1] + (i & 1 ? arr[i] : -arr[i]);
}
bool f = false;
for (int i = n; i > 0; i--) {
hash.insert(sum[i]);
if (f) break;
if (i & 1) {
if (hash.find(sum[i - 1] + k)) {
f = true;
break;
}
} else {
if (hash.find(sum[i - 1] - k)) {
f = true;
break;
}
}
}
printf("Case #%d: %s\n", c++, f ? "Yes." : "No.");
}
return 0;
}