Skip to content

Commit

Permalink
Merge pull request #27 from witek-formanski/mszopa
Browse files Browse the repository at this point in the history
Add solutions for Zad6/Cw7, Zad11/Cw6
  • Loading branch information
MrD4rkne authored Dec 4, 2023
2 parents dbbd937 + 58db2c7 commit ca5c7e9
Show file tree
Hide file tree
Showing 4 changed files with 271 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ _WDP\* MIM UW_
- [zad3_ms](./src/cw6/zad3/zad3_ms.cpp): autor: @MrD4rkne (C++) :white_check_mark:
- [zad. 4.]
- [zad_4](./src/cw6/zad4/zad4.cpp): autor: @pixelkubek (C++) :white_check_mark:
- [zad_4](./src/cw6/zad4/zad4_ms.cpp): autor: @MrD4rkne (C++) :white_check_mark:
- [zad. 5.]
- [katastrofy](./src/cw6/zad5/katastrofy.cpp) (C++) :white_check_mark::microscope:
- [wtc_ms](./src/cw6/zad5/wtc_ms.cpp): autor: @MrD4rkne (C++) :white_check_mark:
Expand All @@ -160,6 +161,7 @@ _WDP\* MIM UW_
- [zad9_ms](./src/cw6/zad9/zad9_ms.cpp): autor: @MrD4rkne (C++) :white_check_mark:
- [zad. 10.]
- [zad_10](./src/cw6/zad10/zad10.cpp): autor: @pixelkubek (C++) :white_check_mark:
- [zad_10_ms](./src/cw6/zad10/zad10_ms.cpp): autor: @MrD4rkne (C++) :white_check_mark:
- [zad. 11.]
- [zad_11](./src/cw6/zad11/zad11.cpp): autor: @pixelkubek (C++) :white_check_mark:
- [zad. 12.]
Expand All @@ -185,6 +187,8 @@ _WDP\* MIM UW_
- [zad5_ms](./src/cw7/zad5/zad5_ms.cpp): autor: @MrD4rkne (C++) :white_check_mark:
- [zad. 6.]
- [rejs](./src/cw7/zad6/rejs.cpp) (C++)
- [zad. 6.]
- [zad5_ms](./src/cw7/zad6/zad6_ms.cpp): autor: @MrD4rkne (C++) :white_check_mark:

### Ćwiczenia VIII: [Listy i drzewa](WDP_.Inf.23_24Z__Listy_i_drzewa.pdf) 2023.11.29

Expand Down
60 changes: 60 additions & 0 deletions src/cw6/zad10/zad10_ms.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <iostream>
#include <stack>

using namespace std;

void clear(stack<char> *heap)
{
while (!heap->empty())
{
heap->pop();
}
}

int nawiasy(char arr[])
{
stack<char> heap;
int i = 0;
int curr = 0;
int max = 0;
while (arr[i] != '0')
{
switch (arr[i])
{
case '(':
heap.push('(');
break;
case ')':
if (heap.empty())
{
curr = 0;
clear(&heap);
}
else{
heap.pop();
curr+=2;
}

if (curr > max)
max = curr;
break;
}
i++;
}
return max;
}

int main()
{
string s = " ";
cin >> s[0];
char c = ' ';
while (c != '0' && cin >> c)
{
s += c;
}
char *temp = (char *)malloc((size_t)s.size() * sizeof(char));
for (int i = 0; i < s.size(); i++)
temp[i] = s[i];
cout << nawiasy(temp) << endl;
}
92 changes: 92 additions & 0 deletions src/cw6/zad4/zad4_ms.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include <iostream>
#include <queue>

using namespace std;

typedef enum pora
{
Wiosna,
Lato,
Jesień,
Zima
} pora;

pora next_season(pora season)
{
int v = season + 1;
return (pora)(v % 4);
}

int pory(int size, pora arr[])
{
if (size <= 0)
return -1;
int shortestLength = -1;

// take each season as first
for (int s = 0; s < 4; s++)
{
int currentLength = -1;

// queue which value is awaited
queue<pora> q;
q.push((pora)s);
for (int i = 0; i < 3; i++)
{
q.push(next_season(q.back()));
}
for (int i = 0; i < size; i++)
{
if (q.front() == arr[i])
{
if (currentLength == -1)
{
// first hit
currentLength = 0;
}
// move queue
q.push(q.front());
q.pop();

// if sequence is completed
if (q.front() == (pora)s)
{
if(shortestLength == -1)
shortestLength = currentLength;
if(shortestLength < currentLength){
shortestLength = currentLength;
}
currentLength = -1;
}
}
if (currentLength >= 0)
currentLength++;
}
}

return shortestLength;
}

int main()
{
int n;
cin >> n;
pora *arr = (pora *)malloc((size_t)n * sizeof(pora));
for (int i = 0; i < n; i++)
{
int input;
cin >> input;
if (input >= 0 && input <= 3)
{
arr[i] = (pora)input;
}
else
i --;
;
}

int w = pory(n, arr);
cout << w << endl;

free(arr);
}
115 changes: 115 additions & 0 deletions src/cw7/zad6/zad6_ms.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>

using namespace std;

struct interv
{
int a, b, v;
};

typedef struct interv *interval;

interval new_interval(int a, int b, int v)
{
interval inv = (interval)malloc(3 * sizeof(int));
inv->a = a;
inv->b = b;
inv->v = v;
return inv;
}

void bin_search(const vector<interval> intervals, int value, int change)
{
int i = 0, j = intervals.size() - 1;
while (i < j)
{
int pivot = (i + j) / 2;
if (intervals[pivot]->a < value)
{
i = pivot + 1;
}
else
j = pivot;
}
intervals[i]->v += change;
}

void push_pair(int i, int v, stack<pair<int, int>> *s)
{
pair<int, int> newTop(i, v);
s->push(newTop);
}

// O(nlogn)
int rejs(int k, const std::vector<int> start, const std::vector<int> end)
{
vector<int> sorted(start.size() * 2);
copy(start.begin(), start.end(), sorted.begin());
copy(end.begin(), end.end(), sorted.begin() + start.size());
// (nlogn)
sort(sorted.begin(), sorted.end());
// create vector with all intervals (relevant)
vector<interval> intervals;
for (int i = 1; i < sorted.size(); i++)
{
intervals.push_back(new_interval(sorted[i - 1], sorted[i], 0));
}
sorted.clear();

// O(nlogn)
// fill intervals with values stating change in number of crewmates
for (int i = 0; i < start.size(); i++)
{
bin_search(intervals, start[i], 1);
bin_search(intervals, end[i], -1);
}

// now find the results
// O(n)
int max = 0;
stack<pair<int, int>> maxes;
push_pair(0, intervals[0]->v, &maxes);
for (int i = 1; i < intervals.size(); i++)
{
if (intervals[i]->v < 0)
{
for (int j = 0; j < abs(intervals[i]->v); j++)
{
int currentLength = intervals[i - 1]->b - intervals[maxes.top().first]->a + 1;
if (currentLength >= k)
{
if (max < maxes.top().second)
max = maxes.top().second;
}
maxes.pop();
}
continue;
}
push_pair(i, maxes.top().second + intervals[i]->v, &maxes);
}
return max;
}

int main()
{
int k;
cin >> k;
int n;
cin >> n;
vector<int> start(n);
for (int i = 0; i < n; i++)
{
cin >> start[i];
}
vector<int> end(n);
for (int i = 0; i < n; i++)
{
cin >> end[i];
}

int w = rejs(k, start, end);
cout << w << endl;
}

0 comments on commit ca5c7e9

Please sign in to comment.