4

One Common Type of Two Pointer Approach

 1 year ago
source link: https://codeforces.com/blog/entry/110408
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
    sort(arr.begin(), arr.end());
    int ans= 0;
    int i=0; int j=0; int sum=0;
    while(j<n){
        while( arr[j].first-arr[i].first>=k){
            sum-=arr[i].second; i++;
        }
        while(j<n && (arr[j].first-arr[i].first)<k){
            sum+=arr[j].second; j++;
            // cout<<sum<<" ";
        }cout<<endl;
        
        ans= max(ans, sum);
        // cout<<sum<<" "<<ans<<endl;
    }
   cout<<ans<<endl;

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK