cses

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

sum_of_three_values.cpp (1148B)


      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 	for (int z = 0; z < n-2; z++) {
     32 		int i = z + 1, j = n - 1, tmp = t[z];
     33 		while (i < j) {
     34 			if (t[i] + t[j] + tmp > x) {
     35 				j--;
     36 			} else if (t[i] + t[j] + tmp < x) {
     37 				i++;
     38 			} else {
     39 				int fi = 0, fj = 0;
     40 				int fz = 0;
     41 				for (int x = 0; x < n; x++) {
     42 					if (og[x] == t[z] && !fz) {
     43 						cout << x + 1 << " ";
     44 						fz = 1;
     45 						continue;
     46 					}
     47 					if (og[x] == t[i] && !fi) {
     48 						cout << x + 1 << " ";
     49 						fi = 1;
     50 						continue;
     51 					}
     52 					if (og[x] == t[j] && !fj) {
     53 						cout << x + 1 << " ";
     54 						fj = 1;
     55 					}
     56 				}
     57 				cout << "\n";
     58 				return;
     59 			}
     60 		}
     61 	}
     62 	cout << "IMPOSSIBLE\n";
     63 }
     64 
     65 int main()
     66 {
     67 	ios::sync_with_stdio(0);
     68 	cin.tie(0);
     69 	solve();
     70 }