-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from witek-formanski/mszopa
Add solutions for Zad6/Cw7, Zad11/Cw6
- Loading branch information
Showing
4 changed files
with
271 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |