(代码实现)北京大学-JudgeOnline-1032

题目:

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值