Advertisement
Abdelrahman_Helmy

Untitled

Mar 26th, 2023
849
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. void Solve() {// << The Art of تخاذل >>
  2.     ll n;cin>>n;
  3.     vector<ll>v(n),copy(n+1); cin>>v;
  4.     for(int i=0;i<n;i++){
  5.         ll k=v[i];
  6.         if(v[i]>0){
  7.             copy[i]+=1;
  8.             copy[i+k]-=1;
  9.         }
  10.     }
  11.     partial_sum(all(copy),copy.begin());
  12.     //**************
  13.     int start = 0, end = 0, maxLength = 1;
  14.     for (int i = 0; i < n; i++) {
  15.         for (int j = i + 1; j < n; j++) {
  16.             bool allEqual = true;
  17.             for (int k = i; k < j; k++) {
  18.                 if (copy[k] != copy[k+1]) {
  19.                     allEqual = false;
  20.                     break;
  21.                 }
  22.             }
  23.             if (allEqual && j - i + 1 > maxLength) {
  24.                 start = i;
  25.                 end = j;
  26.                 maxLength = j - i + 1;
  27.             }
  28.         }
  29.     }
  30.     if(copy[start]==0) cout<<-1<<endl;
  31.    /* cout << "Longest equal subarray: ";
  32.     for (int i = start; i <= end; i++) {
  33.         cout << copy[i] << " ";
  34.     }
  35.     cout << endl;*/
  36.    else cout<<end-start+1<<" "<<start+1<<endl;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement