Test instructions is for you. Three arrays have n m k number from three arrays each take a number can be equal to s
At first I didn't notice that the number could be negative WA many times
Don't think direct violence definitely timed out
Now combines two numbers with a small number of binary enumerators (very time-saving) with fewer violent enumerators
#include <stdio.h> #include <string.h> #include <iostream>using namespaceStd; int Main () { int Num1[510],Num2[510],Num3[510]; int Mark[300000]; int I,J,K,P,D=1; int N,M; while (~scanf("%d%d%d",&N,&M,&K) { for( I=1;I<=N;I++)scanf("%d",&Num1[I]); for (I=1;I<=M;I++)scanf("%d",&Num2[I]);P=0; for (I=1;I<=N;I++) for (J=1;J<=M;J++)Mark[++P]=Num1[I]+Num2[J];Sort(Mark+1,Mark+1+P); for (I=1;I<=K;I++)scanf("%d",&Num3[I]);Sort(Num3+1,Num3+1+K); int S;scanf("%d",&S);Printf("Case%d:\n",D++); while (S --) { int Sum;scanf("%d",&Sum); int Flash=0; int Left,Right,Mid; for (I=1;I<=K;I++) {Left=1;Right=P; while (Left<=Right) {Mid=(Left+Right)/2; if (Mark[Mid]>Sum-Num3[I])Right=Mid-1; else if (Mark[Mid]<Sum-Num3[I])Left=Mid+1; Else {Flash=1; Break ; }} if( Flash ) break; } if( Flash)Printf("yes\n"); Else Printf("no\n"); }} return 0;}
Hdu 21,412 min