diff options
author | Santo Cariotti <dcariotti24@gmail.com> | 2020-03-21 23:03:48 +0100 |
---|---|---|
committer | Santo Cariotti <dcariotti24@gmail.com> | 2020-03-21 23:03:48 +0100 |
commit | 0ff499e7b9bd8aae676285957bb508ea849ee6dc (patch) | |
tree | 66eb3d8d9318bcebd743026adcc1fec8e7e847ec /I_anno/Programmazione_2/scheletri.cpp | |
parent | ad38f2e1ea8a392eecd1de9509455a5309d28a58 (diff) |
feat: coding contest 21/03/20
Diffstat (limited to 'I_anno/Programmazione_2/scheletri.cpp')
-rw-r--r-- | I_anno/Programmazione_2/scheletri.cpp | 56 |
1 files changed, 56 insertions, 0 deletions
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<iostream> +#include<fstream> +#include<vector> +#include<algorithm> + +using namespace std; + +int find_i(int index, vector<int> cms) { + for(int i = index; i < cms.size(); ++i) { + if(cms.at(i) != 0) + return i; + } + + return 0; +} + +int find_j(int index, vector<int> 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<int> 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; +} |