公式

I - CUBRID HA Load Balance 解説 by lycoris1600


Let \(x\) be the number of initially chosen Master servers that are still alive, and let \(y\) be the number of initially chosen Slave servers that are still alive. If \(x>0\), a request query 2 a b c can be processed exactly when \(x \ge a\), \(y \ge c\), and \(x+y \ge a+b+c\). If \(x=0\) and \(y>0\), one Slave immediately becomes a Master, so the actual numbers become \(1\) Master and \(y-1\) Slaves. If \(x=y=0\), only a query with no requests can be processed.

Process the queries in reverse order. Only the first shutdown query for each server in the original order matters; repeated shutdowns of an already turned-off server can be ignored. In reverse order, such a shutdown query 1 i becomes the moment when server \(i\) becomes available to choose. Maintain \(k\), the number of currently chosen available servers, and an interval \([L,R]\) of possible counts of initial Master servers among them. Adding one more server changes \(k\) to \(k+1\) and expands the interval to \([L,R+1]\), because the new server may be chosen as either a Slave or a Master.

For each request query, compute the interval of Master counts that can process it with the current value of \(k\). Let \(T=a+b+c\). If \(T=0\), every Master count in \([0,k]\) is possible. If \(T>0\) and \(k<T\), no Master count is possible. Otherwise, Master count \(0\) is possible exactly when \(a \le 1\) and \(k \ge c+1\), and positive Master counts form the interval \([\max(1,a), k-c]\). The union is still one interval. If the intersection with the maintained interval is empty, keep adding available servers until the intersection becomes nonempty. If no more server can be added, the answer is \(-1\). Otherwise, replace the maintained interval by the intersection and continue. This greedy choice is optimal because adding servers never makes any already processed reverse-time query harder to satisfy, and if the current query cannot be processed, every valid solution must choose more servers by this time. The total time complexity is \(O(N+Q)\).

投稿日時:
最終更新: