Topic:
P Pirates stole D diamonds and then went to the high seas to loot, agreeing to the following strategy:
First, p Pirates decide the serial number of the 1-p by drawing lots. Then a distribution programme (the programme should give a specific amount of each pirate) was proposed by pirate number 1th, and if an absolute majority ( i.e. greater than half ), including number 1th, were to be agreed upon, then the allocation would be followed, otherwise number 1th would be fed into the sea for the Sharks, followed by the 2nd, Number 3rd The Pirates proposed the scheme until a scheme with an absolute majority of consent was available, or only the last pirate, which exclusively owned all the Diamonds. Please write a procedure that gives the number of diamonds you have in the Diamond distribution scheme for pirate number 1th.
The accompanying three assumptions:
1) "smart" and "greedy" assumption: each pirate can always maximize their own interests as a code of conduct;
2) "Humanization" assumes that, in the case of having access to as many diamonds as possible, pirates do not intentionally kill their associates;
3) "Unbiased" assumption: there is no personal vendetta between pirates, the order of the other pirate diamonds to the principle of small sequence priority.
Input Format Description:
Enter 2 positive integers D and P (3<=p<=d<=100).
Output Format Description:
Output the number of diamonds in the diamond distribution scheme for pirate 1th.
Sample input and output:
Serial number |
Input |
Output |
1 |
10 7 |
6 |
2 |
3 3 |
2 |
3 |
100 3 |
99 |
4 |
100 100 |
49 |
Ideas:
Suppose an ordinary situation, 10 diamonds 7 people points.
If there are only 2 people left, then whatever 2 says 1 will oppose it unless he gives him all the diamonds. This is the situation below.
(0,10)
If there are only 3 people left, 3 know that if they die 2 of the scheme, if they want to make their own proposal to achieve as long as the 1 people agree. So 3 will give the number 2nd a Diamond 2 will agree with the 3 proposal. This becomes the following:
(9,1,0)
If there are only 4 people left, 4 know that if they die 3 of the scheme, if they want to make their own proposal to achieve as long as the 2 people agree. So 4 would give a diamond to number 2nd, a diamond for 1th, 1 and 2 would agree to the proposal of 4. This becomes the following:
(7,0,2,1)
If there are only 5 people left, 5 know that if they die 4 of the scheme, if they want to make their own proposal to achieve as long as the 2 people agree. So 5 will give a diamond to No. 3rd and give 1th 2 diamonds. This becomes the following:
(7,0,1,0,2)
If there are only 6 people left, 6 know that if they die 5 of the scheme, if they want to make their own proposal to achieve as long as the 3 people agree. So 6 will give 4, 2nd, a diamond, 3rd, 2 diamonds. This becomes the following:
(6,0,1,2,1,0)
Now we can roll out 7 people, 7 know that if you die 6 of the plan, if you want to make their own proposal to achieve as long as the 3 people to agree on the good. So 7 will give 4, 2nd, a diamond, 3rd, 2 diamonds. This becomes the following:
(6,0,1,2,0,0,1) or (6, 0, 1, 0, 0, 2, 1)
The whole process is as follows:
(0,10)
(9, 1, 0)
(7, 0, 2, 1)
(7, 0, 1, 0, 2)
(6,0, 1, 2, 1, 0)
(6, 0, 1, 2, 0, 0, 1)
You will find that at the time of the penultimate assignment, the n-1 person will be opposed (P=3 exception), so the last nth person wants to get more than half of the votes, it is necessary to put hope in the penultimate 1~ (n-2) this n-2 person. So just give them (n-2)/2+1 The number of diamonds a person has to give them more than n-1, and they will support you. So the algorithm can be n-2 individuals in the following order, and pick the minimum number of diamonds (n-2)/2+1 individuals, give them a diamond, and for other people, directly not to the diamond, so OK.
The above reasoning is of course based on certain assumptions, the most important premise is that pirates are smart enough to consider extreme situations, only rational, so they will start from the back to consider.
In fact, as long as the careful observation of the above series we will summarize the law:sum=d-p/2-1, except P=3.
Because of the p>=3, so the initial situation p=3 allocation scheme must be: (d-1,1,0)
Code:
#include <iostream> #include <deque> #include <vector> #include <algorithm>using namespace std; vector<int> vec;int Spoil (int d,int p,vector<int> &alloc) {int sum; Alloc.push_back (0); Alloc.push_back (1); Alloc.push_back (D-1); for (int i=3;i<p;i++) {sort (Alloc.begin (), Alloc.end ()); sum=0; for (int j=0;j<alloc.size (); j + +) {if (j< (Alloc.size ()-1)/2+1) {alloc[j]++; SUM+=ALLOC[J]; } else alloc[j]=0; } alloc.push_back (D-sum); } return alloc[p-1];} int main () {int D, p;int num;while (cin>>d>>p) {vector<int> alloc; Num=spoil (D,p,alloc); cout<< "First Pirate Get:" <<num<< "Diamonds" <<endl; For (Vector<int>::iterator It=alloc.begin (); It!=alloc.end (); it++) cout<<*it<< ""; Cout<<endl;} return 0;}
Reference article:
Http://blog.csdn.net/linsheng9731/article/details/22613483?utm_source=tuicool
http://m.blog.csdn.net/blog/a418382926/21476755
(algorithm) Pirate loot _1