fork(2) download
  1. //Mitesh Agrawal
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. #define ii pair<ll,ll>
  6. #define dd pair<double,ll>
  7. #define ll long long
  8.  
  9. ll n,k;
  10. ii cake[1005];
  11. dd dp[1005][1005];
  12.  
  13. //ans, last index
  14. dd f(ll idx, ll req){
  15. ll i,j;
  16. dd &ans = dp[idx][req];
  17. dd ans1, ans2;
  18. if(ans != dd(-1,-1)) return ans;
  19. if(req == 0) return ans = ii(0,-1);
  20. if(idx + 1 == req){
  21. ans.first = M_PI * cake[idx].first * cake[idx].first;
  22. for(i = 0; i <= idx; i++)
  23. ans.first += M_PI * 2.0 * cake[i].first * cake[i].second;
  24. ans.second = idx;
  25. return ans;
  26. }
  27. ans1 = f(idx - 1, req);
  28. ans2 = f(idx - 1, req - 1);
  29. if(ans2.second > -1)
  30. ans2.first -= M_PI * cake[ans2.second].first * cake[ans2.second].first;
  31. ans2.first += M_PI * cake[idx].first * cake[idx].first;
  32. ans2.first += M_PI * 2.0 * cake[idx].first * cake[idx].second;
  33. ans2.second = idx;
  34. return ans = max(ans1, ans2);
  35. }
  36.  
  37. int main(){
  38. ll i,j,test,t;
  39. scanf("%lld",&t);
  40. for(test = 1; test <= t; test++){
  41. scanf("%lld %lld",&n,&k);
  42. for(i = 0; i < n; i++)
  43. scanf("%lld %lld",&cake[i].first, &cake[i].second);
  44. sort(cake, cake+n);
  45. for(i = 0; i <= n; i++)
  46. for(j = 0; j <= k; j++)
  47. dp[i][j] = dd(-1, -1);
  48. printf("Case #%lld: %0.12lf\n",test, f(n-1, k).first);
  49. }
  50. return 0;
  51. }
Success #stdin #stdout 0s 31864KB
stdin
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
stdout
Case #1: 138230.076757950912
Case #2: 150796.447372310096
Case #3: 43982.297150257102
Case #4: 625.176938064369