http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=953
这道题是比较简单的枚举加上贪心的题目,题意是将n个圆放进矩形内,要求圆要贴着矩形的底部,问矩形的长最小是多少。
题目需要注意的是有可能有两个半径非常大的圆夹着两个半径非常小的圆的情况,除此之外,代码都很简单。
代码如下:
View Code
1 #define REP(i, n) for (int i = 0; i < (n); i++) 2 #define REP_1(i, n) for (int i = 1; i <= (n); i++) 3 4 int idx[10]; 5 double R[10], pos[10]; 6 7 double cal(int n) { 8 n--; 9 pos[0] = 0.0; 10 REP_1(i, n) { 11 double tmp = -finf; 12 REP(j, i) { 13 tmp = max(tmp, pos[j] + sqrt(sqr(R[idx[i]] + R[idx[j]]) - sqr(R[idx[i]] - R[idx[j]]))); 14 } 15 pos[i] = max(R[idx[i]] - R[idx[0]], tmp); 16 } 17 double ret = 0.0; 18 REP(i, n + 1) ret = max(ret, pos[i] + R[idx[i]]); 19 return R[idx[0]] + ret; 20 } 21 22 double work(int n) { 23 double ret = finf; 24 REP(i, n) idx[i] = i; 25 do { 26 ret = min(ret, cal(n)); 27 } while (next_permutation(idx, idx + n)); 28 return ret; 29 } 30 31 int main() { 32 int T, n; 33 scanf("%d", &T); 34 while (T-- && scanf("%d", &n)) { 35 REP(i, n) scanf("%lf", &R[i]); 36 printf("%.3f\n", work(n)); 37 } 38 return 0; 39 }
——written by Lyon