summaryrefslogtreecommitdiff
path: root/1_anno/Programmazione_2/coding_contest/scheletri.cpp
diff options
context:
space:
mode:
Diffstat (limited to '1_anno/Programmazione_2/coding_contest/scheletri.cpp')
-rw-r--r--1_anno/Programmazione_2/coding_contest/scheletri.cpp56
1 files changed, 56 insertions, 0 deletions
diff --git a/1_anno/Programmazione_2/coding_contest/scheletri.cpp b/1_anno/Programmazione_2/coding_contest/scheletri.cpp
new file mode 100644
index 0000000..2389d25
--- /dev/null
+++ b/1_anno/Programmazione_2/coding_contest/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;
+}