Can you find it?
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others)Total Submission(s): 12153 Accepted Submission(s): 3157
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
Author
wangye
Source
HDU 2007-11 Programming Contest
循环做二分,如果三者直接相加再进行sort排序,排列组合是500×500×500,肯定会超时,所以先加和前两个数组500×500,后一个通过循环二分查找来实现加和。
其他的就是输入输出之类的问题了。
S1:
#include<stdio.h>
#include<algorithm>
using namespace std;
int A[521], B[521], f[251111], g, C[521], L, N, M, i, j, k, X, S;
int binary(int X,int M,int g)
{
for(int h = 1; h <= M; h++)
{
int l = 1, r = g;
while(l <= r)
{
int mid = (l+r)/2;
if(X > C[h] + f[mid]) l = mid + 1;
else if(X < C[h] + f[mid]) r = mid - 1;
else return 1;
}
}
return 0;
}
int main()
{
int tt = 0;
while(scanf("%d%d%d", &L, &N, &M) != EOF)
{
tt++;
for(i = 1; i <= L; i++)
scanf("%d", &A[i]);
for(j = 1; j <= N; j++)
scanf("%d", &B[j]);
g = 1;
for(i = 1; i <= L; i++)
for(j = 1; j <= N; j++)
{
f[g] = A[i] + B[j];
g++;
}
sort(f + 1, f + g);
for(k = 1; k <= M; k++)
scanf("%d", &C[k]);
g--;
scanf("%d", &S);
printf("Case %d:\n", tt);
for(i = 1; i <= S; i++)
{
scanf("%d", &X);
printf(binary(X, M, g) ? "YES\n" : "NO\n");
}
}
return 0;
}
sort(a + 0,a + l + 1);第一位是数组名+某数,加0为a[0],是排序开始的元素。第二位是数组名加结束排序的位置(l)再加一,不是排序结束的位置,而是排序真实结束位置的后一位。所以也可以对数组C排序,虽然并没有任何代码上的帮助。
S2:
#include<stdio.h>
#include<algorithm>
using namespace std;
int A[521], B[521], f[251111], g, C[521], L, N, M, i, j, k, X, S;
int binary(int X,int M,int g)
{
for(int h = 1; h <= M; h++)
{
int l = 1, r = g;
while(l <= r)
{
int mid = (l+r)/2;
if(X > C[h] + f[mid]) l = mid + 1;
else if(X < C[h] + f[mid]) r = mid - 1;
else return 1;
}
}
return 0;
}
int main()
{
int tt = 0;
while(scanf("%d%d%d", &L, &N, &M) != EOF)
{
tt++;
for(i = 1; i <= L; i++)
scanf("%d", &A[i]);
for(j = 1; j <= N; j++)
scanf("%d", &B[j]);
g = 1;
for(i = 1; i <= L; i++)
for(j = 1; j <= N; j++)
{
f[g] = A[i] + B[j];
g++;
}
sort(f + 1, f + g);
for(k = 1; k <= M; k++)
scanf("%d", &C[k]);
sort(C + 1, C + M + 1);
g--;
scanf("%d", &S);
printf("Case %d:\n", tt);
for(i = 1; i <= S; i++)
{
scanf("%d", &X);
printf(binary(X, M, g) ? "YES\n" : "NO\n");
}
}
return 0;
}
但是通过了不代表就是完全正确的,其中虽然保证了每个输入的数据都不超过32位整数(不超出int),但是三个整数相加仍然有可能会超出int,故正式上来讲要用long long型!之所以用int也能对,完全是因为当超出int时,虽然三者相加超出了int,但是X不会超出int故实际上超出int的部分没有对代码造成影响(就算X能超出int,输进去的时候就是负数,三者加和也是负数~~~依然能够查询到)。
S3:
#include<stdio.h>
#include<algorithm>
using namespace std;
long long A[521], B[521], f[251111], g, C[521], L, N, M, i, j, k, X, S;
long long binary(long long X,long long M,long long g)
{
for(long long h = 1; h <= M; h++)
{
long long l = 1, r = g;
while(l <= r)
{
long long mid = (l+r)/2;
if(X > C[h] + f[mid]) l = mid + 1;
else if(X < C[h] + f[mid]) r = mid - 1;
else return 1;
}
}
return 0;
}
int main()
{
long long tt = 0;
while(scanf("%I64d%I64d%I64d", &L, &N, &M) != EOF)
{
tt++;
for(i = 1; i <= L; i++)
scanf("%I64d", &A[i]);
for(j = 1; j <= N; j++)
scanf("%I64d", &B[j]);
g = 1;
for(i = 1; i <= L; i++)
for(j = 1; j <= N; j++)
{
f[g] = A[i] + B[j];
g++;
}
sort(f + 1, f + g);
for(k = 1; k <= M; k++)
scanf("%I64d", &C[k]);
sort(C + 1, C + M + 1);
g--;
scanf("%I64d", &S);
printf("Case %d:\n", tt);
for(i = 1; i <= S; i++)
{
scanf("%I64d", &X);
if (X < (f[1] + C[1]) || X > (f[g] + C[M]))
{
printf("NO\n");
}
else
{
printf(binary(X, M, g) ? "YES\n" : "NO\n");
}
}
}
return 0;
}
if else是发现三者相加其实超过了int的关键点~~~