Greedy Algorithm: How Many Classes a Week at Most

AC is a wonderful thing!

Calculate the maximum number of classes a student can take:

how are you

Input data:

The first line is an integer t (1≤t≤100), representing the number of groups of data. Next for each set of data: 

The first line is an integer 
n (1≤n≤100), which represents the number of all optional courses. 
Next n lines, each with two integers 

a,b (1≤a≤7,1≤b≤6), means that there is a course to choose from in the b session of week a.


Output data:
For each set of data, output a line: 
the first line is an integer, indicating the maximum number of courses the student can take in a week.
#include<iostream>
#include<string>
#include<set>
using namespace std;
const int MAX = 8;
int main() {
	ios::sync_with_stdio(false); cin.tie(0);
	set<int> aSet[MAX];
	int num, classNum, day, course;
	cin >> num;
	for (int i = 0; i < num; i++) {
		//set<int> aSet[MAX]; this is a mistake
		int count = 0;
		cin >> classNum;
		for (int j = 0; j < classNum; j++) {
			cin >> day >> course;
			if (!aSet[day].count(course))//There is no conflict in the course corresponding to this week
			{
				aSet[day].insert(course);
				count++;
			}
		}
		cout << count << endl;
		for (int i = 0; i < MAX; i++) {
			aSet[i].clear();
		}
	}

	return 0;
}


There are several problems in the submission process:
1. After a data group is processed, the count and set are not cleared, resulting in multiple WAs. After that, it is necessary to compile it on your own computer;
2. For each data group, redeclare a set, Later on the Runtime Error..

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324815392&siteId=291194637