描述:在一个圆形操场的四周摆放着n堆石子。现要将石子有次序的合并成一堆。规定每次至少选2堆最多选k堆石子合并成新的一堆,合并的费用为新的一堆的石子数。试设计一个算法,计算出将n堆石子合并成一堆的最大费用和最小费用。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
#include <stdio.h>
2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
int n, k, data[99999];
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
void bubble_sort();
6
int cal_max();
7
int cal_min();
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
int main()
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
11
int i;
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while(scanf("%d %d", &n, &k) == 2)
{
14
for(i=1; i<=n; i++)
15
scanf("%d", &data[i]);
16
bubble_sort();
17
printf("%d %d\n\n", cal_max(), cal_min());
18
}
19![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
return 0;
21
}
22![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
23
void bubble_sort()
24![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
25
int i, j;
26![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
for(i=n; i>1; i--)
28![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(j=1; j<i; j++)
{
29![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(data[j] > data[j+1])
{
30
int temp = data[j];
31
data[j] = data[j+1];
32
data[j+1] = temp;
33
}
34
}
35
}
36![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
37
int cal_max()
38![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
39
int i, sum=0, total, a[99999];
40![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
for(i=1; i<=n; i++)
42
a[i] = data[i];
43![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(i=n; i>1; i--)
{
44
total = a[i] + a[i-1];
45
a[i-1] = total;
46
sum += total;
47
}
48![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
return sum;
50
}
51![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
52
int cal_min()
53![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
54
int temp, t=1, sum =0, i, j, a[99999];
55
56
for(i=1; i<=n; i++)
57
a[i] = data[i];
58![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while(t <= (n-k+1))
{
59
temp = 0;
60
for(i=0; i<k; i++)
61
temp += a[t+i];
62
i = t+k;
63
while(a[i]<temp && i<=n)
64
i++;
65
for(j=(t+k-1); j<i; j++)
66
a[j] = a[j+1];
67
a[i-1] = temp;
68
t += (k-1);
69
sum += temp;
70
}
71![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(t>(n-k+1) && t<n)
{
72
temp = 0;
73
for(i=t; i<=n; i++)
74
temp += a[i];
75
sum += temp;
76
}
77![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
78
return sum;
79
}