summaryrefslogtreecommitdiff
path: root/Year_2/Algorithms/greedy-activity.cc
blob: 3c3a9e40843953c816fa4b98190222ddc99c0336 (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
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

typedef pair<int, int> pii;

void
activity(vector<pii> v, ostream& out)
{
	int n = v.size();
	int c = 0;
	for (int i = 0; i < n; ++i) {
		for (int k = i+1; k < n; ++k) {
			int pivot = v[i].second;
			int lc = 1;
			for (int j = k; j < n; ++j) {
				if (v[j].first >= pivot) {
					lc++;
					pivot = v[j].second;
				}
			}

			c = max(c, lc);
		}
	}

	out << c << endl;
}

int
main(int argc, char** argv)
{
	int ts = (argc > 1) ? stoi(argv[1]) : 100;
	ifstream fin("input.txt");
	ofstream fout("output.txt");
	int n;
	string s;
	int x1, x2;

	for (int i = 0; i < ts; ++i) {
		fin >> n;
		vector<pii> v;

		for (int j = 0; j < n; ++j) {
			fin >> s;
			x1 = stoi(s.substr(1, s.length()));
			fin >> s;
			x2 = stoi(s.substr(0, s.length()-1));
			v.push_back({x1, x2});
		}

		sort(v.begin(), v.end(), [] (pii x1, pii x2) {
			return x1.second < x2.second;
		});

		activity(v, fout);
	}

	fin.close();
	fout.close();

	return 0;
}