fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define int long long
  4. #define f first
  5. #define s second
  6. #define file(a) freopen(a".inp","r",stdin);freopen(a".out","w",stdout);
  7. #define fastoi ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
  8. #define TIME cerr << "\n\n" << (double)clock() / CLOCKS_PER_SEC * 1000 << " ms";
  9. #define ii pair<ll,ll>
  10. using namespace std;
  11. const int maxn = 1e6+5;
  12. const int MOD = 1e9+7;
  13. ll n,k,a[maxn],pre[maxn],ans=0,cnt=0,b[maxn];
  14. int32_t main()
  15. {
  16. fastoi;
  17. #ifndef ONLINE_JUDGE
  18. file("test");
  19. #endif
  20. ll x;
  21. cin >> n >> x;
  22. for (int i=1;i<=n;i++) cin >> a[i];
  23.  
  24. ll l=1,r=n;
  25.  
  26. while (l<r)
  27. {
  28. ll sum = a[l] + a[r];
  29. if (sum == x)
  30. {
  31. ans++;
  32. l++;
  33. r--;
  34. }
  35. else if (sum < x) // sum chưa đạt được x
  36. {
  37. l++;
  38. }
  39. else r--;
  40. }
  41. cout << ans;
  42. TIME;
  43. return 0;
  44. }
  45. //─────────────────────────────────────────────────────────
  46. //─██████──██████─██████████─██████████████─██████──██████─
  47. //─██░░██──██░░██─██░░░░░░██─██░░░░░░░░░░██─██░░██──██░░██─
  48. //─██░░██──██░░██─████░░████─██░░██████████─██░░██──██░░██─
  49. //─██░░██──██░░██───██░░██───██░░██─────────██░░██──██░░██─
  50. //─██░░██████░░██───██░░██───██░░██████████─██░░██──██░░██─
  51. //─██░░░░░░░░░░██───██░░██───██░░░░░░░░░░██─██░░██──██░░██─
  52. //─██░░██████░░██───██░░██───██░░██████████─██░░██──██░░██─
  53. //─██░░██──██░░██───██░░██───██░░██─────────██░░██──██░░██─
  54. //─██░░██──██░░██─████░░████─██░░██████████─██░░██████░░██─
  55. //─██░░██──██░░██─██░░░░░░██─██░░░░░░░░░░██─██░░░░░░░░░░██─
  56. //─██████──██████─██████████─██████████████─██████████████─
  57. //─────────────────────────────────────────────────────────
Success #stdin #stdout #stderr 0.01s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr

7.122 ms