Разбор
Group 1: none of the friends is afraid of either of the animals
In this group, no one is afraid of either dogs or cats, so it does not matter which pets any person has and Anton can just invite all his friends — the answer is .
Time and memory complexity: (it is not even necessary to read all the input)
Group 2: every friend has exactly one pet and is afraid of exactly one animal
It can be seen that here there are only two types of people: those who have dogs and are afraid of cats ("1 2
" type) and vice versa ("2 1
" type). These two types cannot be together since people of both types have pets that people of other types are afraid of, but people of the same type can still be together, so the answer is .
Time complexity:
Memory complexity: or (you do not need to store all the information and process it while reading)
Group 3: none of the friends has a cat
Since there are no friends with cats in this group, we can also forget about the fact that someone can be afraid of them in this group. That is, all the ""'s and ""'s in people's fears can be replaced with ""'s and ""'s respectively)
After such replacement, we can notice that there are only two possible combinations of fears: no fears or a fear of dogs. These two cases can be processed separately:
In the case of no fears, the answer is the number of people who are not afraid of dogs.
In the case of the fear of dogs, the answer is the number of people who do not have dogs.
Out of these two cases, we need to take the maximum answer.
Time complexity:
Memory complexity: or
Group 4: every friend has at most one pet and is afraid of at most one animal
(Hint: the following solution is almost the fully correct one, but here you do not need to handle the 's)
We can notice that there are four cases in which pets are "allowed to be invited": no pets, only cats, only dogs, or both. Let's determine which conditions there are for each case:
In the case of no friends with pets invited, Anton can just invite all the friends with no pets and it does not matter if they have any fears;
In the case of only dogs invited, a person has not to have a cat and has to not be afraid of dogs;
The case of only cats invited is similar to the previous one, but here each person has to not have a dog and not be afraid of cats;
To invite a friend to the party with both animals allowed, they have to not be afraid of any animal.
Here is the implementation of handling these cases on C++:
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int ans[4] = {0}; for (int i = 0, a, b; i < n; i++) { cin >> a >> b; if (a == 0) ans[0]++; if (a != 2 and b != 1) ans[1]++; if (a != 1 and b != 2) ans[2]++; if (b == 0) ans[3]++; } int out = max({ans[0], ans[1], ans[2], ans[3]}); cout << out; }
Time complexity:
Memory complexity: or
Group 5.
This group was added to make the brute force solutions pass. It could be possible to implement, for example, using bitmasks:
int n; cin >> n; int a[n], b[n]; for (int i = 0; i < n; i++) cin >> a[i] >> b[i]; int ans = 0; for (int mask = 0; mask < (1 << n); mask++) { bool have_c = 0, have_d = 0; bool afr_c = 0, afr_d = 0; for (int i = 0; i < n; i++) if (mask >> i & 1) { have_c |= a[i] & 2; have_d |= a[i] & 1; afr_c |= b[i] & 2; afr_d |= b[i] & 1; } if ((have_c and afr_c) or (have_d and afr_d)) continue; ans = max(ans, __builtin_popcount(mask)); } cout << ans << endl;
The point is to check all the possible combinations of friends invited and take the maximum size out of the valid ones.
Time complexity:
Memory complexity:
Group 6. no additional constraints
As it was said before, this solution is the extended version of the solution for the -th group, but here we also need to handle the ""'s. Here are the additional restrictions made for each combination of allowed pets in this group:
no pets invited: (no people with both pets) / not needed since needs already to be here;
only dogs invited: (no people with both pets / both fears);
only cats invited: (the same as for the previous one);
both animals invited: / not needed since needs already to be here.
This can be done by extending the if-conditions, but this can also be done using "tricks" with bitmasks, where the -th bit stands for a cat and the -th bit stands for a dog.
Here is an example of such implementation:
int ans[4] = {0}; for (int i = 0, a, b; i < n; i++) { cin >> a >> b; if (a == 0) ans[0]++; if (!(a & 2) and !(b & 1)) ans[1]++; if (!(a & 1) and !(b & 2)) ans[2]++; if (b == 0) ans[3]++; }
Here, the restrictions for each combination of allowed pets can be seen more clearly.
Time complexity:
Memory complexity: or .