fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6. inline int power(int a, int b) {
  7. int x = 1;
  8. while (b) {
  9. if (b & 1) x *= a;
  10. a *= a;
  11. b >>= 1;
  12. }
  13. return x;
  14. }
  15.  
  16.  
  17. const int M = 1000000007;
  18. const int N = 3e5+9;
  19. const int INF = 2e9+1;
  20. const int LINF = 2000000000000000001;
  21.  
  22. //_ ***************************** START Below *******************************
  23.  
  24.  
  25. bool isPossible(int n, int m, int k, int mid){
  26. int sum = mid*(mid+1)/2;
  27. int x = mid-k;
  28. int y = mid - (n-k+1);
  29.  
  30. int left = 0;
  31. if(x>=0){
  32. left = sum - (x*(x+1))/2;
  33. }
  34. else left = sum + abs(x);
  35.  
  36. int right = 0;
  37. if(y>=0){
  38. right = sum - (y*(y+1))/2;
  39. }
  40. else right = sum + abs(y);
  41.  
  42.  
  43. int total = left + right - mid;
  44.  
  45. return total <= m;
  46. }
  47.  
  48. void consistency(int n, int m, int k){
  49.  
  50. int s = 0, e = n-1;
  51. while(s<e){
  52. int mid = s + (e-s+1)/2;
  53. if(isPossible(n, m, k, mid)){
  54. s = mid;
  55. }
  56. else e = mid-1;
  57. }
  58.  
  59. cout << e << endl;
  60.  
  61. }
  62.  
  63. void solve() {
  64.  
  65. int n, m, k;
  66. cin>>n >> m >> k;
  67.  
  68. consistency(n, m, k);
  69.  
  70.  
  71. }
  72.  
  73.  
  74.  
  75.  
  76.  
  77. int32_t main() {
  78. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  79.  
  80. int t = 1;
  81. cin >> t;
  82. while (t--) {
  83. solve();
  84. }
  85.  
  86. return 0;
  87. }
Success #stdin #stdout 0s 5316KB
stdin
1
5 11 3
stdout
3