Advertisement
newb_ie

Untitled

Sep 22nd, 2021
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2. using namespace std;
  3.  
  4. int main () {
  5. ios::sync_with_stdio(false);
  6. cin.tie(nullptr);
  7. cout.tie(nullptr);
  8. int n;
  9. cin >> n;
  10. long long a[n + 1];
  11. for (int i = 1; i <= n; ++i) cin >> a[i];
  12. sort (a + 1,a + n + 1);
  13. long long sum = 0;
  14. //1 based approach
  15. //lower_bound(a + 1,a + n + 1,x) - (a + 1)
  16. //0 based approach
  17. //lower_bound(a,a + n,x) - a;
  18. for (int i = 1; i <= n; ++i) sum += a[i];
  19. int m;
  20. cin >> m;
  21. for (int i = 1; i <= m; ++i) {
  22. long long res = LLONG_MAX;
  23. long long x,y;
  24. cin >> x >> y; // x = def_power,y = atk power;
  25. int idx = n;
  26. int l = 1,r = n;
  27. while (l <= r) {
  28. int mid = l + (r - l) / 2;
  29. if (a[mid] < x) {
  30. l = mid + 1;
  31. } else {
  32. idx = mid;
  33. r = mid - 1;
  34. }
  35. }
  36. long long p2_attack_need = max(0LL,x - a[idx]); //atk need
  37. long long p2_def_need = max(0LL,y - (sum - a[idx]));
  38. res = min(res,p2_attack_need + p2_def_need);
  39. if (idx != 1) {
  40. long long p1_attack_need = max(0LL,x - a[idx - 1]);
  41. long long p1_def_need = max(0LL,y - (sum - a[idx - 1]));
  42. res = min(res,p1_attack_need + p1_def_need);
  43. }
  44. cout << res << '\n';
  45. }
  46. }
  47.  
  48. Problem Link : https://codeforces.com/contest/1574/problem/C
  49.  
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement