UVa410 - Station Balance

 Station Balance 

The International Space Station contains many centrifuges in its labs. Each centrifuge will have some number (C) of chambers each of which can contain 0, 1, or 2 specimens. You are to write a program which assigns allS specimens to the chambers such that no chamber contains more than 2 specimens and the following expression for IMBALANCE is minimized.

displaymath38

where:

tex2html_wrap_inline40
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 ( C).

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 ( tex2html_wrap_inline50 ) defines the number of chambers in the centrifuge and the second number ( tex2html_wrap_inline52 ) 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
 
思路是贪心,先排序,如果S<C 那么直接填,否则的话,先处理大的,将前2*C-S个大数占一个格子,剩下的几个,按两头取,例如:1 2 3 4 取 1,4和2,3分别填到格子里!
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
vector<vector<int> > vsp;
int c,s;
int num[20];
double chm[20];
void init(){
    vsp.clear();
    vsp.resize(c);
}
int main(){
    int T = 1;
    while(~scanf("%d%d",&c,&s)){
            init();
            double aver = 0;
            for(int i = 0; i < s; i++){
                scanf("%d",&num[i]);
                aver += num[i]*1.0;
            }
            memset(chm,0.0,sizeof chm);
            aver /= c;
            sort(num,num+s);
             int ind = 0;
            if(s <= c){
                for(; ind < s; ind++){
                    vsp[ind].push_back(num[ind]);
                    chm[ind] = num[ind]*1.0;
                }
            }else{
                int ed = s-1;
                for(; ind < 2*c-s; ind++){
                    vsp[ind].push_back(num[ed]);
                    chm[ind] = num[ed]*1.0;
                    ed--;
                }
                int sta = 0;
                while(sta < ed){
                    vsp[ind].push_back(num[sta]);
                    vsp[ind].push_back(num[ed]);
                    chm[ind] = (num[ed]+num[sta])*1.0;
                    ind++;
                    sta++;
                    ed--;
                }
            }
            double ans = 0;
            for(int i = 0; i < vsp.size(); i++){
                ans += fabs(aver-chm[i]);
            }
       //     cout<<aver<<endl;
            printf("Set #%d\n",T++);
            for(int i = 0; i < vsp.size(); i++){
                printf(" %d:",i);
                for(int j = 0; j < vsp[i].size(); j++){
                    cout<<" "<<vsp[i][j];
                }
                cout<<endl;
            }
            printf("IMBALANCE = %.5lf\n\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值