summaryrefslogtreecommitdiff
path: root/cpp/sorveglianza.c++
blob: 41545966f485821bc11cf70b2332d558fbcfefca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* INPUT:
10
6
2 5
0 2
1 3
5 6
4 7
7 9

OUTPUT:
4*/
#include <iostream>
#include <vector>
#include <fstream>

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<int> > turni(turniTot, vector<int>(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;
}