From a21575f569952a0099b2b307812223748c3bb70b Mon Sep 17 00:00:00 2001 From: pixelkubek Date: Sat, 18 Nov 2023 22:33:34 +0100 Subject: [PATCH] Added zad6 --- README.md | 2 ++ src/cw6/zad6/zad6.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/cw6/zad6/zad6.cpp diff --git a/README.md b/README.md index 0cc5fc6..13cd61a 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,8 @@ _WDP\* MIM UW_ - [zad_4](./src/cw6/zad4/zad4.cpp): autor: @pixelkubek (C++) :white_check_mark: - [zad. 5.] - [katastrofy](./src/cw6/zad5/katastrofy.cpp) (C++) :white_check_mark::microscope: +- [zad. 6.] + - [zad_6](./src/cw6/zad6/zad6.cpp): autor: @pixelkubek (C++) :white_check_mark: - [zad. 7.] - [zad_7](./src/cw6/zad7/zad7_skyline.cpp): autor: @pixelkubek (C++) :white_check_mark: - [zad. 8.] diff --git a/src/cw6/zad6/zad6.cpp b/src/cw6/zad6/zad6.cpp new file mode 100644 index 0000000..b7740b3 --- /dev/null +++ b/src/cw6/zad6/zad6.cpp @@ -0,0 +1,50 @@ +#include +#include +#include + +using namespace std; + +int *widoczne(int t[], int n){ + int *wynik = (int*)calloc((size_t)n, sizeof(int)); + int powtorki = 0; + stack s; + for(int i = 0; i < n; i++){ + while(!s.empty() && s.top() <= t[i]){ + if(s.top() == t[i]) + powtorki++; + s.pop(); + wynik[i]++; + } + if(!s.empty()) + wynik[i]++; + s.push(t[i]); + for(int j = 0; j < powtorki; j++) s.push(t[i]); + powtorki = 0; + } + + while(!s.empty()) s.pop(); + for(int i = n - 1; i >= 0; i--){ + while(!s.empty() && s.top() <= t[i]){ + if(s.top() == t[i]) + powtorki++; + s.pop(); + wynik[i]++; + } + if(!s.empty()) + wynik[i]++; + s.push(t[i]); + for(int j = 0; j < powtorki; j++) s.push(t[i]); + powtorki = 0; + } + return wynik; +} + +int main(){ + // int n = 7, t[] = {1, 8, 6, 7, 4, 8, 9}; + int n = 5, t[] = {1, 8, 5, 6, 4}; + int *wynik = widoczne(t, n); + for(int i = 0; i < n; i++) + printf("%i ", wynik[i]); + printf("\n"); + return 0; +} \ No newline at end of file