hdu 5183 Negative and Positive (前缀和加强版,强数据。。)

本文介绍了一种使用输入挂结合Hash方法解决特定算法问题的技术。通过案例演示了如何利用自定义输入文件和Hash表快速查找匹配项,实现对序列的高效验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:
。。。
思路:
恩。
这道题数据太强了,以至于不用输入挂过不了 = =
我是用的输入挂+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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值