EID2 - Editorial

Whats wrong in this code::

#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int T;
  cin >> T;
  while (T--) {
    int a1, a2, a3, c1, c2, c3;
    cin >> a1 >> a2 >> a3 >> c1 >> c2 >> c3;
    vector<pair<int, int>> vect;
    vect.push_back(make_pair(a1, c1));
    vect.push_back(make_pair(a2, c2));
    vect.push_back(make_pair(a3, c3));
    sort(vect.begin(), vect.end());

    string ans;
    for (int i = 0; i < 2; i++) {
      if (vect[i].first == vect[i + 1].first) {
        if (vect[i].second == vect[i + 1].second) {
          ans = "FAIR";
        } else {
          ans = "NOT FAIR";
          break;
        }
      } else if (vect[i].second < vect[i + 1].second) {
        ans = "FAIR";
      } else {
        ans = "NOT FAIR";
        break;
      }
    }
    std::cout << ans << '\n';
  }
  return 0;
}

It fails for the following testcase:

1
17 1 9 75 29 37 

@ssjgz Thanks for your quick reply. But I can’t figure out what’s wrong here. it should be FAIR right??

Yes. Sorry, you’ve given me two bits of code and I don’t know which I’m supposed to be looking at :slight_smile:

You gave the linked solution: CodeChef: Practical coding for everyone

and copy-n-pasted the solution:

#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int T;
  cin >> T;
  while (T--) {
    int a1, a2, a3, c1, c2, c3;
    cin >> a1 >> a2 >> a3 >> c1 >> c2 >> c3;
    vector<pair<int, int>> vect;
    vect.push_back(make_pair(a1, c1));
    vect.push_back(make_pair(a2, c2));
    vect.push_back(make_pair(a3, c3));
    sort(vect.begin(), vect.end());

    string ans;
    for (int i = 0; i < 2; i++) {
      if (vect[i].first == vect[i + 1].first) {
        if (vect[i].second == vect[i + 1].second) {
          ans = "FAIR";
        } else {
          ans = "NOT FAIR";
          break;
        }
      } else if (vect[i].second < vect[i + 1].second) {
        ans = "FAIR";
      } else {
        ans = "NOT FAIR";
        break;
      }
    }
    std::cout << ans << '\n';
  }
  return 0;
}

both of which are different. Which is the one you do want to be checked?

Edit:

Just seen your edit - the new code that’s copy-n-pasted appears to work OK, for me - it passes 10’000 randomised tests, at least!

Yes!!! It’s AC when I replace read(T) with cin >> T

read(T) defined as follow::

template <class T> void read(T &x) {
  int f = 0;
  x = 0;
  char s = getchar();
  while (s < '0' || s > '9') {
    if (s == '-')
      f = 1;
    s = getchar();
  }
  while (s >= '0' && s <= '9') {
    x = (x << 3) + (x << 1) + (s ^ 48);
    s = getchar();
  }
  x = f ? -x : x;
}

Isn’t read(T) equals cin >> T??:face_with_monocle:
I’m not sure, what’s the different here?? Can you please explain it?

Anyway, many many thanks for helping :heart:

Here is my solution to this problem it establish relation between two elements of an array and put the relationship in a third array and at last compares the relationship arrays
https://www.codechef.com/viewsolution/26177703

No, you’re mixing a C-style input method (getchar) with C++ style (cin), but you’ve called ios_base::sync_with_stdio(false) so they conflict.

Edit:

May as well post my solution - we’re given two simple rules to check for violations of; not sure why people are over-complicating things so much :slight_smile:

1 Like

It doesnt work for this test case.
1
7 7 4 8 8 8

can anyone please help me out.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int a[6],i,m=INT_MAX,j,z=0;
for(i=0;i<6;i++) cin >> a[i];
for(i=0;i<3;i++)
if(a[i]<m) {m=a[i]; j=i;}
swap(a[0],a[j]); swap(a[3],a[j+3]);
if(a[1]>a[2])
{swap(a[1],a[2]); swap(a[4],a[5]);}
for(i=1;i<3;i++)
{
if(a[i]==a[i-1] && a[i+3]!=a[i+2]) z=1;
else if(a[i+3]<=a[i+2]) z=1;
}
if(z==1) cout<<“NOT FAIR”<<endl;
else cout<<“FAIR”<<endl;
}
return 0;
}

Please format your code, or link to a solution - the forum software has mangled your code and it won’t compile!

Consider the testcase:

1
2 11 11 38 73 73 

#include<iostream>
using namespace std;

int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		int A[3],C[3];
		for(int i=0;i<3;i++)
		{
			cin>>A[i];
		}
		for(int i=0;i<3;i++)
		{
			cin>>C[i];
		}
		for(int i = 1,j,key1,key2;i<3;i++)
		{
			j = i-1;
			key1 = A[i];
			key2 = C[i];
			while(j >= 0 && key1 < A[j])
			{
				A[j+1]=A[j];
				C[j+1] = C[j];
				j--;
			}
			A[j+1] = key1;
			C[j+1] = key2;
		}
		bool deal = true;
		for(int i = 1;i<3;i++)
		{
			if(A[i]==A[i-1])
			{
				if(C[i]!=C[i-1])
				{
					deal = false;
					break;
				}
			}
			if(C[i-1] > C[i])
			{
				deal = false;
				break;
			}
		}
		if(deal)
		{
			cout<<"FAIR"<<endl;
		}
		else
		{
			cout<<"NOT FAIR"<<endl;
		}
	}
	return 0;
}

I have sorted the age list/array and arranged the corresponding cost list/array, and then checked the fairness.

where I’m doing mistake that I’m resulting into a WA.

Thanks for formatting your code :slight_smile:

It fails on the following testcase:

1
12 4 6 29 24 29 
1 Like

I was doing such a silly mistake…
thanks for the help buddy.

1 Like

When you know what Integer.compare(int a, int b) does…

static String isFair(int[] age, int[] amount) {
	if((Integer.compare(age[0], age[1]) != 
				Integer.compare(amount[0], amount[1]))
			|| (Integer.compare(age[1], age[2])) !=
				Integer.compare(amount[1], amount[2])
			|| (Integer.compare(age[0], age[2])) !=
				Integer.compare(amount[0], amount[2]))
		return "NOT FAIR";
	
	return "FAIR";
}

import java.util.*;
class EID2 {

public static void main(String[] args) {
	Scanner sc=new Scanner(System.in);
	int t=sc.nextInt();
	while(t!=0)
	{
		int []a=new int[6];
		int temp=0;
		int d=0;
		int i1=0,i2=0,i3=0,i4=0;
		for(int i=0;i<6;i++)
		{
			a[i]=sc.nextInt();
		}
		int temp1[]=new int[3];
		int temp2[]=new int[3];
		for(int i=0;i<3;i++)
		{
			temp1[i]=a[i];
		}
		
		System.out.println();
		int j=0;
		for(int i=3;i<6;i++)
		{	
			temp=a[i];
			temp2[j]=temp;
			j++;
		}
		
		if(temp1[0]==temp1[1] && temp1[1]==temp1[2])
		{
			if(temp2[0]!=temp2[1] && temp2[1]!=temp2[2])
			{
				d=2;
			}
		}
		else
		if(temp1[0]==temp1[1])
		{
			if(temp2[0]!=temp2[1])
			{
				d=2;
			}
			
		}
		else
			if(temp1[1]==temp1[2])
			{
				if(temp2[1]!=temp2[2])
				{
					d=2;
				}
			}
			else
				if(temp1[0]==temp1[2])
				{
					if(temp2[0]!=temp2[2])
					{
						d=2;
					}
				}
				else
				{
					int smallest = temp1[0];
			        int biggest = temp1[0];
					for(int i=1; i< temp1.length; i++)
			        {
			                if(temp1[i] > biggest)
			                {
			                        biggest = temp1[i];
			                		i1=i;
			                }
			                else if (temp1[i] < smallest)
			                {
			                        smallest = temp1[i];
			                        i2=i;
			                }
			               
			        }
					int small = temp2[0];
			        int big = temp2[0];
					for(int i=1; i< temp2.length; i++)
			        {
			                if(temp2[i] > big)
			                {
			                        big = temp2[i];
			                        i3=i;
			                }
			                else if (temp2[i] < small)
			                {
			                        small = temp2[i];
			                        i4=i;
			                }
			               
			        }
					d=1;
				}
		if(d==2)
		{
			System.out.print("NOT FAIR"); 
		}
		else
			if(d==0)
			{
				int smallest = temp1[0];
		        int biggest = temp1[0];
				for(int i=1; i< temp1.length; i++)
		        {
		                if(temp1[i] > biggest)
		                {
		                        biggest = temp1[i];
		                		i1=i;
		                }
		                else if (temp1[i] < smallest)
		                {
		                        smallest = temp1[i];
		                        i2=i;
		                }
		               
		        }
				int small = temp2[0];
		        int big = temp2[0];
				for(int i=1; i< temp2.length; i++)
		        {
		                if(temp2[i] > big)
		                {
		                        big = temp2[i];
		                        i3=i;
		                }
		                else if (temp2[i] < small)
		                {
		                        small = temp2[i];
		                        i4=i;
		                }
		               
		        }
				if(i1==i3 && i2==i4) {
					System.out.print("FAIR");
				}
				else {
					System.out.print("NOT FAIR");
				}
			}
			else
				if(d==1) {
					if(i1==i3 && i2==i4) {
						System.out.print("FAIR");
					}
					else {
						System.out.print("NOT FAIR");
					}
				}
		//	System.out.println();
		t--;
	}
}

}

please help me, why it is giving me wrong answer. Atleast tell me, which test cases is not satisfy

  1. Your solution always prints an empty line before all the other outputs :slight_smile:
  2. Consider the testcase:
1
12 4 6 29 24 29 
1 Like

thanks buddy… :v:

1 Like

import java.util.*;
class EID2 {

public static void main(String[] args) {
	Scanner sc=new Scanner(System.in);
	int t=sc.nextInt();
	while(t!=0)
	{
		int []a=new int[6];
		int temp=0;
		int d=0;
		int i1=0,i2=0,i3=0,i4=0;
		for(int i=0;i<6;i++)
		{
			a[i]=sc.nextInt();
		}
		
		int temp1[]=new int[3];
		int temp2[]=new int[3];
		for(int i=0;i<3;i++)
		{
			temp1[i]=a[i];
		}
		int j=0;
		for(int i=3;i<6;i++)
		{	
			temp=a[i];
			temp2[j]=temp;
			j++;
		}
		
			for(int i=0;i<2;i++)
			{
				if(temp1[i]==temp1[i+1])
				{
					if(temp2[i]!=temp2[i+1])
					{
						d=2;
					}
				}
			}
			for(int i=0;i<2;i++)
			{
				if(temp2[i]==temp2[i+1])
				{
					if(temp1[i]!=temp1[i+1])
					{
						d=2;
					}
				}
			}
				if(temp1[0]==temp1[2])
				{
					if(temp2[0]!=temp2[2])
					{
						d=2;
					}
				}
				else
					if(temp2[0]==temp2[2])
					{
						if(temp1[0]!=temp1[2])
						{
							d=2;
						}
					}
				else
				{
					int smallest = temp1[0];
			        int biggest = temp1[0];
					for(int i=1; i< temp1.length; i++)
			        {
			                if(temp1[i] > biggest)
			                {
			                        biggest = temp1[i];
			                		i1=i;
			                }
			                else if (temp1[i] < smallest)
			                {
			                        smallest = temp1[i];
			                        i2=i;
			                }
			               
			        }
					int small = temp2[0];
			        int big = temp2[0];
					for(int i=1; i< temp2.length; i++)
			        {
			                if(temp2[i] > big)
			                {
			                        big = temp2[i];
			                        i3=i;
			                }
			                else if (temp2[i] < small)
			                {
			                        small = temp2[i];
			                        i4=i;
			                }
			               
			        }
					d=1;
				}
		if(d==2)
		{
			System.out.print("NOT FAIR"); 
		}
		else
			if(d==0)
			{
				int smallest = temp1[0];
		        int biggest = temp1[0];
				for(int i=1; i< temp1.length; i++)
		        {
		                if(temp1[i] > biggest)
		                {
		                        biggest = temp1[i];
		                		i1=i;
		                }
		                else if (temp1[i] < smallest)
		                {
		                        smallest = temp1[i];
		                        i2=i;
		                }
		               
		        }
				int small = temp2[0];
		        int big = temp2[0];
				for(int i=1; i< temp2.length; i++)
		        {
		                if(temp2[i] > big)
		                {
		                        big = temp2[i];
		                        i3=i;
		                }
		                else if (temp2[i] < small)
		                {
		                        small = temp2[i];
		                        i4=i;
		                }
		               
		        }
				if(i1==i3 && i2==i4) {
					System.out.print("FAIR");
				}
				else {
					System.out.print("NOT FAIR");
				}
			}
			else
				if(d==1) {
					if(i1==i3 && i2==i4) {
						System.out.print("FAIR");
					}
					else {
						System.out.print("NOT FAIR");
					}
				}
			System.out.println();
		    t--;
	}	
}

}

still not getting after improvement… can you help?

1
17 17 8 63 17 11 
1 Like

Love you Man :smiling_face_with_three_hearts:…thanks for helping me… I never thought this could finally be submit by me.

1 Like