https://code.google.com/codejam/contest/3274486/dashboard

Problem

The kitchen at the Infinite House of Pancakes has just received an order for a stack of K pancakes! The chef currently has N pancakes available, where N ≥ K. Each pancake is a cylinder, and different pancakes may have different radii and heights.

As the sous-chef, you must choose K out of the N available pancakes, discard the others, and arrange those K pancakes in a stack on a plate as follows. First, take the pancake that has the largest radius, and lay it on the plate on one of its circular faces. (If multiple pancakes have the same radius, you can use any of them.) Then, take the remaining pancake with the next largest radius and lay it on top of that pancake, and so on, until all K pancakes are in the stack and the centers of the circular faces are aligned in a line perpendicular to the plate, as illustrated by this example:

A stack of pancakes with varying radii and thicknesses, obeying the rules in the statement.

You know that there is only one thing your diners love as much as they love pancakes: syrup! It is best to maximize the total amount of exposed pancake surface area in the stack, since more exposed pancake surface area means more places to pour on delicious syrup. Any part of a pancake that is not touching part of another pancake or the plate is considered to be exposed.

If you choose the K pancakes optimally, what is the largest total exposed pancake surface area you can achieve?

Input

The first line of the input gives the number of test cases, T. T test cases follow. Each begins with one line with two integers N and K: the total number of available pancakes, and the size of the stack that the diner has ordered. Then, N more lines follow. Each contains two integers Ri and Hi: the radius and height of the i-th pancake, in millimeters.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the maximum possible total exposed pancake surface area, in millimeters squared. y will be considered correct if it is within an absolute or relative error of 10-6 of the correct answer. See the FAQ for an explanation of what that means, and what formats of real numbers we accept.

Limits

1 ≤ T ≤ 100.
1 ≤ K ≤ N.
1 ≤ Ri ≤ 106, for all i.
1 ≤ Hi ≤ 106, for all i.

Small dataset
1 ≤ N ≤ 10.

Large dataset
1 ≤ N ≤ 1000.

Sample

Input

4
2 1
100 20
200 10
2 2
100 20
200 10
3 2
100 10
100 10
100 10
4 2
9 3
7 1
10 1
8 4

Output

Case #1: 138230.076757951
Case #2: 150796.447372310
Case #3: 43982.297150257
Case #4: 625.176938064

In Sample Case #1, the “stack” consists only of one pancake. A stack of just the first pancake would have an exposed area of π × R02 + 2 × π * R0 × H0 = 14000π mm2. A stack of just the second pancake would have an exposed area of 44000π mm2. So it is better to use the second pancake.

In Sample Case #2, we can use both of the same pancakes from case #1. The first pancake contributes its top area and its side, for a total of 14000π mm2. The second pancake contributes some of its top area (the part not covered by the first pancake) and its side, for a total of 34000π mm2. The combined exposed surface area is 48000π mm2.

In Sample Case #3, all of the pancakes have radius 100 and height 10. If we stack two of these together, we effectively have a single new cylinder of radius 100 and height 20. The exposed surface area is 14000π mm2.

In Sample Case #4, the optimal stack uses the pancakes with radii of 8 and 9.

Key

可以用DP做。对DP还是不太熟练,一开始没排序,于是考虑的情况就多一些,总有小规模案例不正确,偏小。后来排个序考虑起来就方便多了。时间复杂度O(n2)。难倒是不难,之前没Debug出来才是最气的。时间复杂度O(n2)。

其实贪心就可以了,同学贪心做的但是时间复杂度也要O(n2),就是少很多赋值。

Code

#include<iostream>
#include<iomanip>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long lld; const int maxn = 1000 + 10;
const long double pi = 3.1415926535897932; int T, N, K;
struct RH { lld R, H, A; } arr[maxn];
lld dp[maxn]; bool cmp(RH &a, RH &b) {
if (a.R == b.R) return a.H > b.H;
return a.R > b.R;
} int main()
{
//freopen("A-small-practice.in", "r", stdin);
//freopen("A-small-practice.out", "w", stdout);
ios::sync_with_stdio(false);
cin >> T;
for (int now_case = 1; now_case <= T; ++now_case) {
cin >> N >> K;
for (int i = 0; i < N; ++i) {
cin >> arr[i].R >> arr[i].H;
arr[i].A = arr[i].R * arr[i].H * 2;
arr[i].R *= arr[i].R;
}
sort(arr, arr + N, cmp);
memset(dp, 0, sizeof(dp));
for (int i = 0; i < N; ++i) {
for (int j = K; j > 1; --j) {
if (dp[j - 1] == 0) continue;
lld tmp = dp[j - 1] + arr[i].A;
if (dp[j] < tmp) dp[j] = tmp;
}
lld tmp = arr[i].A + arr[i].R;
if (dp[1] < tmp) dp[1] = tmp;
}
cout << "Case #" << now_case << ": " << fixed << setprecision(9) << ((long double)dp[K] * pi) << endl;
}
return 0;
}

最新文章

  1. C中的fseek函数使用
  2. codevs 1227 方格取数 2
  3. 序列化反序列化api(入门级)
  4. 【转】OpenStack奥斯汀峰会Keynotes国内抢先看
  5. CentOS-7.0.中安装与配置Tomcat-7的方法
  6. NSInvocation
  7. SQL Server数据库PIVOT函数的使用详解(一)
  8. App内切换语言
  9. eclipse中配置spring环境
  10. Linux下修改Swap分区大小
  11. Windows上安装MySQL的完整教程
  12. 相似度度量:欧氏距离与余弦相似度(Similarity Measurement Euclidean Distance Cosine Similarity)
  13. mybatis一级缓存
  14. Python3 下实现 腾讯人工智能API 调用
  15. 解决input 有readonly属性 各个浏览器的光标兼容性问题
  16. API接口规范
  17. IKAnalyzer兼容Lucene 5.4.0版本抛出异常?
  18. electron安装与使用
  19. java各种链路工具性能监控工具
  20. 比较String.substring()、String.slice()、String.substr()的区别

热门文章

  1. Kruskal算法的实现
  2. WKWebView代理方法解析
  3. 记一次解析XML转对象的笔记
  4. PRINCE2认证
  5. Math.pow用法及实现探究
  6. BroadcastReceiver简单应用实例
  7. 4日6日--ES5新增数组方法
  8. selenium实例:unittest框架+PO开发模式
  9. mysql数据库实操笔记20170418
  10. iOS多线程的三种方法