B - Full House 2 Editorial by en_translator
For beginners
- If you are new to learning programming and do not know where to start, please try Problem A "Welcome to AtCoder" from practice contest. There you can find a sample code for each language.
- Also, if you are not familiar with problems in programming contests, we recommend you to try some problems in "AtCoder Beginners Selection".
- 「C++入門 AtCoder Programming Guide for beginners (APG4b)」 is a C++ tutorial for competitive programmers. Sadly, this is only in Japanese too.
- 「Python入門 AtCoder Programming Guide for beginners (APG4bPython)」 is a Python tutorial for competitive programmers. Again, this is only in Japanese.
Try adding one card
It is sufficient to consider adding one of cards \(1,2,\dots,13\).
One can try all of them to find the correct answer.
Decision of whether a given set of card is Full House has already appeared in ABC263-A (one can utilize the solution introduced afterward).
Sample code (C++):
#include<bits/stdc++.h>
using namespace std;
bool isFullHouse(int a,int b,int c,int d,int e){
vector<int> card={a,b,c,d,e};
sort(card.begin(),card.end());
if(
card[0]==card[1] && card[1]==card[2] &&
card[2]!=card[3] &&
card[3]==card[4]
){
return true;
}
if(
card[0]==card[1] &&
card[1]!=card[2] &&
card[2]==card[3] && card[3]==card[4]
){
return true;
}
return false;
}
int main(){
int a,b,c,d;
cin >> a >> b >> c >> d;
for(int e=1;e<=13;e++){
if(isFullHouse(a,b,c,d,e)){
cout << "Yes\n";
return 0;
}
}
cout << "No\n";
return 0;
}
Rephrasing the condition
In fact, the given set can form Full House by adding one more card if and only if either:
- it consist of three cards with the same number, and one card with another number; or
- it consist of two cards with the same number, and two cards with another number.
The proof immediately follows from removing one card from Full House.
Solution 2. Sort and check
Let \(A_1,A_2,A_3\), and \(A_4\) be the given cards, sorted in ascending order.
- It consist of three cards with the same number, and one card with another number.
This condition is equivalent to:
\(A_1 = A_2 = A_3 \neq A_4\); or
\(A_1 \neq A_2 = A_3 = A_4\).
It consist of three cards with the same number, and one card with another number.
This condition is equivalent to:
- \(A_1 = A_2 \neq A_3 = A_4\).
One can solve this to solve this problem.
Sample code (C++):
#include<bits/stdc++.h>
using namespace std;
int main(){
vector<int> a(4);
cin >> a[0] >> a[1] >> a[2] >> a[3];
sort(a.begin(),a.end());
if(a[0]==a[1] && a[1]==a[2] && a[2]!=a[3]){
cout << "Yes\n";
return 0;
}
if(a[0]!=a[1] && a[1]==a[2] && a[2]==a[3]){
cout << "Yes\n";
return 0;
}
if(a[0]==a[1] && a[1]!=a[2] && a[2]==a[3]){
cout << "Yes\n";
return 0;
}
cout << "No\n";
return 0;
}
If you take a closer look at this solution, one can derive the following condition:
- The answer is
Yes
if and only if exactly one of the following holds: \(A_1 \neq A_2, A_2 \neq A_3\), and \(A_3 \neq A_4\).
Sample code (C++):
#include<bits/stdc++.h>
using namespace std;
int main(){
vector<int> a(4);
cin >> a[0] >> a[1] >> a[2] >> a[3];
sort(a.begin(),a.end());
int c=0;
if(a[0]!=a[1]){c++;}
if(a[1]!=a[2]){c++;}
if(a[2]!=a[3]){c++;}
if(c==1){cout << "Yes\n";}
else{cout << "No\n";}
return 0;
}
Solution 3. solve in the manner of packet sort
Check the rephrased condition in the manner of packet sort. In the sample code, we utilize the map
function of C++.
Sample code (C++):
#include<bits/stdc++.h>
using namespace std;
int main(){
map<int,int> m1,m2;
for(int i=0;i<4;i++){
int a;
cin >> a;
m1[a]++;
}
for(auto &nx : m1){
m2[nx.second]++;
}
if(m2[1]==1 && m2[3]==1){
cout << "Yes\n";
}
else if(m2[2]==2){
cout << "Yes\n";
}
else{
cout << "No\n";
}
return 0;
}
Bonus
In fact, there are only five ways to group cards by the written numbers. These five cases all appear in the samples.
- \((1,1,1,1)\)
- \((2,1,1)\)
- \((2,2)\)
- \((3,1)\)
- \((4)\)
The number of such division is known as the topic called partition function.
posted:
last update: