HDU 2141 Can you find it?

三数求和问题解决
本文探讨了一种解决三数求和问题的有效算法。通过将两数之和存入集合并搜索第三数的方法,实现了时间与空间的平衡,避免了内存溢出的问题。

Can you find it?

Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others)
Total Submission(s): 24455 Accepted Submission(s): 6191

Problem Description
Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate if you can find the three numbers Ai, Bj, Ck, which satisfy the formula Ai+Bj+Ck = X.

Input
There are many cases. Every data case is described as followed: In the first line there are three integers L, N, M, in the second line there are L integers represent the sequence A, in the third line there are N integers represent the sequences B, in the forth line there are M integers represent the sequence C. In the fifth line there is an integer S represents there are S integers X to be calculated. 1<=L, N, M<=500, 1<=S<=1000. all the integers are 32-integers.

Output
For each case, firstly you have to print the case number as the form “Case d:”, then for the S queries, you calculate if the formula can be satisfied or not. If satisfied, you print “YES”, otherwise print “NO”.

Sample Input
3 3 3
1 2 3
1 2 3
1 2 3
3
1
4
10

Sample Output
Case 1:
NO
YES
NO


water problem 感觉奇奇怪怪的 G++ MLE C++跑了1400+ms 在web-diy里 GUN C++ 0MS…
把Ai+Bj+Ck全放set里 O(n*m*l)+O(s*log(n*m*l))的复杂度 不过显然会爆内存
那就把Ai+Bj放set里 时间换点空间
每次遍历C 如果set中存在x-C[k] Yes 否则 No
O(s*m*log(l*n)+O(l*n)的复杂度

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include<cmath>
#include<list>
#include<cstring>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>

#define sci(a) scanf("%d",&a)
#define scll(a) scanf("%lld",&a)
#define scd(a) scanf("%lf",&a)
#define scs(a) scanf("%s",a)
#define pri(a) printf("%d\n",a);
#define prd4(a) printf("%.4lf\n",a);
#define prd2(a) printf("%.2lf\n",a);
#define prs(a) printf("%s\n",a);

#define N 505
int elem[3][N];
set<ll>exist;

int main(){
    //freopen("/home/lu/文档/r.txt","r",stdin);
    //freopen("/home/lu/文档/w.txt","w",stdout);

    int l,n,m,s,t=1;
    ll x;
    while(~scanf("%d%d%d",&l,&n,&m)){
        exist.clear();
        for(int i=0;i<l;++i)
            sci(elem[0][i]);
        for(int i=0;i<n;++i)
            sci(elem[1][i]);
        for(int i=0;i<m;++i)
            sci(elem[2][i]);
        for(int i=0;i<l;++i)
            for(int j=0;j<n;++j)
                exist.insert((ll)elem[0][i]+elem[1][j]);
        sci(s);
        printf("Case %d:\n",t++);
        for(int i=0;i<s;++i){
            scll(x);
            bool ex=false;
            for(int i=0;i<m&&(!ex);++i)
                ex=exist.find(x-elem[2][i])!=exist.cend();
            prs(ex?"YES":"NO");
        }
    }
    return 0;
}
要解决被HDU WAF(Web应用防火墙)拒绝的问题并调整代码,可从以下几个方面着手: ### 网络访问规范 - **IP限制**:HDU WAF可能因IP存在异常访问行为而拒绝访问。检查IP是否被列入黑名单,若因频繁请求被临时封禁,可等待封禁时间结束或联系HDU相关技术支持人员说明情况,请求解封。 - **请求频率**:过度频繁的请求会触发WAF的防护机制。调整代码中的请求频率,添加适当的延迟。以下是Python中使用`time.sleep()`函数添加延迟的示例: ```python import time import requests for i in range(10): response = requests.get('http://acm.hdu.edu.cn') print(response.text) time.sleep(1) # 每次请求间隔1秒 ``` ### 代码合规性 - **请求头设置**:确保代码中的请求头信息符合正常浏览器的请求头格式。部分WAF会根据请求头信息判断请求是否正常。以下是Python中设置请求头的示例: ```python import requests headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} response = requests.get('http://acm.hdu.edu.cn', headers=headers) print(response.text) ``` - **请求参数**:检查代码中发送的请求参数是否包含恶意字符或异常内容。确保参数符合题目要求和网站规定。 ### 代码逻辑与题目要求 - **理解题目意思**:仔细阅读题目描述,确保代码逻辑符合题目要求。如VJ的题有很多格式要求,多打一个空格、没换行系统就会评测为presentation error,所以要在代码中严格遵循题目要求的输出格式[^2]。 - **优化算法复杂度**:若代码运行时间过长或占用资源过多,可能会被WAF视为异常请求。对代码中的算法进行优化,降低时间和空间复杂度。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值