CRICRANK - Editorial

PROBLEM LINK:

Practice
Contest: Division 3
Contest: Division 2
Contest: Division 1

Author: Daanish Mahajan
Tester & Editorialist: Aman Dwivedi

DIFFICULTY:

Cakewalk

PREREQUISITES:

None

PROBLEM:

Given the season stats of two players A and B, denoted by R, W, and C respectively, the person who is better than the other in most statistics is regarded as the better overall player. Tell who is better amongst A and B. It is known that in each statistic, the players have different values.

EXPLANATION:

Since the players have different values in each of the statistics, and the number of statistics is odd (3 stats). It means that we can always decide the sole winner among the two players A and B.

So the player who performs better than the other player in at least two of the stats is considered better among them.

Let us move towards the implementation:

  • We can simply maintain one counter for each of the players (one counter for A and one for B), where the value of the counter denotes the number of stats a player has performed better in.

  • The counter of the player having greater value for the current stat gets incremented by 1.

  • Finally, the winner is that player whose counter value is greater than that of the other player’s.

TIME COMPLEXITY:

O(1) per test case

SOLUTIONS:

Setter
#include<bits/stdc++.h>
# define pb push_back 
#define pii pair<int, int>
#define mp make_pair
# define ll long long int

using namespace std;

const int maxt = 1000;
const string newln = "\n", space = " ";
int a[] = {500, 20, 20};
int mask[8];
int main()
{   
    int t; cin >> t;
    int s[3][2];
    while(t--){
        for(int i = 0; i < 2; i++){
            for(int j = 0; j < 3; j++){
                cin >> s[j][i];
            }
        }
        int c[2]; c[0] = 0; c[1] = 0;
        int value = 0;
        for(int j = 0; j < 3; j++){
            assert(s[j][0] != s[j][1]);
            if(s[j][0] > s[j][1]){
                value |= (1 << j);
                c[0]++;
            }else{
                c[1]++;
            }
        }
        mask[value]++;
        string ans = c[0] > c[1] ? "A" : "B";
        cout << ans << endl;
    }
} 
Tester
#include<bits/stdc++.h>
using namespace std;

#define int long long

long long readInt(long long l, long long r, char endd) {
  long long x = 0;
  int cnt = 0;
  int fi = -1;
  bool is_neg = false;
  while (true) {
    char g = getchar();
    if (g == '-') {
      assert(fi == -1);
      is_neg = true;
      continue;
    }
    if ('0' <= g && g <= '9') {
      x *= 10;
      x += g - '0';
      if (cnt == 0) {
        fi = g - '0';
      }
      cnt++;
      assert(fi != 0 || cnt == 1);
      assert(fi != 0 || is_neg == false);

      assert(!(cnt > 19 || ( cnt == 19 && fi > 1) ));
    } else if (g == endd) {
      if (is_neg) {
        x = -x;
      }
      assert(l <= x && x <= r);
      return x;
    } else {
      assert(false);
    }
  }
}
string readString(int l, int r, char endd) {
  string ret = "";
  int cnt = 0;
  while (true) {
    char g = getchar();
    assert(g != -1);
    if (g == endd) {
      break;
    }
    cnt++;
    ret += g;
  }
  assert(l <= cnt && cnt <= r);
  return ret;
}
long long readIntSp(long long l, long long r) {
  return readInt(l, r, ' ');
}
long long readIntLn(long long l, long long r) {
  return readInt(l, r, '\n');
}
string readStringLn(int l, int r) {
  return readString(l, r, '\n');
}
string readStringSp(int l, int r) {
  return readString(l, r, ' ');
}


void solve()
{
  int r1,r2,w1,w2,c1,c2;
  
  r1=readIntSp(0,500);
  w1=readIntSp(0,20);
  c1=readIntLn(0,20);

  r2=readIntSp(0,500);
  w2=readIntSp(0,20);
  c2=readIntLn(0,20);

  assert(r1!=r2);
  assert(w1!=w2);
  assert(c1!=c2);

  int sc1=0,sc2=0;

  if(r1>r2)
    sc1++;
  else
    sc2++;

  if(c1>c2)
    sc1++;
  else
    sc2++;

  if(w1>w2)
    sc1++;
  else
    sc2++;

  if(sc1>sc2)
    cout<<"A"<<"\n";
  else
    cout<<"B"<<"\n";
} 

int32_t main()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);

  int t;
  t=readIntLn(1,1000);

  while(t--)
    solve();

  assert(getchar() == EOF);

return 0;
}

1 Like

Here I am going to share two of my approaches to solve the problem.
First approach:
In the first approach,I used a naive approach to solve the problem by using simple if else statement.

Implementation:

#include <bits/stdc++.h>
using namespace std;
#define int long long int

void solve(){
    pair<int,int> R;
    pair<int,int> W;
    pair<int,int> C;

    int one_wins = 0;
    int two_wins = 0;

    cin >> R.first >> W.first >> C.first;
    cin >> R.second >> W.second >> C.second;

    if(R.first > R.second){
        one_wins++;
    } else{
        two_wins++;
    }

    if(W.first > W.second){
        one_wins++;
    }else{
        two_wins++;
    }

    if(C.first > C.second){
        one_wins++;
    }else{
        two_wins++;
    }

    if(one_wins > two_wins){
        cout << 'A' << endl;
    }else{
        cout << 'B' << endl;
    }
}

int32_t main(){
   
    int test;
    cin >> test;

    while(test--){
        solve();
    }
    return 0;
}


Second Approach::
In the second approach I tried to enter the data simply using the array.
although I feel this a better approach as we could avoid a lot of unnecessary repetetion unlike the first approach.

Implementation:

#include <bits/stdc++.h>
using namespace std;

// https://www.codechef.com/START6C/problems/CRICRANK
//inorder to solve the test cases
void solve(){
    int player1[3];
    int player2[3];
    
    //inorder to enter the player one stats
    for(int i = 0;i < 3; i++){
        cin >> player1[i];
    }

    //inorder to enter the player 2 stats
    for(int i = 0;i < 3; i++){
        cin >> player2[i];
    }

    //inorder to count the number of wins
    int A = 0, B = 0;
    
    for(int i = 0;i < 3; i++){
        if(player1[i] > player2[i]){
            A++;
        }else{
            B++;
        }
    } 

    cout << (A > B ? 'A' : 'B') << endl;
}


int main(){
    int test;
    cin >> test;

    while(test--){
        solve();
    }
}