P2320 Guiguzi purse (partition)

------------ ------------ restore content begins

Description: https://www.luogu.com.cn/problem/P2320

m coins, put into some of the pockets. Purse money greater than 1 different from each other.

Q is the minimum number purse, make any number less than m gold represented.


For example, a number is 20, so how do we represent a number from 1-20 to all of it?

Suppose we coins represents a number of 1 to 10, is not as long as the number of gold is again a bag 10, the coins can be expressed by 11 to 20

Then we can represent 1 to 5, come back a bag of gold coins to 5 on the line.

And so on.

So why go to the middle to separate it?

Let me try, if won 1-9, to a number of gold coins to the bag 11, it is not easy to find 10 out.

So dichotomy is reasonable, this definition can solve the problem.

But when it came to the odd dichotomy of how to do it? For example, we want to get from 1 to 3

If the direct-half, then we need the bag 3/2 = 1 gold

Just need to be obtained and the remaining 3/2 = 1 gold

Therefore, the correct way is rounded up, require (1 + 3) / 2 = 2 gold

#include <bits/stdc++.h>
using namespace std;
int n,f[10000],cnt=1;
int main()
{
    cin>>n;
    while(n)
    {
        f[cnt++]=(n+1)/2;
        n/=2;
    }
    cout<<cnt-1<<endl;
    for(int i=cnt-1;i>=1;i--)
        cout<<f[i]<<" ";
}

 

 

 

End ------------ ------------ restore content

Guess you like

Origin www.cnblogs.com/iss-ue/p/12530920.html