EID2 - Editorial

my solution understandable for beginners :smile:

#include <bits/stdc++.h>
    using namespace std;
    int age[3];
    int cost[3];
    int32_t main()
    {
      ios_base::sync_with_stdio(false);
      cin.tie(NULL);
      cout.tie(NULL);
      int t ;
      cin >> t;
      while(t--){
          bool f = 1;
        for(int i = 0; i < 3 ;++i) cin >> age[i];
        for(int i = 0; i < 3 ;++i) cin >> cost[i];
        
        for(int i = 0; i < 3 ;++i)
        for(int j = 0; j < 3 ;++j){
            if(i!=j){
                if(age[i] < age[j]){
                    if(cost[i] >= cost[j]) {f = 0; break;}
                }else if(age[i] == age[j]){
                    if(cost[i] != cost[j] ) {f = 0; break;}
                }else{
                    if(cost[i] <= cost[j]) {f = 0; break;}
                }
            }
        }
        if(f) cout << "FAIR\n"; else cout << "NOT FAIR\n";
    }

    }
6 Likes

Sorting isn’t required for this problem. You can simply check for the three pairs of numbers the conditions:

  • if A_i < A_j, then C_i < C_j should hold.
  • if A_i = A_j, then C_i = C_j should hold.
  • if A_i > A_j, then C_i > C_j should hold.

My solution, implemented in C++, is here:

#include<bits/stdc++.h>

#define EXECUTE_FASTER ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define endl '\n'

using namespace std;

int returnDecision(int a, int b, int c, int d)
{
	int flag = 0;
	if (a > b && c > d)
		flag = 1;
	if (a == b && c == d)
		flag = 1;
	if (a < b && c < d)
		flag = 1;
	return flag;
}

int main()
{
	EXECUTE_FASTER;
	int t;
	cin>>t;
	while (t--)
	{
		int a1, a2, a3, c1, c2, c3;
		cin>>a1>>a2>>a3>>c1>>c2>>c3;
		int a = returnDecision(a1, a2, c1, c2);
		int b = returnDecision(a2, a3, c2, c3);
		int c = returnDecision(a1, a3, c1, c3);
		if (a && b && c)
			cout<<"FAIR"<<endl;
		else
			cout<<"NOT FAIR"<<endl;
	}
	return 0;
}
2 Likes

Why is this code giving wrong answer??

#include
#include<math.h>
using namespace std;

/* run this program using the console pauser or add your own getch, system(“pause”) or input loop */

int main()
{
long long int n ,l,r,k,t,mi=0;float s=0;
cin >> t;
for(long long int z=1;z<=t;z++)
{

		long long int a[3],b[3];
		for(long long int i=0;i<6;i++)
	    {
			if(i<3)
				cin>> a[i];
			else
				cin>>b[i-3];
		}
	    for(long long int i=0;i<2;i++)
	    {
	    	for(int j=0;j<3-i-1;j++)
	    	{
	    		if(a[j]>a[j+1])
	    		{
	    			k=a[j];
	    			a[j]=a[j+1];
	    			a[j+1]=k;
	    			k=b[j];
	    			b[j]=b[j+1];
	    			b[j+1]=k;
				}
			}
		}
		l=0;
		for(int j=0;j<3-1;j++)
	    {
	    	if(a[j]==a[j+1] && b[j]!=b[j+1])
	    	{
	    		cout<<"NOT FAIR"<<endl;
	    		l=100;
	    		break;
			}
			else if(b[j]>b[j+1])
			{
	    		cout<<"NOT FAIR"<<endl;
	    		l=100;
	    		break;
			}
		}
		
	   if(l!=100)
		cout << "FAIR"<<endl;
	}
	return 0;
}
2 Likes

Here is my solution. I used following algorithm:-
if a1-a2 has same sign as c1-c2 then it is valid distribution. Here is my code:

#include<bits/stdc++.h>
using namespace std;
int sign (int v) {
  return (0<v)-(v<0);
}
int main() {
  int t;
  cin>>t;
  while(t--) {
    int a[3], c[3], b[3], d[3];
    for(int i =0; i<3; i++) {
      cin>>a[i];
    }
    for(int i =0; i<3; i++) {
      cin>>c[i];
    }
    for(int i =0; i<2; i++) {
      b[i] = a[i] - a[i+1];
    }
    b[2] = a[2] -a[0];
    for(int i =0; i<2; i++) {
      d[i] = c[i] - c[i+1];
    }
    d[2] = c[2] -c[0];
    int flag = 1;
    for(int i = 0; i<3; i++) {
      if (sign(b[i]) != sign(d[i])) {
        flag =0;
        break;
      }
    }
    if (flag) {
      cout<<"FAIR\n";
    } else {
      cout<<"NOT FAIR\n";
    }
  }
  return 0;
}
1 Like

Giving me wrong answer.

#include <stdio.h>
struct person{
int age;
int money;
};

int main() {
int t;
scanf("%d",&t);
for(int i=0;i<t;i++){
struct person p1,p2,p3;
scanf("%d %d %d %d %d %d",&p1.age,&p2.age,&p3.age,&p1.money,&p2.money,&p3.money);
int flag=1;
if(p1.age<p2.age){
if(p1.money>p2.money) flag=-1;
}
else if(p1.age==p2.age){
if(p1.money!=p2.money) flag=-1;
}
else
{
if(p1.money<p2.money) flag=-1;
}

    if(p2.age<p3.age){
        if(p2.money>p3.money) flag=-1;
    }
    else if(p2.age==p3.age){
        if(p2.money!=p3.money) flag=-1;
    }else
    {
        if(p2.money<p3.money) flag=-1;
    }


    if(p3.age<p1.age){
        if(p3.money>p1.money) flag=-1;
    }
    else if(p3.age==p1.age){
        if(p3.money!=p1.money) flag=-1;
    }else
    {
        if(p3.money<p1.money) flag=-1;
    }

    if(flag==1) {printf("FAIR\n");}
    else
    {
        printf("NOT FAIR\n");
    }
    
    
    
}
return 0;

}

1 Like

This is my code, and it didn’t work. Was the easiest problem in the contest, and I couldn’t make it work, I’m feeling really bad…

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

int main(){
    int t;
    cin>>t;
    while(t--){
            int arr[3];
            int sum[3];
            for(int i=0;i<3;i++)
                    cin>>arr[i];
            for(int i=0;i<3;i++)
                    cin>>sum[i];
            bool flag=true;
            for(int i=0;i<3;i++){
                    for(int j=i+1;j<3;j++){
                            if(arr[i]>arr[j]&&sum[i]<sum[j])
                                    flag=false;
                            if(arr[i]==arr[j]&&sum[i]!=sum[j])
                                    flag=false;
                            if(arr[i]<arr[j]&&sum[i]>sum[j])
                                    flag=false;
                    }
            }
            if(flag)
                    cout<<"FAIR"<<endl;
            else
                    cout<<"NOT FAIR"<<endl;
     }
     return 0;
}

I don’t understand what’s wrong with it. Somebody please help me out :frowning:

1 Like

I am pretty sure that i covered almost every cases , please help me out if i overlooked any case .
My solution
Thank you.

1 Like

This by no means is the most efficient program nor the easiest but it was my first codechef challenge.
cases = int(input())
for case in range(cases):
data = list(map(int, input().split())) #splitting the input
fair = True
for child1 in range(2): #number of children - 1
for child2 in range(child1 + 1, 3): #from child 1 to the end
if ((data[child1] > data[child2]) and (data[child1 + 3] > data[child2 + 3])):
fair = True
elif ((data[child1] < data[child2]) and (data[child1 + 3] < data[child2 + 3])):
fair = True
elif ((data[child1] == data[child2]) and (data[child1 + 3] == data[child2 + 3])):
fair = True
else:
fair = False
break #Break out of the inner loop
if not fair: #Break out of the outer loop
break
#Printing the result
if fair:
print(“FAIR”)
else:
print(“NOT FAIR”)

1
2 13 7 46 84 84
FAIR

your code fails at this tc

3 Likes

My Solution

#include <bits/stdc++.h>
#define ll long long
#define fo(i,n) for(i=0 ; i<n ; i++)
#define Fo(i,k,n) for(i=k ; i<n ; i++)

using namespace std;

void pairsort(int a[], int b[], int n)
{
pair<int, char> pairt[n];

// Storing the respective array 
// elements in pairs. 
for (int i = 0; i < n; i++)  
{ 
    pairt[i].first = a[i]; 
    pairt[i].second = b[i]; 
} 

// Sorting the pair array. 
sort(pairt, pairt + n); 
  
// Modifying original arrays 
for (int i = 0; i < n; i++)  
{ 
    a[i] = pairt[i].first; 
    b[i] = pairt[i].second; 
} 

}

int main() {

ios_base::sync_with_stdio(false);
cin.tie(NULL);

ll t;
cin>>(t);
while(t--){
    
    int C[3],A[3];
    cin>>A[0]>>A[1]>>A[2]>>C[0]>>C[1]>>C[2];
   
    if(A[0]==A[1]&& A[1]==A[2] && C[0]==C[1]&& C[1]==C[2])
        cout<<"FAIR"<<"\n";
    else if(A[0]==A[1]&& A[1]==A[2] && (C[0]!=C[1] || C[1]!=C[2]) )
        cout<<"NOT FAIR"<<"\n";
    else if(A[0]==A[1] && C[0]!=C[1] || A[2]==A[1] && C[2]!=C[1] || A[0]==A[2] && C[0]!=C[2] )
        cout<<"NOT FAIR"<<"\n";
    else{
       pairsort(A,C,3);
       
 //      cout<<C[0]<<" "<<C[1]<<" "<<C[2]<<"\n";
        if(A[0]==A[1] && C[0]==C[1] && C[0]<C[2])
             cout<<"FAIR"<<"\n";
        else if(A[2]==A[1] && C[2]==C[1] && C[0]<C[2])
             cout<<"FAIR"<<"\n";
        else if(C[0]<C[1]&& C[1]<C[2])
            cout<<"FAIR"<<"\n";
        else
            cout<<"NOT FAIR"<<"\n";
   
   }
    
}

return 0;

}

format code correctly

Well caught, the conditions should’ve been “<=” and “>=”
Thanks bro

No problem , good luck .

try this

1
2 13 7 46 84 84

Can anyone pls tell whats wrong with this code

#include
using namespace std;

int main() {
int t;cin>>t;
while(t–)
{
int a[3],b[3],i;
for(i=0;i<3;i++)
{
cin>>a[i];
}
for(i=0;i<3;i++)
{
cin>>b[i];
}
int flag=0;
for(i=1;i<3;i++)
{
if(i>1)
{
if((a[i]==a[i-2]&&b[i]!=b[i-2])||(a[i]-a[i-2]>0&&b[i]-b[i-2]<=0)||(a[i]-a[i-2]<0&&b[i]-b[i-2]>=0))
flag=1;break;
}
if(a[i]-a[i-1]>0&&b[i]-b[i-1]>0)
continue;
else if((a[i]-a[i-1]<0&&b[i]-b[i-1]<0))
continue;
else if((a[i]-a[i-1]==0&&b[i]-b[i-1]==0))
continue;
else
{
flag=1;break;
}

       if(i>1&&a[i]==a[i-2]&&b[i]!=b[i-2])
        {
            flag=1;break;
        }
        
    }
    if(flag)
    {
        cout<<"NOT FAIR"<<endl;
    }
    else
    {
        cout<<"FAIR"<<endl;
    }
}
return 0;

}

1 Like

thanx syntaxhacker… it helped , I just wonder how I can come up with this kind of test cases like you ! Can you suggest anything .

learn about stress testing here - https://www.coursera.org/lecture/algorithmic-toolbox/stress-test-implementation-Bskph
implement it with ac solution you will find them where your code fails.
or just think about a tc :smile:

1 Like

Thank you … I shall give it a try

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

#define floop(a,b) for(int i=a;i<b;i++)
#define floops(a,b) for(int j=a;j<b;j++)
#define ifloop(a,b) for(vector::iterator it1=a;it1!=b;it1++)
#define tc() int t; cin>>t; while(t–)
#define ll long long
#define ull unsigned long long

int main(){
tc(){
float A[3];
float C[3];
ll count=0;

    floop(0,3)
        cin>>A[i];
    floop(0,3)
        cin>>C[i];

    float Csum=0;
    float Asum=0;
    floop(0,3){
        Csum=Csum+C[i];
        Asum=Asum+A[i];
    }
    Csum=Csum/3.0;
    Asum=Asum/3.0;

    floop(0,3){
        A[i]=A[i]-Asum;
        C[i]=C[i]-Csum;
    }

    floop(0,3){
        if((A[i]<0 && C[i]<0)||(C[i]>0 && A[i]>0)||(C[i]==0 && A[i]==0))
            count++;
    }

    if(count==3){
        cout<<"FAIR"<<endl;
    }
    else
        cout<<"NOT FAIR"<<endl;
}
}

First time i used codechef and participated in this contest. Here is my code

#include <stdio.h>

int main(void)
{
int no_of_test,a1,a2,a3,c1,c2,c3;
scanf("%d\n",&no_of_test);

while(no_of_test--)
{
    scanf("%d %d %d %d %d %d\n",&a1,&a2,&a3,&c1,&c2,&c3);
    
    if((a1==a2)&&(a1==a3)&&(c1==c2)&&(c1==c3))
    {
        printf("FAIR\n");
    }
    
    else if(((a1>a2)&&(a1>a3)&&(c1>c2)&&(c1>c3)) || ((a1==a3)&&(c1==c3)))
    {
        if(((a2>a3)&&(c2>c3))||((a2==a3)&&(c2==c3)))
        {
            printf("FAIR\n");
        }
        else if((a3>a2)&&(c3>c2))
        {
            printf("FAIR\n");
        }
        else
        {
            printf("NOT FAIR\n");
        }
    }
    else if(((a2>a1)&&(a2>a3)&&(c2>c1)&&(c2>c3)) || ((a1==a2)&&(c1==c2)))
    {
        if(((a1>a3)&&(c1>c3))||((a1==a3)&&(c1==c3)))
        {
            printf("FAIR\n");
        }
        else if((a3>a1)&&(c3>c1))
        {
            printf("FAIR\n");
        }
        else
        {
            printf("NOT FAIR\n");
        }
    }
    else if(((a3>a1)&&(a3>a2)&&(c3>c1)&&(c3>c2)) || ((a2==a3)&&(c2==c3)))
    {
        if(((a1>a2)&&(c1>c2))||((a1==a2)&&(c1==c2)))
        {
            printf("FAIR\n");
        }
        else if((a2>a1)&&(c2>c1))
        {
            printf("FAIR\n");
        }
        else
        {
            printf("NOT FAIR\n");
        }
    }
    else
    {
        printf("NOT FAIR\n");
    }
}

}