cses

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

sum_of_four_values.cpp (1339B)


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