Advertisement
yuhung94

Problem 1. Equal Sum Subarrays

Mar 6th, 2023
774
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #pragma GCC optimzize("Ofast,no-stack-protector")
  2. #include<bits/stdc++.h>
  3. #define int long long
  4. #define quick ios::sync_with_stdio(0);cin.tie(0);
  5. #define rep(x,a,b) for(int x=a;x<=b;x++)
  6. #define repd(x,a,b) for(int x=a;x>=b;x--)
  7. #define lowbit(x) (x&-x)
  8. #define sz(x) (int)(x.size())
  9. #define F first
  10. #define S second
  11. #define all(x) x.begin(),x.end()
  12. #define mp make_pair
  13. #define eb emplace_back
  14. using namespace std;
  15. typedef complex<int> P;
  16. #define X real()
  17. #define Y imag()
  18. typedef pair<int,int> pii;
  19. void debug(){
  20.     cout<<"\n";
  21. }
  22. template <class T,class ... U >
  23. void debug(T a, U ... b){
  24.     cout<<a<<" ",debug(b...);
  25. }
  26. const int N=2e5+7;
  27. const int INF=1e18;
  28. int a[N];
  29. int pa[N];
  30. int ans[N];
  31. bool contain(int l,int r,int k){
  32.     return l<=k&&k<=r;
  33. }
  34. signed main(){
  35.     quick
  36.     int n;
  37.     cin>>n;
  38.     rep(i,1,n){
  39.         cin>>a[i];
  40.         pa[i]=pa[i-1]+a[i];
  41.         ans[i]=INF;
  42.     }
  43.     vector<tuple<int,int,int> > v;
  44.     set<int> Lft;
  45.     set<int> Rgt;
  46.     rep(l,1,n){
  47.         rep(r,l,n){
  48.             v.eb(make_tuple(pa[r]-pa[l-1],l,r));
  49.         }
  50.     }
  51.     sort(all(v));
  52.     rep(k,1,n){
  53.         rep(j,0,sz(v)-2){
  54.             auto [a,b,c]=v[j];
  55.             auto [a2,b2,c2]=v[j+1];
  56.             if(contain(b,c,k)!=contain(b2,c2,k)){
  57.                 ans[k]=min(ans[k],a2-a);
  58.             }
  59.         }
  60.     }
  61.     rep(i,1,n) cout<<ans[i]<<"\n"; 
  62.  
  63.     return 0;
  64. }
  65.  
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement