Algorithm


Problem Name: 2 AD-HOC - beecrowd | 1840

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1840

The Prisoner of Azkaban

 

By Leandro Zatesko, UFFS BR Brazil

Timelimit: 1

In 1950, four men were arrested, accused of setting fire to the church of Chapecó. Whether they were guilty indeed no one will ever know, but the rage of the people is always faster in judging than the courts. Worried about maintaining the physical integrity of the prisoners, the police chief intended to transfer them to Azkaban. “They will be safer in the hands of the dementors than in the hands of the people of Chapecó”, he declared while making the arrangements with the Minister of Magic for the transfer, scheduled for the next morning.

While the prisoners were waiting sleepless for the transfer which would never occur, they decided to play Dammit, a very popular game in Brazil. In one of its many versions, the rules of the game are:

  • Only 40 cards of a traditional french pack with 52 cards are used, discarding all cards with number 8, 9 or 10. The basic ascending ordering of the ranks used in Dammit is:

4 5 6 7 Q J K A 2 3

  • One of the players deals n cards to each player. Then, this player flips a card on the table, which determines the trumps of the game, which are the cards with rank immediately above the rank of the flipped card in the basic ordering. For example, if a card with rank 7 is flipped, the cards defined as the trumps of the game are Q♦, Q♠, Q♥ and Q♣. If a card with rank 3 is flipped, the trumps are 4♦, 4♠, 4♥ and 4♣. The trumps of a match worth more than any other card. Although the suit does not interfere in the value of non-trump cards, among trumps the ascending ordering of value of the suits are:

♦  ♠  ♥  ♣

  • Once the trumps of a match are defined, each player must say how many of the n rounds he thinks he will make. All players must declare their guesses even before the start of the rounds. Then begin the rounds, in each of which each player plays one of his cards revealing it on the table. A player is said to make a round if the card he plays in that round is of greater value than all the other cards played in that round. Since the suits undo a tie only among trumps, it is possible for a round not to be made by any player.
  • At the end of the game, each player scores as many points as the difference between the number of rounds he said he would make and the number of rounds he has actually made. The player with lowest score wins.

 

Input

 

The first line of the input informs the integer n (1 ≤ n ≤ 9), followed by the card flipped on the table in the beginning of the match. Each one of the 4 following lines informs the name of a player, followed by an integer m (0 ≤ mn), which represents the number of rounds the player declared he would make in the beginning of the match. The ordering in which the players are informed is always the same as they play in each round. Follow at last n lines, in a manner that the i-th of these lines informs the 4 cards played in the i-th round, in the ordering in which the cards were played. Each card is informed under the format XY, with X ∈ {4, 5, 6, 7, Q, J, K, A, 2, 3}, Y ∈ {D, S, H, C}, and D, S, H and C corresponding respectively to the suits ♦, ♠, ♥ and ♣. Consider that the name of each player consists of at least 1 and at most 10 characters of the set {a, b, …, z, A, B, …, Z}.

 

Output

 

Print a line containing only the name of the winner of the match. If it is not possible to define a single winner for the match, print a line containing only the character star (*).

 

 

 

Input Samples Output Samples

3 4H
Ivo 3
Romano 2
Orlando 0
Armando 1
2C 3S JD 6H
2H KS 7D 4C
5C 7C QH 5D

Orlando

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


#include <bits/stdc++.h>

using namespace std;
 
string ordem = "32AKJQ7654";
 				
string manilha[4];
int j;
struct card
{
    char op;
    char np;
     
    card() {op = '4'; np = 'D';}
    card(string s) : op(s[0]), np(s[1]) {}
     
    bool operator == (const card &other) const
    {
    	int flag = 0;
    	for (int i = 0 ; i  <  4; ++i)
    	{
    		if ((op == manilha[i][0] && np == manilha[i][1]) || (other.op == manilha[i][0] && other.np == manilha[i][1]))
    		{
    			
    			flag = 1;
    			break;
    		}
    	}
    	if (!flag)
	        return op == other.op;
	    else return op == other.op && np == other.np;
    }
    bool operator  <  (const card &other) const
    {
        int pos1 = -1, pos2 = -1;
         
        for (int i = 0 ; i  <  4; ++i)
        {
            if (this->op == manilha[i][0] && this->np == manilha[i][1]) pos1 = i;
            if (other.op == manilha[i][0] && other.np == manilha[i][1]) pos2 = i;
        }
        int t = pos1 != -1;
        int g = pos2 != -1;
         
        if (t && !g) return false;
        if (g && !t) return true;
        if (!t && !g) 
        {
            int posx1, posx2;
            for (j = 0; j  <  ordem.size() ; ++j)
            {
                if (this->op == ordem[j]) posx1 = j;
                if (other.op == ordem[j]) posx2 = j;
            }
            return posx1 > posx2;
        }
        else
        {
            return pos1  <  pos2;
        }
    }
};
 
void add(string v)
{
    if (v[0] == '4') {manilha[0] = (("5D"));manilha[1] = (("5S"));manilha[2] = (("5H"));manilha[3] = (("5C"));}
    if (v[0] == '5') {manilha[0] = (("6D"));manilha[1] = (("6S"));manilha[2] = (("6H"));manilha[3] = (("6C"));}
    if (v[0] == '6') {manilha[0] = (("7D"));manilha[1] = (("7S"));manilha[2] = (("7H"));manilha[3] = (("7C"));}
    if (v[0] == '7') {manilha[0] = (("QD"));manilha[1] = (("QS"));manilha[2] = (("QH"));manilha[3] = (("QC"));}
    if (v[0] == 'Q') {manilha[0] = (("JD"));manilha[1] = (("JS"));manilha[2] = (("JH"));manilha[3] = (("JC"));}
    if (v[0] == 'J') {manilha[0] = (("KD"));manilha[1] = (("KS"));manilha[2] = (("KH"));manilha[3] = (("KC"));}
    if (v[0] == 'K') {manilha[0] = (("AD"));manilha[1] = (("AS"));manilha[2] = (("AH"));manilha[3] = (("AC"));}
    if (v[0] == 'A') {manilha[0] = (("2D"));manilha[1] = (("2S"));manilha[2] = (("2H"));manilha[3] = (("2C"));}
    if (v[0] == '2') {manilha[0] = (("3D"));manilha[1] = (("3S"));manilha[2] = (("3H"));manilha[3] = (("3C"));}
    if (v[0] == '3') {manilha[0] = (("4D"));manilha[1] = (("4S"));manilha[2] = (("4H"));manilha[3] = (("4C"));}
}
int main()
{
    string vira;
    int n;
    string s[4];
    int v[4] = {0};
    int vencedor[4] = {0};
    card play[4];
    while (cin >> n >> vira)
    {
        add(vira);
        for (int i = 0 ; i  <  4; ++i) v[i] = vencedor[i] = 0;
         
        for (int i = 0 ; i  <  4; ++i)
        {
            cin >> s[i] >> v[i];
        }
         
        while (n--)
        {
            for (int i = 0 ; i  <  4; ++i)
                cin >> play[i].op >> play[i].np;
            card aux = play[0];
             
            for (int i = 1; i  <  4; ++i)
            	if (aux < play[i]) aux = play[i];
            
            	
			int cnt = 0;    
			int pos = -1;            
            for (int i = 0; i  <  4; ++i)
            {
                if (aux == play[i])
                {
       			    ++cnt;
       			    pos = i;     
                }
            }
            if (cnt == 1) ++vencedor[pos];
        }
         
        int menor = INT_MAX;
        int alerta;
        int pos;
        for (int i = 0 ; i  <  4; ++i)
        {
            if (menor > abs(v[i] - vencedor[i]))
            {
                pos = i;
                alerta = 0;
                menor = abs(v[i] - vencedor[i]);
            }
            else if (menor == abs(v[i] - vencedor[i])) alerta = 1;
        }
        if (!alerta)
       		cout << s[pos] << '\n';
        else cout << "*\n";
         
    }   
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
3 4H
Ivo 3
Romano 2
Orlando 0
Armando 1
2C 3S JD 6H
2H KS 7D 4C
5C 7C QH 5D

Output

x
+
cmd
Orlando
Advertisements

Demonstration


Previous
#1837 Beecrowd Online Judge Solution 1837 Preface Solution in C++, Java, Js and Python
Next
#1845 Beecrowd Online Judge Solution 1845 Efilogue Solution in C, C++, Java, Js and Python