Created
September 3, 2019 21:26
-
-
Save vvolhejn/af8d00e46069a836f1d251025ec8db6d to your computer and use it in GitHub Desktop.
MO-P ukazkova uloha reseni 1.5
This file contains hidden or 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 <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