fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5.  
  6. // دالة بتقرر إذا كنا نقدر نخلي كل البطاريات عند القيمة mid
  7. bool can(double mid, const vector<int>& a, double k) {
  8. double surplus = 0.0, need = 0.0;
  9.  
  10. for (double energy : a) {
  11. if (energy > mid) {
  12. surplus += energy - mid; // طاقة زيادة ممكن ننقلها
  13. } else {
  14. need += mid - energy; // طاقة ناقصة محتاجينها
  15. }
  16. }
  17.  
  18. // اللي هيوصل فعليًا للطرف التاني بعد الفقد
  19. double effective_surplus = surplus * (1.0 - k / 100.0);
  20.  
  21. return effective_surplus >= need;
  22. }
  23.  
  24. int main() {
  25. ios::sync_with_stdio(false);
  26. cin.tie(nullptr);
  27.  
  28. int n;
  29. double k;
  30. cin >> n >> k;
  31.  
  32. vector<int> a(n);
  33. for (int& x : a) cin >> x;
  34.  
  35. double low = 0.0, high = 1e9, ans = 0.0;
  36.  
  37. // نعمل binary search على القيمة المطلوبة
  38. for (int iter = 0; iter < 100; ++iter) {
  39. double mid = (low + high) / 2.0;
  40.  
  41. if (can(mid, a, k)) {
  42. ans = mid;
  43. low = mid; // نحاول نكبر القيمة
  44. } else {
  45. high = mid; // القيمة دي كبيرة، نجرب أصغر
  46. }
  47. }
  48.  
  49. cout << fixed << setprecision(9) << ans << "\n";
  50.  
  51. return 0;
  52. }
  53.  
Success #stdin #stdout 0s 5316KB
stdin
3 50
4 2 1
stdout
2.000000000