consolidate all repos to one for archive
This commit is contained in:
@@ -0,0 +1,254 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <fstream>
|
||||
#include <numeric>
|
||||
#include <chrono>
|
||||
#include <filesystem>
|
||||
|
||||
bool isDevider(std::multiset<int> &v, int n)
|
||||
{
|
||||
for (auto i : v)
|
||||
{
|
||||
if (i % n != 0)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int findMaxSlow(std::multiset<int> &v)
|
||||
{
|
||||
int f = *(v.begin());
|
||||
for (size_t i = f; i >= 1; --i)
|
||||
{
|
||||
if (isDevider(v, i))
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::vector<int> findSlow(std::ifstream &file)
|
||||
{
|
||||
int lines = 0;
|
||||
file >> lines;
|
||||
char inst = 0;
|
||||
int num = 0;
|
||||
std::multiset<int> v;
|
||||
|
||||
std::vector<int> out_vec;
|
||||
|
||||
for (size_t i = 0; i < lines; i++)
|
||||
{
|
||||
file >> inst >> num;
|
||||
if (inst == '+')
|
||||
{
|
||||
v.insert(num);
|
||||
}
|
||||
else
|
||||
{
|
||||
v.erase(v.find(num));
|
||||
}
|
||||
out_vec.push_back(findMaxSlow(v));
|
||||
}
|
||||
return out_vec;
|
||||
}
|
||||
|
||||
int findMaxFast(std::multiset<int> &v)
|
||||
{
|
||||
int gretestCommonDevider = *(v.begin());
|
||||
for (auto i : v)
|
||||
{
|
||||
if (gretestCommonDevider != 0)
|
||||
{
|
||||
// GCD is deviding averithing and its not deviding the new number find the new devider
|
||||
if (i % gretestCommonDevider != 0)
|
||||
{
|
||||
gretestCommonDevider = std::gcd(gretestCommonDevider, i);
|
||||
// if GCD is one return 1 becouse 1 is min GCD
|
||||
if (gretestCommonDevider == 1)
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return gretestCommonDevider;
|
||||
}
|
||||
|
||||
std::vector<int> findFast(std::ifstream &file)
|
||||
{
|
||||
int lines = 0;
|
||||
file >> lines;
|
||||
char inst = 0;
|
||||
int num = 0;
|
||||
std::multiset<int> v;
|
||||
int devider = 0;
|
||||
|
||||
std::vector<int> out_vec;
|
||||
out_vec.reserve(lines);
|
||||
|
||||
file >> inst >> num;
|
||||
v.insert(num);
|
||||
devider = findMaxFast(v);
|
||||
|
||||
out_vec.push_back(devider);
|
||||
|
||||
int64_t add = 0;
|
||||
int64_t erase = 0;
|
||||
|
||||
for (size_t i = 1; i < lines; i++)
|
||||
{
|
||||
file >> inst >> num;
|
||||
|
||||
if (inst == '+')
|
||||
{
|
||||
// if you add number to the list just check if its devided by GCD if not find new one
|
||||
v.insert(num);
|
||||
if (devider != 0)
|
||||
{
|
||||
if (num % devider != 0)
|
||||
devider = std::gcd(num, devider);
|
||||
}
|
||||
else
|
||||
devider = std::gcd(num, devider);
|
||||
}
|
||||
else
|
||||
{
|
||||
v.erase(v.find(num));
|
||||
devider = findMaxFast(v);
|
||||
}
|
||||
|
||||
if (devider == 0)
|
||||
out_vec.push_back(1);
|
||||
else
|
||||
out_vec.push_back(devider);
|
||||
}
|
||||
|
||||
return out_vec;
|
||||
}
|
||||
|
||||
#if 0
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc != 3)
|
||||
return 1;
|
||||
int fast = std::stoi(argv[1]);
|
||||
|
||||
std::ifstream file(argv[2], std::ios::in);
|
||||
if (!file.is_open())
|
||||
return 1;
|
||||
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
|
||||
std::vector<int> out_vec;
|
||||
if (fast)
|
||||
out_vec = findFast(file);
|
||||
else
|
||||
out_vec = findSlow(file);
|
||||
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
|
||||
auto time = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
|
||||
std::ofstream out_file("out.txt", std::ios::out);
|
||||
if (!out_file.is_open())
|
||||
return 1;
|
||||
|
||||
for (auto i : out_vec)
|
||||
{
|
||||
out_file << i << std::endl;
|
||||
}
|
||||
|
||||
std::cout << time.count() << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
int main()
|
||||
{
|
||||
std::string directory_path = "sopek_marjetic/";
|
||||
|
||||
std::vector<std::string> files;
|
||||
std::vector<std::string> files_out;
|
||||
|
||||
// Iterate over the files in the directory
|
||||
for (const auto &entry : std::filesystem::directory_iterator(directory_path))
|
||||
{
|
||||
// Check if the entry is a regular file
|
||||
if (std::filesystem::is_regular_file(entry.path()))
|
||||
{
|
||||
if (entry.path().extension() == ".in")
|
||||
files.push_back(entry.path().string());
|
||||
else if (entry.path().extension() == ".out")
|
||||
files_out.push_back(entry.path().string());
|
||||
}
|
||||
}
|
||||
|
||||
std::sort(files.begin(), files.end());
|
||||
std::sort(files_out.begin(), files_out.end());
|
||||
printf(" %8s\t %8s\t %8s\t %s\n", "fast", "slow", "ratio", "file");
|
||||
|
||||
for (int i = 0; i < files.size(); i++)
|
||||
{
|
||||
std::vector<int> res_vec;
|
||||
std::vector<int> out_vec;
|
||||
int num = 0;
|
||||
std::ifstream out_file(files_out[i], std::ios::in);
|
||||
if (!out_file.is_open())
|
||||
continue;
|
||||
|
||||
while (!out_file.eof())
|
||||
{
|
||||
out_file >> num;
|
||||
out_vec.push_back(num);
|
||||
}
|
||||
|
||||
std::ifstream file(files[i], std::ios::in);
|
||||
if (!file.is_open())
|
||||
continue;
|
||||
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
res_vec = findFast(file);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto fast = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
|
||||
for (int i = 0; i < res_vec.size(); i++)
|
||||
{
|
||||
if (res_vec[i] != out_vec[i])
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
file.seekg(0, std::ios::beg);
|
||||
|
||||
auto start1 = std::chrono::high_resolution_clock::now();
|
||||
res_vec = findSlow(file);
|
||||
auto end1 = std::chrono::high_resolution_clock::now();
|
||||
auto slow = std::chrono::duration_cast<std::chrono::milliseconds>(end1 - start1);
|
||||
|
||||
for (size_t i = 0; i < res_vec.size(); i++)
|
||||
{
|
||||
if (res_vec[i] != out_vec[i])
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (fast.count() == 0)
|
||||
fast = std::chrono::milliseconds(1);
|
||||
if (slow.count() == 0)
|
||||
slow = std::chrono::milliseconds(1);
|
||||
|
||||
float ratio = (float)slow.count() / (float)fast.count();
|
||||
|
||||
printf(" %8ld\t %8ld\t %8f\t %s\n", fast.count(), slow.count(), ratio, files[i].c_str());
|
||||
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,30 @@
|
||||
CC=g++
|
||||
|
||||
CFLAGS= -std=c++23
|
||||
|
||||
INCLUDE_DIR= include
|
||||
|
||||
SRC_DIR= src
|
||||
|
||||
# Get all cpp files from src directory
|
||||
SRCS := $(wildcard $(SRC_DIR)/*.cpp)
|
||||
|
||||
all: main
|
||||
|
||||
main: main.cpp $(SRCS)
|
||||
$(CC) $(CFLAGS) -O3 -I$(INCLUDE_DIR) $(SRCS) main.cpp -o main
|
||||
|
||||
debug: main.cpp $(SRCS)
|
||||
$(CC) $(CFLAGS) -I$(INCLUDE_DIR) $(SRCS) -pg -g main.cpp -o main
|
||||
|
||||
slow_run: main
|
||||
./main 0 sopek_marjetic/marjetice04.in
|
||||
|
||||
fast_run: main
|
||||
./main 1 sopek_marjetic/marjetice04.in
|
||||
|
||||
zip:
|
||||
zip -r main.zip main.cpp $(INCLUDE_DIR) $(SRC_DIR) makefile
|
||||
|
||||
clean:
|
||||
rm main
|
||||
@@ -0,0 +1,13 @@
|
||||
fast slow ratio file
|
||||
1 1 1.000000 sopek_marjetic/marjetice00.in
|
||||
2 156 78.000000 sopek_marjetic/marjetice01.in
|
||||
2 20 10.000000 sopek_marjetic/marjetice02.in
|
||||
230 147462 641.139160 sopek_marjetic/marjetice03.in
|
||||
2498 6306 2.524420 sopek_marjetic/marjetice04.in
|
||||
241 300236 1245.792480 sopek_marjetic/marjetice05.in
|
||||
242 944863 3904.392578 sopek_marjetic/marjetice06.in
|
||||
218 218 1.000000 sopek_marjetic/marjetice08.in
|
||||
223 227 1.017937 sopek_marjetic/marjetice09.in
|
||||
2120 4770 2.250000 sopek_marjetic/marjetice10.in
|
||||
249 37365 150.060242 sopek_marjetic/marjetice12.in
|
||||
242 22636 93.537193 sopek_marjetic/marjetice13.in
|
||||
@@ -0,0 +1,25 @@
|
||||
23
|
||||
+ 49140
|
||||
+ 920640
|
||||
- 49140
|
||||
- 920640
|
||||
+ 227220
|
||||
+ 545160
|
||||
- 545160
|
||||
- 227220
|
||||
+ 824460
|
||||
+ 107940
|
||||
- 824460
|
||||
+ 605220
|
||||
- 605220
|
||||
+ 732480
|
||||
- 107940
|
||||
- 732480
|
||||
+ 613200
|
||||
- 613200
|
||||
+ 293160
|
||||
+ 691320
|
||||
- 293160
|
||||
- 691320
|
||||
+ 394380
|
||||
- 394380
|
||||
@@ -0,0 +1,23 @@
|
||||
49140
|
||||
420
|
||||
920640
|
||||
1
|
||||
227220
|
||||
420
|
||||
227220
|
||||
1
|
||||
824460
|
||||
420
|
||||
107940
|
||||
420
|
||||
107940
|
||||
420
|
||||
732480
|
||||
1
|
||||
613200
|
||||
1
|
||||
293160
|
||||
840
|
||||
691320
|
||||
1
|
||||
394380
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user