|
|
back to boardPlease help! Time limit exceeded! #include<stdio.h> int main(void){ long n,m,i,j; int flag; int A[50001],B[50001]; scanf("%ld",&n); for(i=1;i<=n;i++) scanf("%d",&A[i]); scanf("%ld",&m); for(j=1;j<=m;j++) scanf("%d",&B[j]); flag=0; for(i=1;i<=n;i++) for(j=1;j<=m;j++) if(A[i]+B[j]==10000) flag=1; if(flag) printf("YES"); else printf("NO"); return 0; } Re: Please help! Time limit exceeded! not an optimal algo... O(n^2), this problem can be solved with O(n+m) you should think a little more. If you need code, send an email on andrewshmig@gmail.com Edited by author 06.05.2012 00:27 Re: Please help! Time limit exceeded! or u can use BinarySearch to search second value. for example in the first test: 1) u take -175 value, 2) try to find 10175 in second list (using binary search) (because -175 + 10175 == 10000) ... good luck! Edited by author 08.05.2012 00:20 Re: Please help! Time limit exceeded! >not an optimal algo... O(n^2), this problem can be solved with O(n+m) This problem can be solved with O(n). ) Edited by author 02.06.2012 20:52 Re: Please help! Time limit exceeded! Posted by saibogo 18 Jun 2012 13:22 Use the associative containers Re: Please help! Time limit exceeded! you can use HashSet, it's faster Re: Please help! Time limit exceeded! Posted by Ramil 15 Aug 2012 15:35 use the two-pointers method or boolean set. It's enough fast for this problem. |
|
|