Skip to content

Instantly share code, notes, and snippets.

@vvolhejn
Created September 3, 2019 21:26
Show Gist options
  • Select an option

  • Save vvolhejn/af8d00e46069a836f1d251025ec8db6d to your computer and use it in GitHub Desktop.

Select an option

Save vvolhejn/af8d00e46069a836f1d251025ec8db6d to your computer and use it in GitHub Desktop.
MO-P ukazkova uloha reseni 1.5
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int vysledek_i = -1;
int vysledek_j = -1;
for (int i = 0; i < n; i++) {
int j_min = 0;
int j_max = n - 1;
// Dokud mame platny interval
while (j_min <= j_max) {
int j = (j_min + j_max) / 2;
if (a[i] + a[j] < k) {
j_min = j + 1;
} else if (a[i] + a[j] > k) {
j_max = j - 1;
} else if (i != j) {
// Spravny soucet a zaroven nepouzivame dvakrat stejny prvek
vysledek_i = i;
vysledek_j = j;
break;
}
}
}
if (vysledek_i == -1) {
// Nikdy jsme nenastavili vysledek_i, takze jsme nenasli reseni
cout << "reseni neexistuje" << endl;
} else {
cout << (vysledek_i+1) << " " << (vysledek_j+1) << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment