Avoid flooding (min line tree)

There are countless lakes in your country, all of which are empty at first. When the nth Lake rains, if the nth lake is ...

There are countless lakes in your country, all of which are empty at first. When the nth Lake rains, if the nth lake is empty, it will be full of water, otherwise the lake will flood. Your goal is to avoid flooding any lake.

Give you an array of integers, rains, where:

  • Rain [i] > 0 means that on the I day, rain will fall on the I lake.
  • rains[i] == 0 means no lake will rain on the I day. You can choose a lake and drain the water of the lake.

Please return an array ans to meet the following requirements:

  • ans.length == rains.length
  • If rains [i] > 0, ans[i] == -1.
  • If rains[i] == 0, ans[i] is the lake you choose to dry on day I.

If there are multiple possible solutions, return any of them. If you can't stop the flood, return an empty array.

Please note that if you choose to drain a lake full of water, it will become an empty lake. But if you choose to drain an empty lake, nothing will happen (see example 4 for details).

Example 1:

Input: rains = [1,2,3,4] Output: [- 1, - 1, - 1, - 1] Explanation: after the first day, lakes full of water include [1] After the second day, lakes filled with water included [1,2] After the third day, lakes filled with water included [1,2,3] After the fourth day, lakes filled with water included [1,2,3,4] No day will you be able to drain any lake, and no lake will flood.

Example 2:

Input: rains = [1,2,0,0,2,1] Output: [- 1, - 1,2,1, - 1, - 1] Explanation: after the first day, lakes full of water include [1] After the second day, lakes filled with water included [1,2] After the third day, we drained lake 2. So the remaining lakes full of water include [1] After the fourth day, we drained Lake 1. So there is no lake full of water. After the fifth day, lakes full of water include [2]. After the sixth day, lakes filled with water included [1,2]. It can be seen that there will be no flood under this scheme. At the same time, [- 1, - 1,1,2, - 1, - 1] is another feasible scheme without flood.

Example 3:

Input: rains = [1,2,0,1,2] Output: [] Explanation: after the next day, lakes filled with water included [1,2]. We can dry a lake on the third day. But after the third day, lakes 1 and 2 will rain again, so no matter which Lake we drain on the third day, another lake will flood.

Example 4:

Input: rains = [69,0,0,0,69] Output: [- 1,69,1,1, - 1] Explanation: any form such as [- 1,69,x,y,-1], [-1,x,69,y,-1] or [- 1,x,y,69,-1] is a feasible solution, where 1 < = x, y < = 10 ^ 9

Example 5:

Input: rains = [10,20,20] Output: [] Explanation: since Lake 20 will rain for two consecutive days, there is no way to stop the flood.

Tips:

  • 1 <= rains.length <= 10^5
  • 0 <= rains[i] <= 10^9
int n,minn[400000]; vector<int>num; void build(int key, int low, int high) { if (low == high) { minn[key] = num[low]; return; } int mid = (low + high) / 2; build(key * 2, low, mid); build(key * 2 + 1, mid + 1, high); minn[key] = (minn[key * 2] < minn[key * 2 + 1]) ? minn[key * 2] : minn[key * 2 + 1]; } void update(int key, int low, int high, int uplace) { if (low == high) { minn[key] = num[low]; return; } int mid = (low + high) / 2; if (uplace <= mid)update(key * 2, low, mid, uplace); else update(key * 2 + 1, mid + 1, high, uplace); minn[key] = (minn[key * 2] < minn[key * 2 + 1]) ? minn[key * 2] : minn[key * 2 + 1]; } int query(int key, int low, int high, int x, int y) { if (low == x && high == y)return minn[key]; int mid = (low + high) / 2; if (mid < x)return query(key * 2 + 1, mid + 1, high, x, y); if (mid >= y)return query(key * 2, low, mid, x, y); int a = query(key * 2, low, mid, x, mid); int b = query(key * 2 + 1, mid + 1, high, mid + 1, y); return (a<b) ? a : b; } class Solution { public: vector<int> avoidFlood(vector<int>& rains) { num=rains; num.insert(num.begin(),0); n=rains.size(); for(int i=0;i<rains.size();i++)num[i+1]=(rains[i]==0)?i:1234567; build(1, 1, n); unordered_map<int,int>m; vector<int>ans,ansempty; ans.resize(n,1); for(int i=0;i<rains.size();i++) { if(rains[i]==0)continue; if(m[rains[i]]==0) { m[rains[i]]=i+1; continue; } int k=m[rains[i]]-1; int km=query(1,1,n,k+1,i+1); if(km==1234567)return ansempty; num[km+1]=1234567; update(1,1,n,km+1); m[rains[i]]=i+1; ans[km]=rains[i]; } for(int i=0;i<rains.size();i++)if(rains[i])ans[i]=-1; return ans; } };

21 June 2020, 06:09 | Views: 4322

Add new comment

For adding a comment, please log in
or create account

0 comments