UVA - 410 Station Balance(贪心) (day_4_J)

版权声明:商业转载请联系作者获得授权,非商业转载请注明出处。 https://blog.csdn.net/weixin_43939327/article/details/86661546

Station Balance

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
i=1
| CMi − AM |
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)个腔室,每个腔室可包含0,1或2个样品。您将编写一个程序,将所有S样本分配给腔室,使得没有腔室包含超过2个样本,并且IMBALANCE的以下表达式被最小化。 (来自谷歌翻译)

题目分析:

要是s<c时,没有放东西的腔室也要print出来,s>c时每个都要放,有的放两个,有的放一个,单独放的必须比所有放两个大都大。

代码实现:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<stack>
#include<cstdlib>
#include<cmath>
#include<queue>
//#include <bits/stdc++.h>
using namespace std;

const int INF = 0x3f3f3f3f;
#define pf          printf
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define ms(i,j) memset(i,j,sizeof(i))

int a[12];
int main()
{
    int S=1,k,n,m,sum,i;
    double avg,blc;
    while(~sff(n,m))
    {
        ms(a,0);
        k=0,sum=0,blc=0;
        for(int i=0; i<m; i++)
            sf(a[i]),sum+=a[i];
        avg=((double)(sum))/n;
        sort(a,a+m);
        pf("Set #%d\n",S++);
        else if(m<=n)
        {
            for(i=0; i<n; i++)
            {
                if(a[i]!=0)
                    pf(" %d: %d\n",k++,a[i]);
                else
                    pf(" %d:\n",k++);
                blc+=fabs(avg-a[i]);
            }
        }
        else
        {
            for(i=m-1; i>2*m-2*n-1; i--)
            {
                pf(" %d: %d\n",k++,a[i]);
                blc+=fabs(avg-a[i]);
            }
            for(; i>=0; i-=2)
            {
                pf(" %d: %d %d\n",k++,a[i],a[2*m-2*n-1-i]);
                blc+=fabs(avg-a[i]-a[2*m-2*n-1-i]);
            }
        }
        pf("IMBALANCE = %.5lf\n\n",blc);
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43939327/article/details/86661546