Write in front
This problem is a very easy to think of the greedy topic, but the correctness of the proof is very complex. However, at present, all the questions on the Internet do not give any correctness proof of the greedy algorithm, all only stay in the description of a greedy algorithm. Based on the love of Algorithms and Computer Science (escape), I spent 2 weeks in-depth study of the problem, and consulted Apass.jack Daniel, finally in his help to prove the greed of the correctness. The next step is to demonstrate the process in detail.
PS:Apass.Jack provided the entire proof framework (though I later found a mistake and revised the proof), thank you for that!
Title Description
Given $n$ ($n \le 10^5) The price of an item in a city, each city can only buy or sell one item at most, neither buy nor sell. Of course, if there is no item on hand, it cannot be sold. Ask from City 1 to City $n$, on the way to make the most money after the sale, under the premise of the minimum number of transactions. The initial hand does not have any items, but there is unlimited money.
Sample Input
3 4 1 2 Ten 9 5 9 5 9 Ten 5 2 2 1
Sample Output
- 4 5 2 0 0
Algorithm description
We do not consider minimizing the number of transactions, only the maximization of profits. There is a better thought of the greedy algorithm:
Traverse the city from left to right and maintain the current state of each city (buy items, sell items, or do nothing). For the city $i$, we look for the lowest price in the 1 to $i-1$ $j$ and $j$ city is not bought (can sell), if $j$ city price is lower than $i$ city, then buy in $j$ City and sell in $i$ city. Update the trading status of the $j$ city at the same time: if the previous status is sell, update to do nothing; otherwise update to buy.
This way, you can get the answer by traversing 1 to $n$. When implemented, it is clear that priority queue maintenance is available. Such time complexity $o (n\log N) $.
Basic terminology of algorithm correctness proof
For ease of presentation, the following marks are introduced:
(1) The price of City $i$ is expressed by $p_i$;
(2) A policy is defined as a collection of buying and selling states for each city, expressed in $s_i$. Among them, $s _i=0$ mean to do nothing, $s _i=1$ means buy; $s _i=-1$ means sell.
(3) define $s$ prefix and $h_s (i) =\sum_{i=1}^n {s_i}$, and supplement $h_s (0) =0$; Obviously a scheme is legal when and only if to any of the \le I \le n$, $h _s (i) \ge 0$.
(4) define $ (i,j) $ to represent an ascending two-tuple $ (i < j) $;
(5) said $ (i,j) $ is separated by the policy $s$, or called in the Strategy $s$ $ (I,J) $ cannot belong to the same sale if there $i \le K < j$, making $h_s (k) =0$.
Proof ideas
This problem directly proves to be very difficult because it dynamically modifies the previous policy. We consider the introduction of an intermediate step, that is to get the optimal solution to satisfy the charged nature, and then prove that the greedy solution also satisfies this property, then greed is naturally correct.
Next, we will prove the following main theorems, respectively, to establish the properties of the optimal solution and the nature of the greedy solution.
In addition, to simplify the proof, the following sections assume that all $p_i$ differ. Obviously if the $p_i$ and the algorithm is correct, then the $p_i$ can be the same as the algorithm is also necessarily correct (by taking the limit).
Theorem 1
If a policy $s$ is the optimal policy, then $h_s (n) =0$, and for any $ (I,J) $ must be satisfied:
(1) If $s_i=s_j=0$ or $s_i<s_j$, there must be $p_i>p_j$.
(2) If $s_i=s_j=0$ or $s_i>s_j$, and $p_i>p_j$,$ (I,J) $ must be separated.
Proof: $h _s (n) =0$ obviously.
(1) If not, we will $s_i$ plus 1, will $s_j$ minus 1, obviously to any $i$ have $h_s (i) \ge 0$, it is a legitimate scheme, but the profit is greater, contradiction.
(2) If not, then to any $i \le k<j$ have $h_s (k) \ge1$. We have reduced the $s_i$ by 1, $s _j$ plus 1, obviously to arbitrary $i$, $h _s (i) \ge 0$ is still set up, so it is a legitimate scheme, but the profit is greater, contradictory.
Theorem 2
The strategy of satisfying Theorem 1 must be given by greedy algorithm.
Proof: Using mathematical inductive method, $n =1$ is obviously established.
A strategy for satisfying theorem 1 when considering $n$, because $h_s (n) =0$ must have $s_n=0$ or $s_n=-1$.
(1) If $s_n=0$, then this strategy satisfies the theorem 1 strategy in the former $n-1$ city, has been given by the greedy algorithm. When the greedy algorithm to the first $n$ city, it must do nothing, if not, there will be $p_j<p_n$ and $s_j \le 0$. Then the two-tuple $ (j,n) $ and theorem 1 (1) contradict.
(2) if $s_n=-1$. Set $k$ is the largest =0$ in \le k<n$ that satisfies $h_s (k) $k$, $j $ for $k<j\le n$ $s_j and the highest price in the city.
Consider $s_j$ minus 1 and others unchanged, so that we get a new strategy, which is recorded as $w$, then the new strategy $h_w (n-1) =0$ and $h_w$ constant, $w_1$ to $w_{n-1}$ is a legitimate solution for the former $n-1$ city. The proof that it satisfies the condition of theorem 1.
Note A property: if $ (i,j) $ is separated in $s$, it must be separated in $w$, because $w$ only $j$ 1 smaller than $s$, the other values are the same. Using this property, for the strategy $w$, any two-tuple which does not contain the $j$ must satisfy the condition of theorem 1; we just need to consider whether the two-tuple containing the $j$ satisfies the theorem 1 condition.
For theorem 1 (1):
A): if $ (i,j) $ satisfies $w_i=w_j=0$ or $w_i<w_j$, there must be $s_i<s_j$, because $s$ satisfies theorem 1 (1) $p_i>p_j$;
b): if $ (j,i) $ satisfies $w_j=w_i=0$ or $w_j<w_i$, then $s_i=w_i \ge 0$, because $j$ is $k$ after $s$ non-negative price the highest, therefore $p_j>p_i$.
For theorem 1 (2):
A): if $ (i,j) $ to meet $w_i>w_j$ or $w_i=w_j=0$, and $p_i>p_j$, then $s_i=w_i \ge 0$, because $j$ is $k$ after $s$ non-negative price of the highest, so there will be $i \le k$, so $ (i,j) $ be separated by $s$;
b): if $ (j,i) $ satisfies $w_j>w_i$ or $w_i=w_j=0$, and $p_j>p_i$, then $s_j>s_i$, by theorem 1 (2) $ (j,i) $ in $s$ strategy is separated, this is the largest $k$ with $h_s (k) =0 $ in urban contradiction, there will be no such situation.
In conclusion, $W $ is the strategy of satisfying Theorem 1, which is bound to be the result of the $n-1$ of the greedy algorithm. Finally consider the operation of the algorithm in the $n$ city.
First of all there must be $p_j<p_n$, if not by $s_j \ge 0 >-1=s_n$ but $ (j,n) $ is not in $s$ is divided into the contradiction of Theorem 1 (2). and $w_j \le 0$, so the algorithm will be sold in the $n$ city (because you can buy in $j$ and can increase profits), the following only consider not necessarily buy $j$ city. Set the algorithm to buy the city for $t$, and $p_j>p_t$, then $s_t \le 0$ and $s_j \ge 0$, then $t$ can not before $j$ (otherwise inconsistent with Theorem 1 (1)). Likewise $t$ cannot after $j$ (otherwise by the theorem 1 (2) $ (j,t) $ by $s$ separate, with $k$ is the largest city contradiction of $h_s (k) =0$), so that the city $t=j$ to buy. Therefore, the strategy of satisfying Theorem 1 is definitely given by greedy algorithm. The certificate is completed.
Theorem 3
The strategy of satisfying Theorem 1 is unique, thus the greedy algorithm is correct.
Proof: Because the strategy of Theorem 1 must be given by greedy algorithm, and greedy algorithm gives only one strategy, so the strategy of satisfying Theorem 1 is unique. By theorem 1, the optimal strategy satisfies the theorem 1, so the algorithm is correct.
Minimize trade times
According to the above theorem, when the price is not the same as the optimal strategy is unique, but the price can be the same when it is not necessarily, at this time there is the problem of minimizing the number of transactions. Consider modifying the algorithm at this time, when the lowest price in the priority queue is more than one, take precedence over the city where the item was originally sold, so that the state of the city is modified to do nothing. This correctness is obvious. For goods of the same price, they are completely equivalent to the subsequent cities, so as far as possible they will be not bought or sold inevitably to achieve the minimum number of transactions.
In conclusion, this problem is solved completely.
Reference Code
1#include <cstdio>2#include <queue>3 using namespacestd;4 intp[100001];5 structnode{6 inti;7 BOOLsell;8 BOOL operator< (ConstNode t)Const{9 returnP[i]! = p[t.i]? P[i] > P[T.I]:!sell &&T.sell;Ten } One }; A intMain () - { - inttest, N; thescanf"%d", &test); - while(test--){ -Priority_queue<node>Q; -scanf"%d", &n); + for(inti =0; I < n; i++) -scanf"%d", &p[i]); + Long LongAns =0; A intCNT =0; atQ.push ({0,0 }); - for(inti =1; I < n; i++){ - intj =q.top (). I; - if(P[j] <P[i]) { - BOOLSell =Q.top (). Sell; - Q.pop (); inAns + = p[i]-P[j]; - if(Sell) Q.push (node{J,0 }); to ElseCNT + =2; +Q.push (node{I,1 }); - } the ElseQ.push (node{I,0 }); * } $printf"%lld%d\n", ans, cnt);Panax Notoginseng } -}
HDU6438 Buy and Resell Problem solving report (a rigorous proof of an interesting greedy question)