cses

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

commit 6115f7b029e960ac888b3fe7b0ca7a8ada099258
parent b4d79d74477b977fdc5ce0f274d8f9fdb401e3e8
Author: superpozycja <anna@superpozycja.net>
Date:   Fri,  1 Nov 2024 23:11:39 +0100

solve sum of four values

Diffstat:
Asorting_and_searching/sum_of_four_values.cpp | 78++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 78 insertions(+), 0 deletions(-)

diff --git a/sorting_and_searching/sum_of_four_values.cpp b/sorting_and_searching/sum_of_four_values.cpp @@ -0,0 +1,78 @@ +#include <bits/stdc++.h> + +using namespace std; + +using ui = unsigned int; +using l = long; +using ul = unsigned long; +using ll = long long; +using ull = unsigned long long; + +using vi = vector<int>; +using vui = vector<ui>; +using vl = vector<l>; +using vul = vector<ul>; +using vll = vector<ll>; +using vull = vector<ull>; + +void solve() +{ + int n, x; + cin >> n >> x; + int t[n]; + int og[n]; + for (int i = 0; i < n; i++) { + cin >> t[i]; + og[i] = t[i]; + } + + sort(t, t + n); + + for (int y = 0; y < n - 3; y++) { + for (int z = y + 1; z < n - 2; z++) { + int i = z + 1, j = n - 1; + while (i < j) { + if (t[i] + t[j] + t[z] + t[y] > x) { + j--; + } else if (t[i] + t[j] + t[z] + t[y] < x) { + i++; + } else { + int fi = 0, fj = 0; + int fz = 0, fy = 0; + for (int x = 0; x < n; x++) { + if (og[x] == t[y] && !fy) { + cout << x + 1 << " "; + fy = 1; + continue; + } + if (og[x] == t[z] && !fz) { + cout << x + 1 << " "; + fz = 1; + continue; + } + if (og[x] == t[i] && !fi) { + cout << x + 1 << " "; + fi = 1; + continue; + } + if (og[x] == t[j] && !fj) { + cout << x + 1 << " "; + fj = 1; + } + } + cout << "\n"; + return; + } + } + } + + } + cout << "IMPOSSIBLE\n"; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + solve(); +}