fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main(){
  5. long long n;
  6. cin >> n;
  7. vector<long long> a, b, junc, kvec, junc2;
  8. for(long long i=0; i<n; i++){
  9. long long x;
  10. cin >> x;
  11. a.push_back(x); //inputs the array
  12. }
  13. junc2.push_back(a[0]); //Start Point
  14. junc2.push_back(a[n-1]); //End Point
  15. for(long long i=1; i<n-1; i++){
  16. if(a[i]>a[i-1] && a[i]>a[i+1]){
  17. junc.push_back(a[i]); // Calculating peaks and pushing them in a vector
  18. }
  19. else if(a[i]<a[i-1] && a[i]<a[i+1]){
  20. junc.push_back(a[i]); // Calculating crests and pushing them in the vector
  21. }
  22. else{
  23. junc2.push_back(a[i]); //All other Points in input. They have to be dealt in the same way as start and end points
  24. }
  25. }
  26. for(long long i=0; i<n-1; i++){ // Making the input continuous
  27. while(a[i]!=a[i+1]){
  28. b.push_back(a[i]);
  29. if(a[i]<a[i+1]){
  30. a[i]++;
  31. }
  32. else{
  33. a[i]--;
  34. }
  35. }
  36. }
  37. b.push_back(a[n-1]);
  38.  
  39. for(long long i=0; i<junc.size(); i++){
  40. long long k = 0;
  41. for(long long j=0; j<b.size(); j++){
  42. if(junc[i]==b[j]){ //Calculating the frequency of peaks and crests
  43. k++;
  44. }
  45. }
  46. kvec.push_back((k*2)-1); //Calculating the number of intersections each peak or crest will give
  47. }
  48. for(long long i=0; i<junc2.size(); i++){
  49. long long k = 0;
  50. for(long long j=0; j<b.size(); j++){
  51. if(junc2[i]==b[j]){ //Calculating frequence of start, end and other points in input
  52. k++;
  53. }
  54. }
  55. kvec.push_back(k); //Since they are in the middle of the peaks and crests, their frequency itself will be the maximum k
  56. }
  57. cout << *max_element(kvec.begin(), kvec.end()); //Calculating the maximum of all
  58.  
  59. }
  60.  
Runtime error #stdin #stdout #stderr 1.05s 528384KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc