cses

solution to cses exercise problems
git clone git://git.superpozycja.net/cses
Log | Files | Refs | README

sum_of_two_values.cpp (904B)


      1 #include <bits/stdc++.h>
      2 
      3 using namespace std;
      4 
      5 using ui = unsigned int;
      6 using l = long;
      7 using ul = unsigned long;
      8 using ll = long long;
      9 using ull = unsigned long long;
     10 
     11 using vi = vector<int>;
     12 using vui = vector<ui>;
     13 using vl = vector<l>;
     14 using vul = vector<ul>;
     15 using vll = vector<ll>;
     16 using vull = vector<ull>;
     17 
     18 void solve()
     19 {
     20 	int n, x;
     21 	cin >> n >> x;
     22 	int t[n];
     23 	int og[n];
     24 	for (int i = 0; i < n; i++) {
     25 		cin >> t[i];
     26 		og[i] = t[i];
     27 	}
     28 
     29 	sort(t, t + n);
     30 
     31 	int i = 0, j = n-1;
     32 
     33 	while (i < j) {
     34 		if (t[i] + t[j] > x) {
     35 			j--;
     36 		} else if (t[i] + t[j] < x) {
     37 			i++;
     38 		} else {
     39 			int fi = 0, fj = 0;
     40 			for (int x = 0; x < n; x++) {
     41 				if(og[x] == t[i] && !fi) {
     42 					cout << x+1 << " ";
     43 					fi = 1;
     44 					continue;
     45 				}
     46 				if(og[x] == t[j] && !fj) {
     47 					cout << x+1 << " ";
     48 					fj = 1;
     49 				}
     50 			}
     51 			cout << "\n";
     52 			return;
     53 
     54 		}
     55 	}
     56 
     57 	cout << "IMPOSSIBLE\n";
     58 
     59 }
     60 
     61 int main()
     62 {
     63 	solve();
     64 }