From 483d63fa7249ad8d6020680c48c3cf6df35010b3 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Wed, 26 Apr 2017 16:37:39 +0200 Subject: Moved all C++ files into CPP folder --- cpp/sorveglianza.c++ | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 cpp/sorveglianza.c++ (limited to 'cpp/sorveglianza.c++') diff --git a/cpp/sorveglianza.c++ b/cpp/sorveglianza.c++ new file mode 100644 index 0000000..4154596 --- /dev/null +++ b/cpp/sorveglianza.c++ @@ -0,0 +1,88 @@ +/* INPUT: +10 +6 +2 5 +0 2 +1 3 +5 6 +4 7 +7 9 + +OUTPUT: +4*/ +#include +#include +#include + +using namespace std; + +int main(void) +{ + ifstream in; + ofstream out; + in.open("input.txt"); + out.open("output.txt"); + + int giorni, turniTot, i, j, tot = 0, m[2], x; + + in >> giorni; + in >> turniTot; + + vector< vector > turni(turniTot, vector(2)); + int* eDay = new int[giorni]; + + for(i = 0; i < giorni; i++) eDay[i] = 0; + + for(i = 0; i < turniTot; i++) + { + for(j = 0; j < 2; j++) + { + in >> x; + turni[i][j] = x; + } + } + + for(i = 0; i < turniTot-1; i++) + { + if(turni[i][0] > turni[i+1][0]) { + m[0] = turni[i][0]; + m[1] = turni[i][1]; + + turni[i][0] = turni[i+1][0]; + turni[i][1] = turni[i+1][1]; + + turni[i+1][0] = m[0]; + turni[i+1][1] = m[1]; + } + + if(turni[i][1] == giorni-1) { + m[0] = turni[i][0]; + m[1] = turni[i][1]; + + turni[i][0] = turni[i+1][0]; + turni[i][1] = turni[i+1][1]; + + turni[i+1][0] = m[0]; + turni[i+1][1] = m[1]; + } + } + + for(i = 0; i < turniTot-1; i++) + { + if(eDay[turni[i][0]] == 0) { + for(j = turni[i][0]; j <= turni[i][1]; j++) eDay[j] = 1; + tot++; + } else if(eDay[turni[i][1]] == 0 && turni[i][1] > turni[i+1][0]) { + for(j = turni[i][1]; j >= turni[i][0]; j--) eDay[j] = 1; + tot++; + } + } + + out << tot << endl; + + delete[] eDay; + in.close(); + out.close(); + + return 0; +} -- cgit v1.2.3-18-g5258