FreakSkipper

Valor Exato

Sep 19th, 2020 (edited)
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define vi vector<int>
  6. #define ll long long
  7. #define pb push_back
  8. #define mp make_pair
  9. #define ii pair<int, int>
  10.  
  11. int main() {
  12.     int n, m;
  13.     cin >> n >> m;
  14.     vi livro(n);
  15.  
  16.     for (int i = 0; i < n; i++) {
  17.         cin >> livro[i];
  18.     }
  19.  
  20.     sort(livro.begin(), livro.end());
  21.  
  22.     int x = -1, y = -1;
  23.     for (int i = 0; i < n - 1 && livro[i] <= m / 2; i++) {
  24.         for (int j = i + 1; j < n; j++) {
  25.             if (livro[i] + livro[j] == m) {
  26.                 if (x == -1 || abs(x - y) > abs(livro[i] - livro[j])) {
  27.                     x = livro[i];
  28.                     y = livro[j];
  29.                     break;
  30.                 }
  31.             }
  32.         }
  33.     }
  34.  
  35.     cout << x << " " << y << endl;
  36.  
  37.     return 0;
  38. }
Add Comment
Please, Sign In to add comment