fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #define max_n 1e-9
  5.  
  6. int t = 1, test = 1, n;
  7. struct magnet{
  8. double pos;
  9. double weight;
  10. };
  11. magnet a[12];
  12.  
  13. inline double abs_sub(double x, double y){
  14. return x > y ? x - y : y - x;
  15. }
  16.  
  17. int calculate(double pos_mid, int id){
  18. double total_l = 0, total_r = 0;
  19.  
  20. for(int i = id; i >= 1; i--){
  21. double d1 = abs_sub(pos_mid, a[i].pos);
  22. double wei_1 = a[i].weight;
  23. total_l += wei_1 / (d1 * d1);
  24. }
  25. for(int i = id + 1; i <= n; i++){
  26. double d2 = abs_sub(pos_mid, a[i].pos);
  27. double wei_2 = a[i].weight;
  28. total_r += wei_2 / (d2 * d2);
  29. }
  30.  
  31. double tmp = abs_sub(total_l, total_r);
  32. if(tmp < max_n){
  33. return 0;
  34. }
  35. return total_l < total_r ? 1 : 2;
  36. }
  37.  
  38. double solution(int index){
  39. double start = a[index].pos;
  40. double end = a[index+1].pos;
  41. double result, mid;
  42.  
  43. while(start <= end){
  44. mid = (start + end) / 2;
  45. if(calculate(mid, index) == 0){
  46. result = mid;
  47. break;
  48. }else if(calculate(mid, index) == 1){
  49. start = mid;
  50. }else{
  51. end = mid;
  52. }
  53. //cout << mid << endl;
  54. }
  55. return result;
  56. }
  57.  
  58. int main(){
  59. freopen("input.txt", "r", stdin);
  60. while(t--){
  61. cin >> n;
  62. for(int i = 1; i <= n; i++){
  63. cin >> a[i].pos;
  64. }
  65. for(int i = 1; i <= n; i++){
  66. cin >> a[i].weight;
  67. }
  68. cout << "Case #" << test << endl;
  69. for(int i = 1; i < n; i++){
  70. cout << fixed;
  71. cout.precision(10);
  72. cout << solution(i) << " ";
  73. }
  74. cout << endl;
  75. /*for(int i = 1; i <= n; i++){
  76. cout << a[i].pos << " ";
  77. }
  78. for(int i = 1; i <= n; i++){
  79. cout << a[i].weight << " ";
  80. }
  81. cout << endl;*/
  82. test++;
  83. }
  84. //while(1);
  85. return 0;
  86. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Case #1