Advertisement
RaFiN_

an example of two pointer including negetive numbers

Mar 21st, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1.  
  2. /*
  3.  
  4.  
  5. 1141F2 - Same Sum Blocks (Hard)
  6. Let's x the same sum of blocks in the answer. Obviously, x can be represented as a sum of some adjacent elements of a, i.e. x=a[l]+a[l+1]+⋯+a[r] for some l and r.
  7.  
  8. Iterate over all possible blocks in O(n2) and for each sum store all the blocks. You can use 'map<int, vector<pair<int,int»>' to store blocks grouped by a sum. You can do it with the following code:
  9.  
  10.  
  11. map<int, vector<pair<int,int>>> segs;
  12. for (int r = 0; r < n; r++) {
  13.     int sum = 0;                              
  14.     for (int l = r; l >= 0; l–) {
  15.         sum += a[l];
  16.         segs[sum].push_back({l, r});
  17.     }
  18. }
  19. Note, that blocks are sorted by the right end in each group.
  20.  
  21. After it you can independently try each group (there are O(n2) of them) and find the maximal disjoint set of blocks of a group. You can do it greedily, each time taking into the answer segment with the smallest right end. Since in each group they are ordered by the right end, you can find the required maximal disjoint block set with one pass. Let's assume pp is the current group of blocks (they are ordered by the right end), then the following code constructs the maximal disjoint set:
  22.  
  23.  
  24. int cur = 0;
  25. int r = -1;
  26. vector<pair<int,int>> now;
  27. for (auto seg: pp)
  28.     if (seg.first > r) {
  29.         cur++;
  30.         now.push_back(seg);
  31.         r = seg.second;
  32.     }
  33. Choose the maximum among maximal disjoint sets for the groups.
  34.  
  35.  
  36. */
  37.  
  38. map<int,vector<pii> >m;int n,arr[MAX];set<int>s;
  39.  
  40.  
  41. int main()
  42. {
  43.     ///booster()
  44.     ///read("input.txt");
  45.     scani(n);
  46.     for(int i = 0;i<n;i++){
  47.  
  48.         scani(arr[i]);
  49.     }
  50.  
  51.     for(int i = 0;i<n;i++){
  52.         int sum = 0;
  53.         for(int j = i;j>=0;j--){
  54.             sum+=arr[j];
  55.             m[sum].pb(pii(j,i));
  56.             s.insert(sum);
  57.  
  58.         }
  59.     }
  60.     int ans = 0,pnt;
  61.     for(auto it : s){
  62.         int cnt = 0,r = -1;
  63.         for(auto it1 : m[it]){
  64.             ///cout<<it<<" "<<it1.ff<<" "<<it1.ss<<endl;
  65.             if(it1.ff>r){
  66.                 cnt++;
  67.                 r = it1.ss;
  68.             }
  69.         }
  70.         if(cnt>ans){
  71.             ans = cnt;
  72.             pnt = it;
  73.         }
  74.     }
  75.  
  76.     OUT(ans);
  77.     int r = -1;
  78.     for(auto it : m[pnt]){
  79.  
  80.         if(it.ff>r){
  81.             printf("%d %d\n",it.ff+1,it.ss+1);
  82.             r = it.ss;
  83.         }
  84.     }
  85.  
  86.  
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement