consolidate all repos to one for archive
This commit is contained in:
95
semester_5/aturp/naloga_2/main.cpp
Normal file
95
semester_5/aturp/naloga_2/main.cpp
Normal file
@@ -0,0 +1,95 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
|
||||
std::vector<std::vector<int>> vec;
|
||||
|
||||
bool test(int num)
|
||||
{
|
||||
int prev = 0;
|
||||
int count = 0;
|
||||
|
||||
for (size_t i = 0; i < vec.size(); i++)
|
||||
{
|
||||
for (size_t j = 0; j < vec[i].size(); j++)
|
||||
{
|
||||
if (vec[i][j] <= num)
|
||||
continue;
|
||||
|
||||
if (count == 0)
|
||||
{
|
||||
prev = vec[i][j];
|
||||
count++;
|
||||
}
|
||||
else if (count == 1)
|
||||
{
|
||||
if (prev != vec[i][j])
|
||||
return false;
|
||||
count = 0;
|
||||
}
|
||||
}
|
||||
if (count > 0)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int alg(int start, bool s, int end, bool e)
|
||||
{
|
||||
if (end - start == 1)
|
||||
{
|
||||
if (s)
|
||||
return start;
|
||||
else if (e)
|
||||
return end;
|
||||
}
|
||||
int middle = (start + end) / 2;
|
||||
bool m = test(middle);
|
||||
|
||||
if (m == e)
|
||||
{
|
||||
return alg(start, s, middle, m);
|
||||
}
|
||||
else if (s == m)
|
||||
{
|
||||
return alg(middle, m, end, e);
|
||||
}
|
||||
|
||||
return middle;
|
||||
}
|
||||
|
||||
int start_alg(int num)
|
||||
{
|
||||
bool s = test(0);
|
||||
bool e = test(num);
|
||||
return alg(0, s, num, e);
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
std::ifstream ifile(argv[1]);
|
||||
int size = 0;
|
||||
ifile >> size;
|
||||
|
||||
int num = 0;
|
||||
int max = 0;
|
||||
|
||||
vec.emplace_back(std::vector<int>());
|
||||
vec.emplace_back(std::vector<int>());
|
||||
|
||||
for (size_t i = 0; i < 2; i++)
|
||||
{
|
||||
for (size_t j = 0; j < size; j++)
|
||||
{
|
||||
ifile >> num;
|
||||
if (num > max)
|
||||
max = num;
|
||||
vec[i].push_back(num);
|
||||
}
|
||||
}
|
||||
|
||||
int fin = start_alg(max);
|
||||
std::cout << fin << "\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user