The International Space Station contains many centrifuges in its labs. Each centrifuge will have some
number © of chambers each of which can contain 0, 1, or 2 specimens. You are to write a program
which assigns all S specimens to the chambers such that no chamber contains more than 2 specimens
and the following expression for IMBALANCE is minimized.
IMBALANCE =
C
∑| CMi − AM |
i=1
where:
CMi
is the Chamber Mass of chamber i and is computed by summing the masses of the specimens
assigned to chamber i.
AM is the Average Mass of the chambers and is computed by dividing the sum of the masses of all
specimens by the number of chambers ©.
Input
Input to this program will be a file with multiple sets of input. The first line of each set will contain
two numbers. The first number (1 ≤ C ≤ 5) defines the number of chambers in the centrifuge and the
second number (1 ≤ S ≤ 2C) defines the number of specimens in the input set. The second line of
input will contain S integers representing the masses of the specimens in the set. Each specimen mass
will be between 1 and 1000 and will be delimited by the beginning or end of the line and/or one or
more blanks.
Output
For each input set, you are to print a line specifying the set number (starting with 1) in the format
‘Set #X’ where X is the set number.
The next C lines will contain the chamber number in column 1, a colon in column number 2, and
then the masses of the specimens your program has assigned to that chamber starting in column 4.
The masses in your output should be separated by exactly one blank.
Your program should then print ‘IMBALANCE = X’ on a line by itself where X is the computed
imbalance of your specimen assignments printed to 5 digits of precision to the right of the decimal.
The final line of output for each set should be a blank line. (Follow the sample output format.)
Sample Input
2 3
6 3 8
3 5
51 19 27 14 33
5 9
1 2 3 5 7 11 13 17 19
Sample Output
Set #1
0: 6 3
1: 8
IMBALANCE = 1.00000
Set #2
0: 51
1: 19 27
2: 14 33
IMBALANCE = 6.00000
Set #3
0: 1 17
1: 2 13
2: 3 11
3: 5 7
4: 19
IMBALANCE = 11.60000
问题描述
实验室里有许多离心机,现在要测试样品的质量,给你C个实验室和S个样品,并给出样品的质量,每间实验室最多能放2个样品,求最小的IMBALANCE,IMBALANCE的求法在题目上,其中CMi表示第i个实验室中的样品质量和,AM表示C个实验室的CMi的平均值。以题目的输出格式输出。
问题分析
排序后,先把数值大的单独放,剩下的按一大一小组合放置,如剩余中最大的和最小的安排在一起,次大的和次小的安排在一起,以此类推。
这题较需要注意输出格式,有个地方比较坑,即使是实验室是空的也要按格式输出。
参考:https://www.algorithmist.com/index.php/UVa_410
代码如下
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
int main(){
int time,c,s,i,h,k,d,x[15];
double ave,ans;
time=1;
while(scanf("%d %d",&c,&s)!=EOF){
ave=0;
for(i=0;i<s;i++){
scanf("%d",x+i);
ave+=x[i];
}
ave/=c;
sort(x,x+s);
printf("Set #%d\n",time++);
if(c>s) d=s;//单独放的个数
else d=2*c-s;
ans=0;
k=0;//计数
for(h=s-1;h>=0&&k<d;h--){//把大的单独放
ans+=abs(ave-x[h]);
printf(" %d: %d\n",k++,x[h]);
}
if(c>s){//如果可以让全部样品单独放,有些实验室会是空着的,对应按格式输出和计数
while(k<c) printf(" %d:\n",k++);
ans+=ave*(c-s);
}
else
for(i=0;k<c;i++,h--){//剩下的样品按最大和最小的组合,次大和次小的组合依次类推
ans+=abs(ave-x[i]-x[h]);
printf(" %d: %d %d\n",k++,x[i],x[h]);
}
printf("IMBALANCE = %.5lf\n\n",ans);
}
return 0;
}