fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n;
  6. cin >> n;
  7.  
  8. vector<long long> a(n);
  9.  
  10. for (int i = 0; i < n; i++) {
  11. cin >> a[i];
  12. }
  13. long long k;
  14. cin>>k;
  15.  
  16.  
  17. unordered_map<int, int>mp1;
  18. unordered_map<int, int>mp2;
  19.  
  20. mp1[0]=-1;
  21. mp2[0]=-1;
  22.  
  23. int psum=0;
  24. int maxlen=0;
  25. int minlen=INT_MAX;
  26. int count =0;
  27. int count1=0;
  28.  
  29. for(int j=0;j<n;j++){
  30. psum = psum+a[j];
  31. int x = psum-k;
  32. if (mp1.find(x) != mp1.end()) {
  33. int i = mp1[x] + 1;
  34. int length = j - i + 1;
  35.  
  36. if(length>maxlen){
  37. maxlen= length;
  38. count=1;
  39. }
  40. else if(maxlen==length)
  41. count++;
  42. }
  43.  
  44. if (mp2.find(x) != mp2.end()) {
  45. int i = mp2[x] + 1;
  46. int length = j - i + 1;
  47.  
  48. if(length<minlen){
  49. minlen= length;
  50. count1=1;
  51. }
  52. else if(minlen==length)count1++;
  53. }
  54.  
  55.  
  56. if(mp1.find(psum)==mp1.end())
  57. mp1[psum]=j;
  58. mp2[psum]=j;
  59.  
  60.  
  61.  
  62. }
  63. cout<<count<<endl<<count1;
  64.  
  65. return 0;
  66. }
Success #stdin #stdout 0s 5320KB
stdin
15
1 2 3 2 4 1 1 3 2 2 1 4 1 2 3
5
stdout
2
7