原题
correctly and cost less time can date with them.
The problem is that : give you n positive integers and an integer k. You need to calculate how many different solutions the equation x + y = k has . x and y must be among the given n integers. Two solutions are different if x0 != x1 or y0 != y1.
Now smart Acmers, solving the problem as soon as possible. So you can dating with pretty girls. How wonderful!
2 5 4 1 2 3 4 5 8 8 1 4 5 7 8 9 2 6
3 5
源码:
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; int a[100005]; int main() { int t; int n,m; int i,j; int k; cin>>t; while(t--) { cin>>n>>k; for(i=1; i<=n; i++) scanf("%d",&a[i]); a[0]=-99999; sort(a,a+n+1); int count=0; int big,small; int mid; for(i=1; i<=n; i++) { if(a[i-1]!=a[i]) { big=n; small=1; while(small<=big) { mid=(big+small)/2; if(a[mid]+a[i]==k) { count++; break; } if(a[mid]+a[i]>k) big=mid-1; else small=mid+1; } } } cout<<count<<endl; } return 0; }