(# 现成的STL直接逮住用
要想了解更多关于lower_bound/upper_bound -> 【C++函数速查】lower_bound和upper_bound使用方法详细解读

Vector版

  1. #include<bits/stdc++.h>
  2. #define ULL unsigned long long
  3. #define LL long long
  4. #define endl '\n'
  5. #define debug(a) cout<<#a<<"="<<a<<endl;
  6. #define PII pair<int,int>
  7. using namespace std;
  8. const int N = 1 *1e7 + 10,M = 5 * 1e3 + 10,inf = 0x3f3f3f3f;
  9. int n,m;
  10. int find(int x,vector<int> a)
  11. {
  12. return lower_bound(a.begin(),a.end(),x) - a.begin();
  13. }
  14. void solve()
  15. {
  16. cin>>n;
  17. vector<int> arr(n);
  18. for(int i=0;i<n;i++) cin>>arr[i];
  19. cin>>m;
  20. sort(arr.begin(),arr.end());
  21. // 1 2 4 5
  22. for(auto x : arr)
  23. {
  24. int t = m - x; //求出要找出的数
  25. int i = find(t,arr); //找到第一个大于等于t的下标
  26. if(arr[i] == t && arr[i] != x) //该下标的数是等于t并且不是自己
  27. {
  28. cout<<x<<' '<<t;
  29. exit(0);
  30. }
  31. }
  32. cout<<"No"<<endl;
  33. }
  34. int main()
  35. {
  36. ios::sync_with_stdio(0);cin.tie(nullptr),cout.tie(0);
  37. int _=1;
  38. // cin>>_;
  39. while(_--)
  40. {
  41. solve();
  42. }
  43. return 0;
  44. }
  45. /**
  46. * author: Nijika_jia
  47. * created: 2024.10.12 23:02:03
  48. */

常规数组版

  1. #include<bits/stdc++.h>
  2. #define ULL unsigned long long
  3. #define LL long long
  4. #define endl '\n'
  5. #define debug(a) cout<<#a<<"="<<a<<endl;
  6. #define PII pair<int,int>
  7. using namespace std;
  8. const int N = 1 *1e7 + 10,M = 5 * 1e3 + 10,inf = 0x3f3f3f3f;
  9. int n,m;
  10. int arr[N];
  11. int find(int x,int a[])
  12. {
  13. return lower_bound(a,a+n,x) - a;
  14. }
  15. void solve()
  16. {
  17. cin>>n;
  18. for(int i=0;i<n;i++) cin>>arr[i];
  19. cin>>m;
  20. // 1 2 4 5
  21. sort(arr,arr+n);
  22. for(int i=0;i<n;i++)
  23. {
  24. int t = m - arr[i]; //求出要找出的数
  25. int j = find(t,arr); //找到第一个大于等于t的下标
  26. if(arr[j] == t && arr[j] != arr[i]) //该下标的数是等于t并且不是自己
  27. {
  28. cout<<arr[i]<<' '<<t;
  29. exit(0);
  30. }
  31. }
  32. cout<<"No"<<endl;
  33. }
  34. int main()
  35. {
  36. ios::sync_with_stdio(0);cin.tie(nullptr),cout.tie(0);
  37. int _=1;
  38. // cin>>_;
  39. while(_--)
  40. {
  41. solve();
  42. }
  43. return 0;
  44. }
  45. /**
  46. * author: Nijika_jia
  47. * created: 2024.10.12 23:02:03
  48. */

手写二分版

  1. #include<bits/stdc++.h>
  2. #define ULL unsigned long long
  3. #define LL long long
  4. #define endl '\n'
  5. #define debug(a) cout<<#a<<"="<<a<<endl;
  6. #define all(v) v.begin(), v.end()
  7. #define PII pair<int,int>
  8. using namespace std;
  9. const int N = 1 *1e6 + 10,M = 5 * 1e3 + 10,inf = 0x3f3f3f3f;
  10. int n,m;
  11. int a[N];
  12. int find(int x)
  13. {
  14. int l = 0 , r = n;
  15. while (l < r)
  16. {
  17. int mid = l + r >> 1;
  18. if(a[mid] == x) return mid;
  19. if(a[mid] < x) l = mid+1;
  20. else r = mid;
  21. }
  22. return l;
  23. }
  24. void solve()
  25. {
  26. cin>>n;
  27. for(int i=0;i<n;i++) cin>>a[i];
  28. cin>>m;
  29. sort(a,a+n);
  30. for(int i=0;i<n;i++)
  31. {
  32. int t = m - a[i];
  33. int j = find(t);
  34. if(a[j] != a[i] && a[j] == t)
  35. {
  36. cout<<a[i]<<' '<<a[j];
  37. exit(0);
  38. }
  39. }
  40. cout<<"No"<<endl;
  41. }
  42. int main()
  43. {
  44. ios::sync_with_stdio(0);cin.tie(nullptr),cout.tie(nullptr);
  45. int _=1;
  46. // cin>>_;
  47. while(_--)
  48. {
  49. solve();
  50. }
  51. return 0;
  52. }
  53. /**
  54. * author: Nijika_jia
  55. * created: 2024.10.16 22:52:32
  56. */
点赞(0)
 

9.9 分

1 人评分

 

C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:

一点编程也不会写的:零基础C语言学练课程

解决困扰你多年的C语言疑难杂症特性的C语言进阶课程

从零到写出一个爬虫的Python编程课程

只会语法写不出代码?手把手带你写100个编程真题的编程百练课程

信息学奥赛或C++选手的 必学C++课程

蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程

手把手讲解近五年真题的蓝桥杯辅导课程

评论列表 共有 1 条评论

菜鸟 5月前 回复TA
大佬,膜拜膜拜你