Created
April 30, 2019 02:35
-
-
Save sofhiasouza/2ef2ada6ad54554930d3e955661273bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
#define MAXN 100010 | |
using namespace std; | |
int main() | |
{ | |
int n, vet[MAXN], k; | |
cin >> n; | |
for(int i = 0 ; i < n ; i++) | |
{ | |
cin >> vet[i]; | |
} | |
cin >> k; | |
for(int i = 0 ; i < n ; i++) //faço a busca binária para cada casa | |
{ | |
int ini = 0, fim = n-1, r; | |
while(ini <= fim) | |
{ | |
int meio = (ini+fim)/2; | |
if(vet[i] + vet[meio] >= k) | |
{ | |
r = meio; | |
fim = meio-1; | |
} | |
else ini = meio+1; | |
} | |
if(vet[i] + vet[r] == k) //se encontrei uma soma que é igual a k, printo os dois valores | |
{ //primeiro o menor e depois o maior, e dou um break | |
cout << min(vet[i], vet[r]) << ' ' << max(vet[i], vet[r]) << "\n"; | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment