【题解】
排序,然后枚举前两个数,再用类似two pointer的思想扫第三个数即可,不需要二分。复杂度n方。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<cmath> 5 #define LL long long 6 #define rg register 7 #define N 10010 8 using namespace std; 9 int n,tot,a[N]; 10 struct rec{ 11 int x,y,z; 12 }b[N]; 13 inline int read(){ 14 int k=0,f=1; char c=getchar(); 15 while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar(); 16 while('0'<=c&&c<='9')k=k*10+c-'0',c=getchar(); 17 return k*f; 18 } 19 int main(){ 20 n=read(); 21 for(rg int i=1;i<=n;i++) a[i]=read(); 22 sort(a+1,a+1+n); 23 for(rg int i=1;i<n-1;i++) 24 for(rg int j=i+1;j<n;j++){ 25 int tmp=-a[i]-a[j],pointer=n; 26 while(a[pointer]>tmp&&pointer>j) pointer--; 27 if(a[pointer]==tmp&&pointer>j) b[++tot]=(rec){a[i],a[j],a[pointer]}; 28 if(pointer<=j) break; 29 } 30 if(!tot){ 31 puts("No Solution"); return 0; 32 } 33 for(rg int i=1;i<=tot;i++) printf("%d %d %d ",b[i].x,b[i].y,b[i].z); 34 return 0; 35 }