2 solutions
-
0
#include<bits/stdc++.h> using namespace std; int n, x, a[1000010],sum=0; int main(){ cin >> n; for(int i = 0;i < n; i++){ cin >> a[i]; } cin >> x; sort(a, a + n); for(int i = 0;i < n; i++){ int idx = lower_bound(a, a + n, x - a[i]) - a; if(a[idx] == x - a[i]) sum++; } cout << sum / 2; return 0; }
-
0
萌新写法:枚举+二分
#include <bits/stdc++.h> using namespace std; int n,m,size = 0,num[1000005]; int check(int x){ int left = -1,right = n,mid; while(left + 1 < right){ mid = (left + right) / 2; if(num[mid] > x){ right = mid; } else{ left = mid; } } if(num[right - 1] == x){ return 1; } return -1; } int main(){ int list; cin>>n; for(int i = 0 ; i < n ; i++){ cin>>num[i]; } sort(num,num+n); cin>>m; for(int i = 0 ; i < n ; i++){ list = check(m - num[i]); if(list != -1){ size++; } } cout<<size/2<<endl; return 0; }
- 1
Information
- ID
- 1102
- Time
- 1000ms
- Memory
- 20MiB
- Difficulty
- 6
- Tags
- # Submissions
- 76
- Accepted
- 21
- Uploaded By