From 0ff499e7b9bd8aae676285957bb508ea849ee6dc Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sat, 21 Mar 2020 23:03:48 +0100 Subject: feat: coding contest 21/03/20 --- I_anno/Programmazione_2/scheletri.cpp | 56 +++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 I_anno/Programmazione_2/scheletri.cpp (limited to 'I_anno/Programmazione_2') diff --git a/I_anno/Programmazione_2/scheletri.cpp b/I_anno/Programmazione_2/scheletri.cpp new file mode 100644 index 0000000..2389d25 --- /dev/null +++ b/I_anno/Programmazione_2/scheletri.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +#include + +using namespace std; + +int find_i(int index, vector cms) { + for(int i = index; i < cms.size(); ++i) { + if(cms.at(i) != 0) + return i; + } + + return 0; +} + +int find_j(int index, vector cms) { + for(int j = index; j < cms.size()-1; ++j) { + if(cms.at(j+1) == 0) + return j+1; + } + + return cms.size(); +} + +int main() { + ifstream in("input.txt"); + ofstream out("output.txt"); + + short N; + for(int __c = 0; __c < 100; ++__c) { + in >> N; + vector cms; + int el; + for(short i = 0; i < N; ++i) { + in >> el; + cms.push_back(el); + } + + int counter{}; + int i{}, j{}; + while(count_if(begin(cms), end(cms), [](int num) { return num == 0; } ) != cms.size() ) { + i = find_i(i, cms); + j = find_j(i, cms); + for(int ii = i; ii < j; ++ii) { + --cms[ii]; + } + ++counter; + } + out << counter << endl; + } + + out.close(); + in.close(); + return 0; +} -- cgit v1.2.3-18-g5258