fork download
  1. // "Brevity is the soul of wit."
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. #define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  7.  
  8. typedef long long ll;
  9.  
  10. struct Pair {
  11. int value;
  12. int index;
  13. Pair(int _value, int _index) :
  14. value(_value),
  15. index(_index)
  16. {}
  17. };
  18.  
  19. int main() {
  20. #ifdef LOCAL
  21. freopen("input.txt", "r", stdin);
  22. #endif
  23.  
  24. vector<int> A{10,9,8,7};
  25. vector<int> B{1,2,3,4};
  26.  
  27. Pair max1(INT_MIN, -1);
  28. Pair max2(INT_MIN, -1);
  29. for (int i = 0; i < (int)A.size(); i++) {
  30. if (max1.value < A[i]) {
  31. max1.value = A[i];
  32. max1.index = i;
  33. }
  34. if (max2.value < A[i] and max1.value != A[i]) {
  35. max2.value = A[i];
  36. max2.index = i;
  37. }
  38. }
  39.  
  40. Pair min1(INT_MAX, -1);
  41. Pair min2(INT_MAX, -1);
  42. for (int i = 0; i < (int)B.size(); i++) {
  43. if (min1.value > B[i]) {
  44. min1.value = B[i];
  45. min1.index = i;
  46. }
  47. if (min2.value > B[i] and min1.value != B[i]) {
  48. min2.value = B[i];
  49. min2.index = i;
  50. }
  51. }
  52.  
  53. if (max1.index != min1.index) {
  54. cout << max1.value - min1.value << "\n";
  55. } else {
  56. cout << max(max1.value - min2.value, max2.value - min1.value) << "\n";
  57. }
  58.  
  59. return 0;
  60. }
  61.  
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
8