题意:
。。。
思路:
恩。
这道题数据太强了,以至于不用输入挂过不了 = =
我是用的输入挂+hash
int t, n, k;
LL a[Maxn+5], f[2][Maxn+5];
const int Mod = (int)(1e6+7);
struct Ent{
LL val;
int nxt;
};
Ent buffer[Mod*2+5];
int head[Mod+5], tot;
void read(LL &x) {
char ch = getchar();
LL tag = 1;
x = 0;
while ((ch < '0' || ch > '9') && ch != '-') {
ch = getchar();
}
if (ch == '-') {
tag = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - 48;
ch = getchar();
}
x *= tag;
}
inline int Find(LL k) {
int key = k % Mod;
if (key < 0) key += Mod;
for (int i=head[key];i!=-1;i=buffer[i].nxt) {
if (buffer[i].val == k) return 1;
}
return 0;
}
inline void Add(LL k) {
//if (Find(k)) return;
int key = k % Mod;
if (key < 0) key += Mod;
buffer[tot++] = (Ent){k, head[key]};
head[key] = tot-1;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.in", "r", stdin);
#endif
SPEED_UP
scanf("%d", &t);
LL x;
rep(kase, 1, t) {
scanf("%d%d", &n, &k);
//printf("d: %d\n", n);
LL K = k;
f[0][0] = f[1][0] = 0;
//map<LL, int> m1, m2;
tot = 0;memset(head, -1, sizeof(head));
int ok = 0;
rep(i, 1, n) //scanf("%I64d", a+i);
{
if (i<n) read(a[i]);
else scanf("%I64d", a+i);
}
rep(i, 1, n) {
LL x = a[i];
if (i&1) {
f[0][i] = f[0][i-1]+x;
f[1][i] = f[1][i-1]-x;
}
else {
f[0][i] = f[0][i-1]-x;
f[1][i] = f[1][i-1]+x;
}
//printf("input %I64d: \n", x);
//printf("%I64d, %I64d\n", f[0][i], f[1][i]);
//if (m1[f[0][i]-K] > 0) {
if (Find(f[0][i]-K)) {
//printf("found: %I64d\n", f[0][i]-k*1ll);
ok = 1;break;
}
//if (m2[f[1][i]-K] > 0) {
if (Find(f[1][i]-K)) {
//printf("found: %I64d\n", f[1][i]-k*1ll);
ok = 1;break;
}
if (i&1) Add(f[1][i]);//m2[f[1][i]] += 1;
else Add(f[0][i]);//m1[f[0][i]] += 1;
if (f[0][i] == k) {
ok = 1;break;
}
}
printf("Case #%d: ",kase);
if (ok) puts("Yes.");
else puts("No.");
}
return 0;
}
再收藏一个神牛的代码
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <cstring>
#define Max(a, b) ((FASTBUFFER = ((a) - (b)) >> 31), ((b) & FASTBUFFER | (a) & ~FASTBUFFER))
#define Min(a, b) ((FASTBUFFER = ((a) - (b)) >> 31), ((a) & FASTBUFFER | (b) & ~FASTBUFFER))
#define Swap(a, b) (a ^= b, b ^= a, a ^= b)
#define OO 2147483647
#define priority_queue PQ
using namespace std;
int FASTBUFFER;
const int N = 1000005;
int test;
void read(int &x) {
char ch = getchar();
int tag = 1;
x = 0;
while ((ch < '0' || ch > '9') && ch != '-') {
ch = getchar();
}
if (ch == '-') {
tag = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - 48;
ch = getchar();
}
x *= tag;
}
int n, k, hashTag, hashNow[N];
long long s[N], temp[N];
void work() {
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; i++) {
int x;
if (i == n) {
scanf("%d", &x);
} else {
read(x);
}
if (i & 1) {
s[i] = s[i - 1] + x;
} else {
s[i] = s[i - 1] - x;
}
temp[i] = s[i];
}
sort(temp + 1, temp + n + 1);
int tot = unique(temp + 1, temp + n + 1) - temp - 1;
hashTag++;
for (int i = n; i >= 0; i--) {
long long need;
if (i & 1) {
need = -((long long)k - s[i]);
} else {
need = s[i] + k;
}
long long *xx = lower_bound(temp + 1, temp + n + 1, need);
if (*xx == need && hashNow[xx - temp] == hashTag) {
printf("Yes.\n");
return;
}
hashNow[lower_bound(temp + 1, temp + n + 1, s[i]) - temp] = hashTag;
}
printf("No.\n");
}
int tt;
int main() {
//freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
scanf("%d", &test);
while (test--) {
printf("Case #%d: ", ++tt);
work();
}
return 0;
}