Se si riformula il problema in modo leggermente diverso (ma equivalente), un algoritmo diventa più ovvio:
Sono coinvolte parti: persone e un ristorante. Diciamo essere la quantità di denaro partito dovrei avere dopo il pasto è finito e pagato. Ad esempio, se Alice ha $ 36 e deve $ 25, Bob ha $ 12 e deve $ 11 e Carl ha $ 30 e deve $ 25, diciamo che è il ristorante e ha:nn−1piip0
p=(61,11,1,5)
That is, when the meal is over the restaurant should have $61, Alice should have $11, Bob should have $1 and Carl should have $5.
Now let b enumerate all of the m bills involved. For example:
b=(1,5,10,20,1,1,5,5,10,20)
The denominations of the bills do not matter, but I have chosen denominations of paper US currency for this example because they are familiar.
We are seeking to minimize the number of bills that change hands, so we associate a "cost" with person i leaving with bill j by using a {0,1} matrix C. Entries of 0 in this matrix indicate which bills each party starts with (C0,j=0 for all j because the restaurant starts with no bills).
Continuing our example:
C=⎡⎣⎢⎢⎢0011001100110011010101010101010101100110⎤⎦⎥⎥⎥
indicates that Alice started with $1, $5, $10, $20, Bob started with $1, $1, $5, $5, and Carl started with $10 and $20.
Again, the goal is to minimize the number of bills that change hands. In other words:
Minimize:subject to:and∑i=0n−1∑j=0m−1Ci,jxi,j∑i=0n−1xi,j=1 for 0≤j<m,∑j=0m−1xi,jbj=pi for 0≤i<n,xi,j≥0
The first constraint says that the solution can only assign a particular bill to one party, and the second ensures that everyone pays the appropriate amount.
This is the 0,1 INTEGER PROGRAMMING problem and is NP-complete (see [Karp 1972]). The Wikipedia page about linear programming has information on different algorithms that can be used for these types of problems.
There are potentially multiple optimal solutions; by hand the first solution to the example I came up with was:
x=⎡⎣⎢⎢⎢0100100001001000101000000001100010001000⎤⎦⎥⎥⎥
which means Alice pays exactly $5 and $20, Bob pays exactly $1, $5 and $5, and Carl overpays $10 and $20 and then removes a $5 from the table.
I also used the Mixed Integer Linear Program module of the Sage Math system which has the ability to use different solver backends (GLPK, COIN, CPLEX, or Gurobi). The first solution it gave was
x=⎡⎣⎢⎢⎢0100100001001000001010000000100110001000⎤⎦⎥⎥⎥
which is almost the same except that Carl took the "other" $5 that Bob put on the table.
Formulating the problem in this way satisfies all the properties you list (you can extrapolate which bills end up where from C and the solution matrix x). The exception is perhaps #4 which was discussed in the comments of the question. It is not clear to me what you would like to do in the situation that there is no feasible solution to the set of linear equations:
Identify a subset of people that can pay the reduced total? Or perhaps a subset of people which could still pay the entire bill, i.e. they pay for their friend.
Your final statement makes it seem like you are interested in the case that the denominations of the bills are fixed, this doesn't change the problem however.
In any case, there is also an O(1) solution in which every person pays with a credit card.