<p>Winter is coming! During the contest, your first job is to design a standard heater with a fixed warm radius to warm all the houses.</p>

<p>Every house can be warmed, as long as the house is within the heater&#39;s warm radius range.&nbsp;</p>

<p>Given the positions of <code>houses</code> and <code>heaters</code> on a horizontal line, return <em>the minimum radius standard of heaters&nbsp;so that those heaters could cover all houses.</em></p>

<p><strong>Notice</strong> that&nbsp;all the <code>heaters</code> follow your radius standard, and the warm radius will the same.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> houses = [1,2,3], heaters = [2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The only heater was placed in the position 2, and if we use the radius 1 standard, then all the houses can be warmed.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> houses = [1,2,3,4], heaters = [1,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The two heaters were placed at positions 1 and 4. We need to use a radius 1 standard, then all the houses can be warmed.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> houses = [1,5], heaters = [2]
<strong>Output:</strong> 3
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= houses.length, heaters.length &lt;= 3 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= houses[i], heaters[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution {
public:
    int findRadius(vector<int>& houses, vector<int>& heaters) {
        
        sort(houses.begin(),houses.end());

        sort(heaters.begin(),heaters.end());

        int l = 0;

        int r = houses.size();

        int ext = houses[r-1];

        r = 1000000000;

        int ans = 1000000000;

        while(r >= l)
        {
            int mid = (r + l)/2;

            int l1,r1;

            cout<<mid<<endl;
            vector<pair<int,int>>v;

            vector<int> v1;

            for(int i = 0; i < heaters.size();i++)
            {
                if(i == 0)
                {
                    l1 = max(heaters[i] - mid, 0);

                    r1 = min(heaters[i] + mid, ext);

                }
                else
                {
                    int l2 = max(heaters[i] - mid, 0);

                    int r2 = min(heaters[i] + mid, ext);

                    if(l2 > r1)
                    {
                        v.push_back({l1,r1});
                        
                        v1.push_back(l1);

                        l1 = l2;

                        r1 = r2;
                    }
                    else
                    {
                        l1 = min(l2, l1);

                        r1 = max(r1, r2);
                    }
                }
            }

            v.push_back({l1,r1});

            v1.push_back(l1);


            for(int i = 0; i < v.size();i++)
            {
                cout<<v[i].first<<" "<<v[i].second<<endl;
            }

            

            int cnt = 0;

            for(int i = 0; i < houses.size(); i++)
            {
                auto itr = lower_bound(v1.begin(),v1.end(),houses[i]);

                if(itr != v1.end())
                {
                    int ind = itr - v1.begin();

                    if(v1[ind] == houses[i])
                    {

                    }
                    else
                    {
                        if(ind - 1 >= 0)
                        {
                            ind--;
                        }
                        else
                        {
                            continue;
                        }
                    }
                    if(v[ind].first <= houses[i] and houses[i] <= v[ind].second)
                    {
                        cnt++;
                    }
                }
                else
                {
                    int siz1 = v.size();

                    if(v[siz1-1].first <= houses[i] and houses[i] <= v[siz1-1].second)
                    {
                        cnt++;
                    }

                }
            }

            cout<<"cnt"<<" "<<cnt<<endl;


            if(cnt == houses.size())
            {
                ans = min(ans, mid);

                r = mid - 1;
            }
            else
            {
                l = mid + 1;
            }

        }

        return ans;
    }
};
31600