Two-point lookup. Log each v[i], find k-v[i] in the array, modify v[i after the lookup is complete) Avoid duplicate output
#include <iostream>#include<algorithm>#include<vector>#include<cstdio>using namespaceStd;typedefLong LongLL;#defineINF 0x3f3f3f3fintmain () {vector<LL>VV; BOOLSign =false; LL T,k,n; scanf ("%lld%lld",&k,&N); for(intI=0; i<n;i++) {scanf ("%lld",&t); Vv.push_back (t); } sort (Vv.begin (), Vv.end ()); intL =vv.size (); for(intI=0; i<l;i++) { if(Vv[i]==inf)Continue; Vector<LL>:: Iterator it; if(Binary_search (Vv.begin (), Vv.end (), K-Vv[i])) It= Lower_bound (Vv.begin (), Vv.end (), K-Vv[i]); Else Continue; if(It!=vv.end () &&*it!=Vv[i]) { Sign=true; if(*it>Vv[i]) printf ("%lld%lld\n", vv[i],*it); Elseprintf ("%lld%lld\n",*It,vv[i]); *it =INF; } } if(!Sign ) printf ("No solution\n"); return 0;}
Three number and 0, in the previous code based on a slight modification can be.
#include <iostream>#include<algorithm>#include<vector>#include<cstring>#include<cstdio>using namespaceStd;typedefLong LongLL;#defineMAXN 1001#defineINF 0x3f3f3f3fBOOLBEEN[MAXN];BOOLAll_sign =false; LL N;vector<LL>VV;voidSolve (LL k,vector<ll>vv,ll POS) {memset (Been,false,sizeof(been)); for(LL i=pos+1; i<n;i++) { if(been[i]==true)Continue; Vector<LL>:: Iterator it; if(Binary_search (Vv.begin () +pos+1, Vv.end (), K-Vv[i])) It= Lower_bound (Vv.begin () +pos+1, Vv.end (), K-Vv[i]); Else Continue; if(It!=vv.end () &&*it!=Vv[i]) {All_sign=true; if(*it>Vv[i]) printf ("%lld%lld%lld\n",-k,vv[i],*it); Elseprintf ("%lld%lld%lld\n",-k,*It,vv[i]); Been[it-vv.begin ()] =true; } }}intMain () {LL temp,i; scanf ("%lld",&N); for(i=0; i<n;i++) {scanf ("%lld",&temp); Vv.push_back (temp); } sort (Vv.begin (), Vv.end ()); for(i=0; i<n;i++) { if(vv[i]>=0) Break; Temp=Vv[i]; Solve (-temp,vv,i); Been[i]=true; } if(!all_sign) printf ("No solution\n"); return 0;}
1001 in array and equal to K in number pairs 1090 3 numbers and 0