24h購物| | PChome| 登入
2013-06-12 08:15:47| 人氣1,683| 回應0 | 上一篇 | 下一篇

[UVA][greedy] 410 - Station Balance

推薦 0 收藏 0 轉貼0 訂閱站台


 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 all S 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

題目描述:
要用離心機,每個試管最多放兩種不同的試樣,現在給你一些試樣,根據公式計算最小值。

題目解法:

基本上就是要求平衡。
很容易就想到是一個 greedy, 將大的數字配小的數字, 由於不是每個都可以匹配。
因此要補上一些 0 質量的試樣進行匹配。



#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;

int main() {
int C, S, cases = 0;
while(scanf("%d %d", &C, &S) == 2) {
int x[20] = {};
int n = 2*C, i, j, k;
double avg = 0;
for(i = 0; i < S; i++) {
scanf("%d", &x[i]);
avg += x[i];
}
avg /= C;
sort(x, x+n);
printf("Set #%d\n", ++cases);
double IMBALANCE = 0;
for(i = 0; i < C; i++) {
printf("%2d:", i);
if(x[i]) printf(" %d", x[i]);
if(x[n-i-1])printf(" %d", x[n-i-1]);
puts("");
IMBALANCE += fabs(x[i] + x[n-i-1] - avg);
}
printf("IMBALANCE = %.5lf\n\n", IMBALANCE);
}
return 0;
}

台長: Morris
人氣(1,683) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][greedy] 12321 - Gas Stations
此分類上一篇:[UVA] 11683 - Laser Sculpture

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文