题目:
Parliament
Time Limit:1000MS Memory Limit:10000K
Total Submit:3430 Accepted:1504
Description
New convocation of The Fool Land's Parliament consists of N delegates. According to the present regulation delegates should be divided into disjoint groups of different sizes and every day each group has to send one delegate to the conciliatory committee. The composition of the conciliatory committee should be different each day. The Parliament works only while this can be accomplished.
You are to write a program that will determine how many delegates should contain each group in order for Parliament to work as long as possible.
Input
The input file contains a single integer N (5<=N<=1000 ).
Output
Write to the output file the sizes of groups that allow the Parliament to work for the maximal possible time. These sizes should be printed on a single line in ascending order and should be separated by spaces.
Sample Input
Sample Output
代码实现:
3 4
//AC,输入输出不是这个题的,需要改改,实现的代码是哈尔滨工程大学的JudgeOnline
#include <stdio.h> int main() { int N,i,j,k; int D,S,t,w; scanf("%d",&N); for (i=0;i<N;i++) { scanf("%d",&D); S=0; for (j=2;S<=D;j++) { S+=j;} j--; S-=j; t=D-S; if (i) printf("/n"); if (t) { for (k=2;k+t<j;k++) printf("%d ",k); w=k+t; for (k++;k<j;k++) printf("%d ",k); printf("%d/n",w); } else { for (k=2;k<j-1;k++) printf("%d ",k); printf("%d/n",k); } } return 0; }
解题思路:
这个题刚开始看的时候没有一点思路;但是它竟然满足贪心算法,呵呵,有点不明白吧
因为它撇开了1,而且想它拆成的项乘积最大,所以必须拆成的项越多越好.
我们的策略就是将这个数从二开始累加,当和超过这个数的时候结束累加,将这个数减去前面的累加的和,得到的值进行和前面的值依次比较
但必须满足相加后的值不能和以有的项相等.
这个时候的项就是最后要求的结果.
对于这个题满足贪心算法的证明,我不给予证明,是个数学问题,有兴趣的自己可以去推导.
7