Task: | 3SUM |
Sender: | laluj |
Submission time: | 2024-10-21 16:58:15 +0300 |
Language: | C++ (C++17) |
Status: | READY |
Result: | ACCEPTED |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.00 s | details |
#2 | ACCEPTED | 0.00 s | details |
#3 | ACCEPTED | 0.00 s | details |
#4 | ACCEPTED | 0.00 s | details |
#5 | ACCEPTED | 0.00 s | details |
#6 | ACCEPTED | 0.00 s | details |
#7 | ACCEPTED | 0.00 s | details |
#8 | ACCEPTED | 0.00 s | details |
#9 | ACCEPTED | 0.01 s | details |
#10 | ACCEPTED | 0.03 s | details |
#11 | ACCEPTED | 0.03 s | details |
#12 | ACCEPTED | 0.01 s | details |
#13 | ACCEPTED | 0.03 s | details |
#14 | ACCEPTED | 0.03 s | details |
#15 | ACCEPTED | 0.03 s | details |
#16 | ACCEPTED | 0.01 s | details |
#17 | ACCEPTED | 0.01 s | details |
#18 | ACCEPTED | 0.01 s | details |
#19 | ACCEPTED | 0.03 s | details |
#20 | ACCEPTED | 0.01 s | details |
#21 | ACCEPTED | 0.03 s | details |
#22 | ACCEPTED | 0.03 s | details |
#23 | ACCEPTED | 0.00 s | details |
Code
#include <bits/stdc++.h> using namespace std; #define debug(x) cerr << #x << ": " << x << endl; #define ll long long #define ull unsigned long long #define vi vector<int> #include <bits/stdc++.h> using namespace std; // Function to find a triplet with a given sum in an array void find3Numbers(vector<ll>& arr, ll sum) { int n = arr.size(); vector<pair<ll,size_t>> val_and_pos (n); for (int i = 0; i < n; i++) { val_and_pos[i] = {arr[i], i}; } // Sort the elements sort(val_and_pos.begin(), val_and_pos.end()); // Fix the first element one by one // and find the other two elements for (int i = 0; i < n - 2; i++) { // To find the other two elements, start two index // variables from two corners of the array and move // them toward each other int l = i + 1; // index of the first element // in the remaining elements int r = n - 1; // index of the last element while (l < r) { int curr_sum = val_and_pos[i].first + val_and_pos[l].first + val_and_pos[r].first; if (curr_sum == sum) { cout << val_and_pos[i].second + 1 << " " << val_and_pos[l].second + 1 << " " << val_and_pos[r].second + 1 << endl; return; } else if (curr_sum < sum) l++; else // curr_sum > sum r--; } } // If we reach here, then no triplet was found cout << "IMPOSSIBLE" << endl; } int main() { int n; ll x; cin >> n >> x; vector<ll> arr (n); for (int i = 0; i < n; i++) { cin >> arr[i]; } find3Numbers(arr, x); return 0; }
Test details
Test 1
Verdict: ACCEPTED
input |
---|
1 3 1 |
correct output |
---|
IMPOSSIBLE |
user output |
---|
IMPOSSIBLE |
Test 2
Verdict: ACCEPTED
input |
---|
3 5 1 3 2 |
correct output |
---|
IMPOSSIBLE |
user output |
---|
IMPOSSIBLE |
Test 3
Verdict: ACCEPTED
input |
---|
3 6 1 3 2 |
correct output |
---|
1 3 2 |
user output |
---|
1 3 2 |
Test 4
Verdict: ACCEPTED
input |
---|
3 7 3 2 1 |
correct output |
---|
IMPOSSIBLE |
user output |
---|
IMPOSSIBLE |
Test 5
Verdict: ACCEPTED
input |
---|
7 3 2 1 1 2 2 1 1 |
correct output |
---|
2 3 7 |
user output |
---|
2 3 7 |
Test 6
Verdict: ACCEPTED
input |
---|
7 4 1 1 2 2 1 2 1 |
correct output |
---|
1 2 6 |
user output |
---|
1 2 6 |
Test 7
Verdict: ACCEPTED
input |
---|
7 5 1 2 1 2 2 1 1 |
correct output |
---|
1 2 5 |
user output |
---|
1 2 5 |
Test 8
Verdict: ACCEPTED
input |
---|
7 6 2 1 1 1 1 2 2 |
correct output |
---|
1 6 7 |
user output |
---|
1 6 7 |
Test 9
Verdict: ACCEPTED
input |
---|
5000 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
correct output |
---|
1 2 5000 |
user output |
---|
1 2 5000 |
Test 10
Verdict: ACCEPTED
input |
---|
5000 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
correct output |
---|
IMPOSSIBLE |
user output |
---|
IMPOSSIBLE |
Test 11
Verdict: ACCEPTED
input |
---|
5000 6 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
correct output |
---|
714 3518 4240 |
user output |
---|
714 3518 4240 |
Test 12
Verdict: ACCEPTED
input |
---|
5000 919900245 663612758 9075403 585385629 98... |
correct output |
---|
2787 465 2266 |
user output |
---|
2787 465 2266 |
Test 13
Verdict: ACCEPTED
input |
---|
5000 999989608 12983 25966 38949 51932 64915 ... |
correct output |
---|
IMPOSSIBLE |
user output |
---|
IMPOSSIBLE |
Test 14
Verdict: ACCEPTED
input |
---|
5000 1000000000 65536 131072 196608 262144 327... |
correct output |
---|
IMPOSSIBLE |
user output |
---|
IMPOSSIBLE |
Test 15
Verdict: ACCEPTED
input |
---|
5000 642700000 6427 12854 19281 25708 32135 3... |
correct output |
---|
IMPOSSIBLE |
user output |
---|
IMPOSSIBLE |
Test 16
Verdict: ACCEPTED
input |
---|
5000 919900246 663612758 9075403 585385629 98... |
correct output |
---|
193 1698 4019 |
user output |
---|
193 1698 4019 |
Test 17
Verdict: ACCEPTED
input |
---|
5000 919900247 663612758 9075403 585385629 98... |
correct output |
---|
4258 470 1911 |
user output |
---|
4258 470 1911 |
Test 18
Verdict: ACCEPTED
input |
---|
5000 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 ... |
correct output |
---|
4998 4999 5000 |
user output |
---|
4998 4999 5000 |
Test 19
Verdict: ACCEPTED
input |
---|
5000 919900247 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
correct output |
---|
IMPOSSIBLE |
user output |
---|
IMPOSSIBLE |
Test 20
Verdict: ACCEPTED
input |
---|
4999 919900245 9075403 585385629 987230075 83... |
correct output |
---|
2786 464 2265 |
user output |
---|
2786 464 2265 |
Test 21
Verdict: ACCEPTED
input |
---|
5000 1000000000 261323261 25262018 237798562 3... |
correct output |
---|
IMPOSSIBLE |
user output |
---|
IMPOSSIBLE |
Test 22
Verdict: ACCEPTED
input |
---|
5000 76305003 1 5088 10175 15262 20349 25436... |
correct output |
---|
IMPOSSIBLE |
user output |
---|
IMPOSSIBLE |
Test 23
Verdict: ACCEPTED
input |
---|
2 6 2 2 |
correct output |
---|
IMPOSSIBLE |
user output |
---|
IMPOSSIBLE |