CodeForces 2020 Ateneo de Manila University D. Riana and Distribution of Pie 思维题

1. 题目描述

1.1. Limit

Time Limit: 1 second

Memory Limit: 256 megabytes

1.2. Problem Description

Riana baked a round pie in preparation for her birthday party. After baking the pie, she thought that it would be boring to simply give the pie evenly across everyone attending her party. After some thought, she came up with the idea of using the pie in a game for the party goers.

In her game, each person attending the party will each take only one turn.

One full turn goes like this:

  1. Person i i begins his turn by choosing a percentage P i % P_i\% between 0-100.

  2. Person i i then takes P i % P_i\% of the pie remaining.

  3. Person i i ends his turn by taking P i % P_i\% of the slices of pie that have already been taken by everyone who went before person i i .

For example, if four people Mathil, Quin, Octavian, and Velyna attend the party:

  • In the first turn, Mathil starts by choosing 30%.

  • He then takes 30% of the remaining pie which has not been touched, meaning Mathil takes 30%.

    • Mathil has 30% of the pie.
    • The pie is now 70% of its original size.
  • In the second turnn Quin chooses 20%.

  • Quin then takes 20% of what’s left of the pie, which is 14% of the pie’s original size.

  • Next, Quin takes 20% of Mathil’s slice, which is 6% of the pie’s original size.

    • Mathil now only has 24% of the pie, while Quin has (14%+6%) 20% of the pie’s original size.
    • The pie is now 56% of its original size.
  • In the third turn, Octavian comes in and chooses 10%.

  • Octavian take 10% of the what’s left of the pie, which is 5.6% of the pie’s original size.

  • He then takes 10% of Mathil’s remaining 24% and Quin’s 20%

    • Mathil now only has 21.6% of the pie’s original size.
    • Quin now only has 18% of the pie’s original size.
    • Octavian has 10% of the pie’s original size.
    • The pie is now 50.4% of its original size.
  • In the fourth and final turn, Velyna comes in and chooses 50%.

  • Velyna takes 50% of the what’s left of the pie, which is 25.2% of the pie’s original size.

  • She then takes 50% of Mathil’s remaining 21.6%, Quin’s remaining 18%, and Octavian’s 10%.

    • Mathil now only has 10.8% of the pie’s original size.
    • Quin now only has 9% of the pie’s original size.
    • Octavian now only has 5% of the pie’s original size.
    • Velyna has 50% of the pie’s original size.
    • The pie is now 25.2% of its original size.

But wait! this is wrong! Riana’s household has a strict ‘no leftovers’ policy, so she wants 100% of the pie to get eaten!

Riana is also an advocate of equality, so she wants to distribute the pie slices such that the difference between the biggest slice and the smallest slice is minimized.

However, Riana is also secretly lazy, so she enlisted the help of her friend, you! Help her figure out the optimal percentage each person should take.

1.3. Input

The input will contain one line containing a single integer N N , the number of people going to the party.

The value N N is guaranteed to be between 1 1 and 1 0 3 10^{3} .

1.4. Output

Output contains N N lines.

The i i th line of the output must contain the percentage of the pie which the i i th person to take slices should take. (The first person’s percentage should be on the first line, the second should be on the second line, the third should be on the third line, etc.)

Answers will be accepted if they are within 1 0 4 10^{-4} away from the correct answer.

1.5. Sample Input

1

1.6. Sample Output

100.0000

1.7. Note

In the sample test case, since the first person is the only person in the party (quite sad), he takes the whole cake.

This satisfies the two conditions that Riana wanted to satisfy:

  1. 100% of the cake is eaten.
  2. The difference between the Maximum and the Minimum is minimized. (The difference is 0.)

1.8. Source

CodeForces 102556 D. Riana and Distribution of Pie


2. 解读

题意:桌子上有一只炸鸡,它的初始比例为 100 % 100\%

  • Person i i 先在 [ 0 , 100 ] [0,100] 中选择一个数字 P i P_i ,构成比例 P i % P_i\%

  • Person i i 再拿走桌子上剩下的炸鸡的 P i % P_i\%

  • Person i i 最后非常残忍地依次从每个在他前面拿炸鸡的人手中拿走 P i % P_i\% 他们手中的炸鸡。

你以为这样就完事了吗?还有两个要求

  1. 炸鸡必须 100 % 100\% 地被分掉
  2. 分到最多的人和分到最少的人他们手中炸鸡比例的差值要最小

输出的结果与标准答案差值不超过 1 0 4 10^{-4} 即为正确。

题解

  • 首先从炸鸡必须 100 % 100\% 地被分掉这个条件来考虑。如果每个人选择的比例 P i % [ 0 , 1 ) P_i\% \in[0, 1) ,那么炸鸡永远不会被分完,所以肯定要有一个人的 P i % = 1 P_i\% = 1 ,如果这个人不是第一个人,则会把前面的人手上的炸鸡全部抢走,实在过于残忍,也不满足要求2,所以让第一个人先拿走全部的炸鸡。

  • 再从要求2来考虑,全部平分的情况下,差值是最小的,那么要怎么达到平分的效果呢?我们已经有了 P 1 % = 1 P_1\% = 1 ,我们可以再想到,最后一个人要从前面的所有人手上拿炸鸡,那么最后他手上一定会有 P n % P_n\% 个炸鸡。

有了对第一个人和最后一个人的推断,那么考虑只有2个人的情况, P 1 % = 1 P_1\% = 1 P 2 % = 0.5 P_2\% = 0.5 ,炸鸡就被平分了。

再考虑3个人的情况,3个人要平分的话,每个人 1/3,也就是 P 3 % = 1 / 3 P_3\% = 1/3 ,那么其实只要前面2个人手上各有 0.5只炸鸡,最后炸鸡就被平分了。

于是我们就可以推断,第 i i 个人的分配比例为

P i % = 1 i P_i\% = \frac{1}{i}

保留5位小数即可。

3. 代码

#include <iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        printf("%.5lf\n", (1.0 / i) * 100);
    }
}


联系邮箱[email protected]

CSDNhttps://me.csdn.net/qq_41729780

知乎https://zhuanlan.zhihu.com/c_1225417532351741952

公众号复杂网络与机器学习

欢迎关注/转载,有问题欢迎通过邮箱交流。

猜你喜欢

转载自blog.csdn.net/qq_41729780/article/details/106664114