Advertisement
ash_sinha

Untitled

Oct 6th, 2022
622
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. using namespace __gnu_pbds;
  4. using namespace std;
  5.  
  6. #define ff              first
  7. #define ss              second
  8. #define int             long long
  9. #define pb              push_back
  10. #define mp              make_pair
  11. #define pii             pair<int,int>
  12. #define vi              vector<int>
  13. #define mii             map<int,int>
  14. #define pqb             priority_queue<int>
  15. #define pqs             priority_queue<int,vi,greater<int> >
  16. #define setbits(x)      __builtin_popcountll(x)
  17. #define zrobits(x)      __builtin_ctzll(x)
  18. #define mod             1000000007
  19. #define inf             1e18
  20. #define ps(x,y)         fixed<<setprecision(y)<<x
  21. #define mk(arr,n,type)  type *arr=new type[n];
  22. #define w(x)            int x; cin>>x; while(x--)
  23. mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());
  24. #define fastio          ios_base::sync_with_stdio(0); cin.tie(0);
  25.  
  26. ////////  f is the solving function
  27.  
  28. int f(int pos,vector<int> &a)
  29. {
  30.     int ans1=-1e9,ans2=-1e9,n=a.size(),s=0,g=0;
  31.     // case 1 left pointer to i
  32.     for(int i=0;i<pos;i++){
  33.         if(a[i]>a[pos]) g++;
  34.         else if(a[i]<a[pos]) s++;
  35.     }
  36.     ans1=max(ans1,g-s);
  37.     for(int i=n-1;i>pos;i--)
  38.     {
  39.         if(a[i]>a[pos]) g++;
  40.         else if(a[i]<a[pos]) s++;
  41.         ans1=max(ans1,g-s);
  42.     }
  43.     s=0;g=0;
  44.     // case 2 right pointer to i
  45.     for(int i=n-1;i>pos;i--){
  46.         if(a[i]>a[pos]) g++;
  47.         else if(a[i]<a[pos]) s++;
  48.     }
  49.     ans2=max(ans2,g-s);
  50.     for(int i=0;i<pos;i++)
  51.     {
  52.         if(a[i]>a[pos]) g++;
  53.         else if(a[i]<a[pos]) s++;
  54.         ans2=max(ans2,g-s);
  55.     }
  56.     return max(ans1,ans2);
  57. }
  58.  
  59. ///// solve is the driving function
  60.  
  61. vector<int> solve(vector<int> &a)
  62. {
  63.     int n=a.size();
  64.     vector<int> ans(n);
  65.     for(int i=0;i<n;i++) ans[i]=f(i,a);
  66.     return ans;
  67. }
  68.  
  69. int32_t main()
  70. {
  71.     vi a = {1,2,5,4,3};
  72.     vi ans=solve(a);
  73.     for(auto x:ans) cout<<x<<" ";
  74.    
  75. return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement