Skip to content

Commit

Permalink
sum3val
Browse files Browse the repository at this point in the history
  • Loading branch information
megargayu committed Oct 26, 2022
1 parent 72a0716 commit c03a1d6
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 0 deletions.
25 changes: 25 additions & 0 deletions cses/sortingsearching/sum3val/brute.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <bits/stdc++.h>
using namespace std;

vector<int> a(5000);

int main()
{
int n, x;
cin >> n >> x;

for (int i = 0; i < n; ++i)
cin >> a[i];

for (int i = 2; i < n; ++i)
for (int j = 1; j < i; ++j)
for (int k = 0; k < j; ++k)
if (a[i] + a[j] + a[k] == x)
{
cout << i + 1 << ' ' << j + 1 << ' ' << k + 1 << '\n';
return 0;
}

cout << "IMPOSSIBLE\n";
return 0;
}
1 change: 1 addition & 0 deletions cses/sortingsearching/sum3val/correct.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8 7 3
1 change: 1 addition & 0 deletions cses/sortingsearching/sum3val/incorrect.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
IMPOSSIBLE
2 changes: 2 additions & 0 deletions cses/sortingsearching/sum3val/input.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
9 71
5 10 48 49 31 43 16 7 19
45 changes: 45 additions & 0 deletions cses/sortingsearching/sum3val/stress.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from random import randint
from subprocess import run, PIPE

def gen_input():
n = randint(1, 10)
x = randint(1, 100)
vals = [str(randint(1, 100)) for _ in range(n)]

return f"{n} {x}\n{' '.join(vals)}\n"

def stress():
generated = gen_input()
my_solution = run(["./sum3val"], stdout=PIPE, input=generated, encoding="ascii")
correct_solution = run(["./brute"], stdout=PIPE, input=generated, encoding="ascii")

print(my_solution.stdout.split(" "), correct_solution.stdout.split(" "))
correct = (my_solution.stdout == correct_solution.stdout == "IMPOSSIBLE") or \
(set(map(str.strip, my_solution.stdout.split(" "))) ==
set(map(str.strip, correct_solution.stdout.split(" "))))

if not correct:
with open("input.in", "w") as fin:
fin.write(generated)

with open("correct.out", "w") as fout:
fout.write(correct_solution.stdout)

with open("incorrect.out", "w") as fout:
fout.write(my_solution.stdout)

return False

return True


i = 1
while True:
result = stress()
if result:
print(f"Success on test case {i}")
else:
print(f"Found wrong test case ({i})!")
break

i += 1
45 changes: 45 additions & 0 deletions cses/sortingsearching/sum3val/sum3val.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// https://cses.fi/problemset/task/1641

// TODO

#include <bits/stdc++.h>
using namespace std;

#define MAX_N 5000

vector<pair<int, int>> a(MAX_N);

int main()
{
int n, x;
cin >> n >> x;

for (int i = 0; i < n; ++i)
cin >> a[i].first, a[i].second = i + 1;

sort(a.begin(), a.begin() + n, [](const auto &a, const auto &b)
{ return a.first < b.first; });

for (int i = 0; i < n; ++i)
{
const int need = x - a[i].first;
int l = 0, r = n - 1;
while (l != r)
{
if (l != i && r != i && a[l].first + a[r].first == need)
{
cout << a[i].second << ' ' << a[l].second << ' ' << a[r].second << '\n';
return 0;
}

if (a[l].first + a[r].first < need)
++l;
else
--r;
}
}

cout << "IMPOSSIBLE\n";

return 0;
}

0 comments on commit c03a1d6

Please sign in to comment.